http://www.mastertheboss.com/quickstart-tutorials-hibernate/jpa/hibernate-tutorial-with-eclipse
Hibernate is an object/relational mapping tool for Java environments. What does it mean the term object/relational mapping? simply a technique of mapping a data representation from an object model to a relational data model with a SQL-based schema.
Hibernate tutorial with Eclipse
For this tutorial we'll use Hibernate 3.5.6 GA which you can download here:
http://www.hibernate.org/downloads
The tutorial will be built using Eclipse Enterprise, anyway, since we'll create the Project as simple java project you can easily adapt it on any other IDE. Ok, at fist let's create a new Java project. We'll name it "Hibernate tutorial".
Next step is adding a Java class which will be mapped on the DB. We'll create class com.sample.Person :
Class is a sample JavaBean with properties and getters/setters
Hibernate needs to know how to load and store objects of the persistent class. This is where the Hibernate mapping file comes into play. The mapping file tells Hibernate what table in the database it has to access, and what columns in that table it should use.
Create a file named Person.hbm.xml in the same package/folder of your JavaBean Person.
The id element is the declaration of the identifier property, name="id" declares the name of the Java property - The column attribute tells Hibernate which column of the PERSON table we use for this primary key.
The nested generator element specifies the identifier generation strategy: in this case we used native, which picks the best strategy depending on the configured database (dialect).
The fields are enlist as "property". Notice the column attribute is added on every property even if this can be skipped if java property = database field.
Hibernate configuration file
This file contains the configuration information needed by Hibernate to connect to a RDBMS. For Hibernate's configuration, we can use a simple hibernate.properties file, a slightly more sophisticated hibernate.cfg.xml file, or even complete programmatic setup. Most users prefer the XML configuration file.
As you can see, hibernate.cfg.xml configures the dialect, the JDBC parameters, the connection pool, the cache provider and the single classes mapped.
This example uses MySQL as database. You need to create a database named "hibernatetutorial" and assign to the user "hibernate" all the privileges required.
mysql> create database hibernatetutorial;
Query OK, 1 row affected (0.00 sec)
CREATE USER 'hibernate'@'localhost' IDENTIFIED BY 'hibernate';
mysql> use hibernatetutorial;
Database changed
mysql> grant all privileges on hibernatetutorial to hibernate;
Query OK, 0 rows affected (0.03 sec)
In this configuration file we have set the property "hibernate.hbm2ddl.auto" to true which means automatic drop/creation of the database schema. Remember to comment this line after the first run.
At the bottom we have declared the resource "com/sample/Person.hbm.xml" which is the mapping for Class Person.
The hibernate.cfg.xml file needs to be added to classpath. We recommend creating a new folder for storing the configuration file, for example conf, and add this folder to the CLASSPATH. In Eclipse you have this option in the "Java Build Path" manu, reached from "Properties".
Project libraries
Ok, now the mapping is complete. Before creating a client appllication we'll add some libraries to the project. The following libraries can be found in Hibernate distribution:
Then you need mysql JDBC connector (or the right connector for your RDBMS)
Ok. Last library needed is the Simply Logging Facade util. This library allows to plug in the desired logging framework at deployment time (log4j /jdk1.4).
We have tested with the following version:
http://www.slf4j.org/dist/slf4j-1.5.2.zip
You can then choose either log4j or JDK logging implementation: for example if you choose JDK14 logging implementation simply add the following jar:
Accessing your Bean
For completing our Hibernate tutorial we'll create a Java Class “TestPerson” in the package “com.sample”. Add the following source code. It includes methods to create entries in the database, to update and to list them.
As you can see the TestPerson starts at first building a SessionFactory: this object is used to open up new Sessions. A Session represents a single-threaded unit of work, the SessionFactory is a thread-safe global object, instantiated once.
A Session is a single unit of work and begins when it is first needed, that is when the first call to getCurrentSession() is made. It is then bound by Hibernate to the current thread. When the transaction ends, either through commit or rollback, Hibernate automatically unbinds the Session from the thread and closes it for you.
In this example we use a SessionFactoryUtil class to instantiate the SessionFactory just once:
Ok, if you have completed all the steps your project should look like this:
<
Here are some common runtime errors and their possible solution:
org.hibernate.HibernateException: No CurrentSessionContext configured
You haven't configured the current_session_context_class property. Add this to your hibernate.cfg.xml:
<property name="current_session_context_class">thread</property>
org.hibernate.MappingException: Unknown entity: sample.hibernate.Person
It's likely that you have not added (or added with wrong Classname/namespace) the Class Mapping file.
Could not parse mapping document from resource sample/hibernate/Person.hbm.xml
Exception in thread "main" java.lang.ExceptionInInitializerError
Maybe you have forgot to add the DTD information at the top of your Person.hbm.xml
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder
You are missing the Simply Logging Facade util. Download it http://www.slf4j.org/dist/slf4j-1.5.2.zip and then add either log4j or JDK14 implementation.
Hibernate tutorial code: