Explain how to configure Hibernate?
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; } }
Ads