Hibernate Configuration

Hibernate Configuration

Explain how to configure Hibernate?

View Answers

March 7, 2011 at 3:23 PM

Hibernate configuration--- It uses a XML file. The name of this file is hibernate.cfg.xml. It is used to create connection pool or established the required environment variable. Required file for configuration (by using annotation)

Step1 - hibernate.cfg.xml ? It is a configuration file, which provides database connection.

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.url">jdbc:mysql://192.168.10.13:3306/onlinexamination</property>
        <property name="hibernate.connection.username">root</property>
        <property name="connection.password">root</property>
        <property name="connection.pool_size">1</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="show_sql">true</property>
        <property name="current_session_context_class">thread</property>
        <property name="hbm2ddl.auto">update</property>
        <mapping class="net.roseindia.Model.AddInformation" />
    </session-factory>
</hibernate-configuration>

Step 2 -- HibernateUtil class The HibernateUtil is a java class, which provides SessionFactory from the Hibernate configuration file. It manages the Hibernate session. HibernateUtil class is a helper class that handle Hibernate's SessionFactory to obtain a Session object. This class load the hibernate.cfg.xml file using configure () method.

package net.roseindia.hibernateUtil;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
    private static final SessionFactory sessionFactory = buildSessionFactory();

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
            return new AnnotationConfiguration().configure()
                    .buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }
}

Step3- Model class

It is a POJO class. It has properties and their getter setter method. It also provides mapping of properties to table column.

package net.roseindia.Model;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "userinfo")
public class AddInformation implements Serializable {
    private String firstname;
    private int id;
    @Id
    @GeneratedValue
    @Column(name = "id")
    public int getId() {
        return id;
    }

