Hibernate Tutorial
Hibernate Tutorial explains step by step details of setting / configuring Hibernate with Eclipse
Hibernate is popular open source ORM (Object Relation Mapping) tool for Java platform, for mapping an entity to a traditional relational database like Oracle, MySQL etc
By using Hibernate, we can done database operations like Create, Read, Update & Delete etc, with very little code.
We can also use Hibernate Query Language (HQL) for the jdbc operation, you can see an example of Hibernate Query Tutorial
If you want to configure Hibernate using annotations, then you can follow this example Hibernate Annotations Tutorial
Required Libraries
You need to download
Following jar must be in classpath
- antlr-2.7.6.jar
- commons-collections-3.1.jar
- javassist-3.12.0.GA.jar
- jta-1.1.jar
- hibernate-jpa-2.0-api-1.0.1.Final.jar
- hibernate3.jar
- mysql-connector-java-5.1.18-bin.jar
- dom4j-1.6.1.jar
- slf4j-api-1.6.1.jar
I am creating a sample project that persists Student object (simple POJO (Plain Old Java Object)) into database using hibernate.
Firstly create a Java Project (File->New->Project), select Java Project and click next, provide name as "HibernateExample" according to following screenshot
hibernate.cfg.xml
<?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://localhost/test</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.MySQLDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="com/javatips/student/Student.hbm.xml"/> </session-factory> </hibernate-configuration>