In this section, you will learn get a brief description about hibernate.
In this section you will learn about hibernate for j2ee.
All the famous j2ee web containers, application servers and standalone applications easily integrate with Hibernate .Generally, Hibernate is incorporated in J2EE applications using EJB session beans, Java Swing applications and in Java Servlet-based applications. Hibernate implements the persistence API and query language defined by EJB 3.0 persistence.
The advantage, supported RDBMS, architecture and configuration of Hibernate is given below :
Hibernate means ?
Hibernate is a Object-relational mapping (ORM) tool for Java. It was initiated by Gavin King in 2001. ORM methodology is used to map classes to tables, class instances is mapped to rows and attributes are mapped to table columns. It also map data types of Java to SQL data types.
Hibernate is a persistence framework which used to store and fetch data from Java environment to database table. Persistence is a storing process of data to permanent storing medium like SQL database and fetching back to any time which may be after ending the process which stores data to this table.
Hibernate supports following RDBMS :
The figure given below represents the architecture of Hibernate :
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 org.hibernate.SessionFactory . Various XML mapping files or Java Annotations compilation build these mapping.
Following 3 types of configuration is provided by the Hibernate :
This XML file resides in root of application's CLASSPATH. This file constitutes hibernate configuration.
A sample hibernate.cfg.xml file is given below :
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//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/ankdb</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.pool_size">10</property> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.current_session_context_class">thread</property> <mapping class="worker.Worker" /> </session-factory> </hibernate-configuration>
This property file contains the key-value pair for different hibernate configuration.
The sample hibernate.properties file is given below :
hibernate.connection.driver_class=com.mysql.jdbc.Driver hibernate.connection.url= jdbc:mysql://192.168.10.13:3306/ankdb hibernate.connection.username=root hibernate.connection.password=root hibernate.connection.pool_size=1 hibernate.transaction.factory_class = \ org.hibernate.transaction.JTATransactionFactory hibernate.transaction.manager_lookup_class = \ org.hibernate.transaction.JBossTransactionManagerLookup hibernate.dialect = org.hibernate.dialect.MySQLDialect
(3) Programmatic configuration
This is the manual configuration which needs to be done in Java file. We can get an instance of org.hibernate.cfg.Configuration directly by specifying mapped class using .addClass( ).
Configuration cfg = new Configuration() .addClass(worker.Worker.class) .addClass(worker.Department.class);
Building a SessionFactory
After creating instance of org.hibernate.cfg.Configuration , you can create instance of SessionFactory as follows:
SessionFactory sessions = cfg.buildSessionFactory();
Creating Session instance
Between database table and application, Session represents a communication channel. From SessionFactory , Session can be created as follows:
Session session = sessions.openSession();
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: May 19, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles