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:
Advertisements
hibernate Configuration with eclpse 3.1 - Hibernatehibernate 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 - Hibernatehibernate 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
Struts2 And Hibernate ConfigurationStruts2 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
Doctype Hibernate-configuration 5Using 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 FileHibernate 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 FileHibernate 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 dtdHibernate 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 Hibernate ConfigurationThe
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 filesHibernate 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 Hibernate 5 JPA ConfigurationHibernate 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 ConfigurationHibernate 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 Hibernate db2In this section, you will learn about the db2
configuration in
Hibernate Hibernate configIn this section, you will learn about
Hibernate config or
configuration in
Hibernate hibernate tomcatIn this section, you will learn about
Hibernate datasource
configuration in Tomcat
Hibernate - HibernateHibernate sessionfactory configuration What is the process of
hibernate sessionfactory
configuration HIBERNATEHIBERNATE How u connects database using
hibernate 3.0? Where you have written the database
configuration details
hibernatehibernate 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 hibernatehibernate what is
hibernate listeners
hibernatehibernate how to uses optional column in
hibernate hibernatehibernate 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
HibernateHibernate
hibernate delete query execution
hibernatehibernate what is
hibernate why we use
HibernateHibernate 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
hibernatewhy
hibernate? why
hibernate?
Hibernate: -
Hibernate... library.
It solves object-relational impedance mismatch problems.
Hibernate makes the application development process easy
Hibernate saves the development
hibernatehibernate pls give one simple example program for
hibernate hibernatehibernate why we should use
hibernate projection
HibernateHibernate Please tell me the difference between
hibernate and jdbc
HibernateHibernate hello,
how to run
hibernate program in netbeans?please send me step by step instructions to run
hibernate in netbeans
hibernatehibernate can any one one explain what is
hibernate ?
Please visit the following link:
Hibernate Tutorials
hibernatehibernate how to execute a sequence(in database) from
Hibernate program or java program
Please visit the following link:
Hibernate Tutorials
HibernateHibernate How do you handle parent - child tables relationships in
hibernate?
How do you handle Detach state in
hibernate HibernateHibernate hi sir i need
hibernate complete tutorial for download
Hi Friend,
Please visit the following link:
Hibernate Tutorials
Thanks