Working with Hibernate and JPA
Recently I was experimenting with possible combination of Hibernate and JPA. As a developer, one can have options of using hibernate mapping files or annotations (both jpa and hibernate) or JPA mapping files with JPA style Entity managers. Following are the possible combination which I found useful:
Hibernate gives you following options:
1. Use Hibernate core. When using Hibernate core, object to relationship mapping should be done via xml mapping files. Core can be used iin all java versions. For each table we will have a bean with variables representing each column and a xml descriptor to define the mapping between database table columns and the bean properties. Each such mapping file should be listed in hibernate.cfg.xml file by tag <mapping resource=”cml file name”/>. To interact with database, developers can use HibernateSessionFactory and Session Objects.
SessionFactory will be retrieved using:
sessionFactory = new Configuration().configure().buildSessionFactory();
2. Use JPA and Hibernate annotations: You can use this option in JDK 1.5 and above. This facility gives flexibility allowing one to specify meta data using JPA and Hibernate annotations. You can mark the persistent classes with the JPA annotations and then use features of hibernate using hibernate’s annotations. Each such annotated entity class should be listed in hibernate.cfg.xml file by tag <mapping/>. To interact with database, developers can use HibernateSessionFactory and Session Objects.
SessionFactory will be retrieved using AnnotationConfiguration as below:
sessionFactory = new AnnotationConfiguration ().configure().buildSessionFactory();
This setting also provides flexibility. If some hibernate functionality couldn’t be retrieved using annotations, one can also use the mapping xml file. You can define the mapping data in xml file, and replace the <mapping class=””> tag by <mapping file=””>. This setting will completely ignore the annotations. This I find very useful as it gives you flexibility during mapping.
3. Annotations with Hibernate EntityManager – JPA way: This is full JPA standard way of mapping and accessing db. Hibernate implements the EntityManager interface, defined by JPA. Using this manager, one can access db in JPA way using some common functions provided. To use this method one can annotate their classes by both hibernate and JPA annotations. If one uses JPA annotations with JPA Entity manager, the application becomes fully portable among multiple JPA providers with minimal changes.
Also you can override annotations with JPA xml mappings. But in this settings you can’t use Hibernate’s xml mapping facility nor you can embed your hibernates mappings into JPA’s mapping file. You define a persistence.xml file instead of hibernate.cfg.xml.
You create EntityManageFactory instead of SessionFactory like below:
entityManagerFactory = Persistence.createEntityManagerFactory(“defaultManager”);
and use Entitymanager to access db.
Sample codes for each type is given below:
- Hibernate core application download here – run com.myapp.client.hibernate.TaskTest
- Hibernate annotations application download here –run com.myapp.client.hibernate.TaskTest
- Annotations with Hibernate EntityManager application download here – com.myapp.client.jpa.TaskTest
You can import these project in eclipse.
The requirements for the code are:
- Mysql database.
- The jars listed in Hibernate Tutorial(link)
- The database in mysql by name: hibernatetest
- table user_tasks :
create table user_tasks(
user_task_id int(11) not null auto_increment,
task_name_desc varchar(300),
start_time date,
end_time date,
total_task_time int(20),
primary key(user_task_id)
);
Going for an interview or want to test your hibernate skills , check out our hibernate interview questions








