Thursday, March 7, 2013

Hibernate Course: Section:8 - Hibernate API V51 - V58

In the previous section we talked about Hibernate API, as we know Hibernate is the implentation of JPA APIs, Hibernate added some extra APIs.0


we have Session in Hibernate and EntityManager in JPA.
and you can see in the table above the methods mapping.


In this section we will talk about JPA APIs

JPA Configuration
add persistence.xml , this file should be inside META-INF
add orb.hibernate dependency.


as you can see, in persistence.xml, you set the provider, in our case it is hibernate, which means we will use hibernate implementation for JPA. (other possible provider is eclipse links).

we dont have to specify the Entities we have here like what we were doing with hibernate.xml, the application will go over the classes in the class path and detect all the classes that are annotated with @Entity.

JPA Saving Entities

as you can see we have EntityManagerFactory in JPA which is similar to SesssionFactory in Hibernate.
we have EntityManager which is similar to Hibernate Session
we have EntityTransaction which is similar to Hibernate Transaction.

as you can see we use persist() in JPA to save to database

another thing "infinite-finance" is the persistence-unit name that is defined in the xml file


JPA Retreiving Entities

find() is similar to get() in hibernate.


getRegerence() is similar to load() in Hibernate.

JPA Modifying Entity 

here we do find()
then change the object
then on commit() the record will be updated

JPA Removing Entity

we use remove() which is similar to delete() in Hibernate

JPA Reattache 

we use detach() to detach, and merge() to attach again.


No comments:

Post a Comment