    @Column(name = "firstname")
    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Step4?DAO It is java class. This class is used for accessing data from database table. The method of this class will be called from action class.

package net.roseindia.dao;

import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.classic.Session;
import net.roseindia.Model.AddInformation;
import net.roseindia.hibernateUtil.HibernateUtil;

public class InformationDAO extends HibernateUtil {
public AddInformation add(AddInformation obInformation) {
Session session =  HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        session.save(obInformation);
        session.getTransaction().commit();
        return obInformation;
    }

}









Related Tutorials/Questions & Answers:
Hibernate Configuration
Hibernate Configuration  Explain how to configure Hibernate
Hibernate dialect configuration
Hibernate dialect configuration  How to configure hibernate dialect in the Hibernate configuration file? Thanks
Advertisements
hibernate Configuration with eclpse 3.1 - Hibernate
hibernate Configuration with eclpse 3.1  Dear Sir, i m developing a program using hibernate with struts. but i am not able to configure eclipse 3.1 ide for it. so please tell me step by step configuration process
hibernate configuration with eclipse 3.1 - Hibernate
hibernate configuration with eclipse 3.1  Dear Sir, i got your mail... simple hibernate program code and with struts also. so please tell me the step by step process. i have that folder in d:/hibernate i thing
Hibernate configuration file
This tutorial helps you in understanding the configuration file of Hibernate
Struts2 And Hibernate Configuration
Struts2 And Hibernate Configuration  Sir, I am new to struts2, I am trying to write a program using struts2 and hibernate. I want to know how.... Bachan Sahoo   Struts Hibernate Integration
Eclipse configuration - Hibernate
to configure the tomcat and hibernate in eclipse?. Thanks, Anusha
Doctype Hibernate-configuration 5
Using Doctype in hibernate-configuration 5 In Hibernate 5 correct doctype should be used and this data is provided in the configuration file of Hibernate...; In the configuration of file of Hibernate we also provide
Hibernate Configuration File
Hibernate Configuration File In this tutorial you will learn about the hibernate configuration file. To define a Hibernate Configuration info a resource file...' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC
Hibernate Configuration File
Hibernate Configuration File In this section we will read about the hibernate... connection information to a file. Hibernate Configuration file i.e.... of Configuration class. Hibernate configuration file provides the information
Hibernate 5 configuration dtd
Hibernate 5 configuration dtd Example In Hibernate 5 dtd is different from... in the hibernate.cfg.xml file. What is Hibernate Configuration file? To use Hibernate we have... of hibernate.cfg file In Hibernate 5 you should use correct dtd configuration
What are the most common methods of Hibernate configuration?
What are the most common methods of Hibernate configuration?  Hi, What are the most common methods of Hibernate configuration? thanks
Can you provide me Hibernate configuration tutorial?
Can you provide me Hibernate configuration tutorial?  Hello There, I want to learn about Hibernate configuration, Can anyone provide me with hibernate configuration tutorial> Thanks
Hibernate Configuration
The configuration of Hibernate is handled by the instance of org.hibernate.cfg.Configuration . It symbolizes the complete mapping between Java data types to the SQL database or database table. It is used to construct
Hibernate Configuration files
Hibernate Configuration files - Learn about the files in the Hibernate... the configuration files of Hibernate. As a beginner you should be aware of all... based applications. Following is the list of configuration files in Hibernate
How to get hibernate configuration from sessionfactory?
How to get hibernate configuration from sessionfactory?  Hi, I need to get hibernate configuration from sessionfactory, how can i do it? Thanks..., you would be able to understand how to get hibernate configuration from
Hibernate 5 JPA Configuration
Hibernate 5 JPA Configuration in application The JPA API depends on one... will learn to use Hibernate 5 JPA Configuration file to make fully functional... the Hibernate 5 JPA Configuration code which you can used to use Hibernate 5 and JPA
Hibernate 5 JPA Configuration
Hibernate 5 JPA Configuration in application The JPA API depends on one... will learn to use Hibernate 5 JPA Configuration file to make fully functional...; In this tutorial I explained you the Hibernate 5 JPA Configuration code which
Writing Hibernate Configuration Files
Writing Hibernate Configuration Files In the previous section we completed.... In this section we will write required hibernate configuration files. For this tutorial we need following Hibernate configuration files: Hibernate Configuration
Writing Hibernate Configuration Files
Writing Hibernate Configuration Files   ... we will write required hibernate configuration files. For this tutorial we need following Hibernate configuration files:ADS_TO_REPLACE_1 Hibernate
Spring MVC and Hibernate with DWR 3 -- Configuration Using MySQL Database
Spring MVC and Hibernate with DWR 3 -- Configuration Using MySQL Database  How to configure Spring MVC and Hibernate with DWR 3, Simple example -- reading a table, editing-deleting and adding new rows
Hibernate db2
In this section, you will learn about the db2 configuration in Hibernate
Hibernate config
In this section, you will learn about Hibernate config or configuration in Hibernate
Maven Repository/Dependency: org.xwiki.contrib | xwiki-platform-cloud-configuration-hibernate-api
Maven Repository/Dependency of Group ID org.xwiki.contrib and Artifact ID xwiki-platform-cloud-configuration-hibernate-api. Latest version of org.xwiki.contrib:xwiki-platform-cloud-configuration-hibernate-api dependencies
Maven Repository/Dependency: org.xwiki.contrib | xwiki-platform-cloud-configuration-hibernate-environment
Maven Repository/Dependency of Group ID org.xwiki.contrib and Artifact ID xwiki-platform-cloud-configuration-hibernate-environment. Latest version of org.xwiki.contrib:xwiki-platform-cloud-configuration-hibernate-environment dependencies
hibernate tomcat
In this section, you will learn about Hibernate datasource configuration in Tomcat
Hibernate - Hibernate
Hibernate sessionfactory configuration  What is the process of hibernate sessionfactory configuration
HIBERNATE
HIBERNATE  How u connects database using hibernate 3.0? Where you have written the database configuration details
hibernate
hibernate  how to impot mysql database client jar file into eclipse for hibernate configuration   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/hibernate/runninge-xample.shtml
HIBERNATE
HIBERNATE   What is difference between Jdbc and Hibernate
hibernate
hibernate  what is hibernate flow
hibernate
hibernate  what is hibernate listeners
hibernate
hibernate  how to uses optional column in hibernate
hibernate
hibernate  please give me the link where i can freely download hibernate software(with dependencies)   Learn hibernate, if you want to learn hibernate, please visit the following link: Hibernate Tutorials
Hibernate
Hibernate  hibernate delete query execution
hibernate
hibernate  what is hibernate why we use
Hibernate
Hibernate  how to use pagination concept in Hibernate
hibernate
hibernate   I want to learn how to use hibernate framework in web application . for storing database in our application
hibernate
hibernate   I want to learn how to use hibernate framework in web application . for storing database in our application
hibernate
why hibernate?  why hibernate?   Hibernate: -Hibernate... library. It solves object-relational impedance mismatch problems. Hibernate makes the application development process easy Hibernate saves the development
hibernate
hibernate  pls give one simple example program for hibernate
hibernate
hibernate  why we should use hibernate projection
Hibernate
Hibernate  Please tell me the difference between hibernate and jdbc
Hibernate
Hibernate  hello, how to run hibernate program in netbeans?please send me step by step instructions to run hibernate in netbeans
hibernate
hibernate  can any one one explain what is hibernate ?   Please visit the following link: Hibernate Tutorials
hibernate
hibernate  how to execute a sequence(in database) from Hibernate program or java program   Please visit the following link: Hibernate Tutorials
Hibernate
Hibernate  How do you handle parent - child tables relationships in hibernate? How do you handle Detach state in hibernate
Hibernate - Hibernate
Hibernate Features  Hibernate features in eclipse
Hibernate
Hibernate  hi sir i need hibernate complete tutorial for download   Hi Friend, Please visit the following link: Hibernate Tutorials Thanks
Hibernate Code - Hibernate Interview Questions
Hibernate Code   Hi Friends, In Hibernate, wat is Annotation. There is no need of hibernate configuration file in hibernate version 3 - right . but hbm is necessary for all versions

Ads