JPA with Hibernate 3.
0 Java persistence API
Instructor Notes:
Page 03-1
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Add instructor notes
here.
This lesson is startup for setting up JPA in our application and explains how to
perform basic operations on entities using JPA interfaces/classes.
Page 03-2
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
JPAQL is covered in
next chapter.
The shows important components of JPA that each application uses to
communicate with the database.
This lesson gives an introduction about what are those components, their
functionality and how to configure them in our application.
Page 03-3
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
The slide shows typical JPA components interaction/working.
1. You normally start with a persistence strategy by identifying which classes
need to be made entities.
2. Next step is to create configuration file (an XML document named
persistence.xml) that contains the details about the relational database.
3. EntityManagerFactory is an factory based class responsible for creating
EntityManager instance. It is obtained using Persistence class's
createEntityManagerFactory static method.
4. EntityManagerFactory class designed to create EntityManager.
5. Once you have an EntityManager, you can start managing your entities. You
can persist an entity, find one that matches a set of criteria, and so on. Each
work of EntityManager with entities must be governed under EntityTransaction.
Let us discuss the each step in detail.
Page 03-4
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Add instructor notes
here.
Requirements for Entity Classes:
The class must be annotated with the javax.persistence.Entity annotation.
The class must have a public or protected, no-argument constructor. The class
may have other constructors.
The class must not be declared final. No methods or persistent instance
variables must be declared final.
Entities may extend both entity and non-entity classes, and non-entity classes
may extend entity classes.
Persistent instance variables must be declared private, protected, or package-
private and can be accessed directly only by the entity class’s methods.
Clients must access the entity’s state through accessor or business methods.
Note: As there are two ways to configure entities, either in XML (orm.xml) or
with annotations, to keep contents simple and manageable, this course
focuses only on annotations to configure entity classes.
Page 03-5
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
The @Entity annotation marks this class as an entity bean, so it must have a
no-argument constructor that is visible with at least protected scope.
Each entity bean has to have a primary key, which you annotate on the class
with the @Id annotation.
In some situation, few properties of an entity, do not need to be stored in the
database. In this case, ORM do not take this property for all the Database
operation. This can be done using @Transient annotation.
By default, the @Id annotation will automatically determine the most
appropriate primary key generation strategy to use—you can override this by
also applying the @GeneratedValue annotation. This takes a pair of
attributes: strategy and generator
The strategy attribute must be a value from the GeneratorType enumeration,
which defines four types of strategy constants.
1. AUTO: (Default) JPA decides which generator type to use, based on the
database’s support for primary key generation.
2. IDENTITY: The database is responsible for determining and assigning the
next primary key.
3. SEQUENCE: Some databases support a SEQUENCE column type.
4. TABLE: This type keeps a separate table with the primary key values.
Note: Identity strategy depends upon database, for example, if database
(MySQL) support AUTO INCREMENT column, then we can use IDENTITY to
support this feature. For SEQUENCE and TABLE generator, see further notes.
Page 03-6
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
To connect with database, you need to set various properties regarding driver class,
user name and password. This configuration is done with an XML file named
persistence.xml.
Elements in persistence.xml:
The <persistence> is the root element of persistence.xml file. A persistence unit
defines all the entity classes that need to be managed and the JDBC details to connect
to an underlying relational database.
1. <persistence-unit> : It has the name attribute specifies a name that can be
referenced from your Java code. The transaction-type attribute informs ORM about
transaction management. It may take values like:
a. RESOURCE_LOCAL: Application will handle transaction management. i.e.
creating, starting and closing of transactions.
b. JTA: JEE server Container will take care for transaction management.
2. <provider>: Specifies the fully-qualified name of the JMS provider class. E.g.
hibernate.
3. <property>: Minimum four properties must be nested using <property> element .
needed. These properties specify the JDBC URL, JDBC username, JDBC password,
and driver.
4. <class> : Each class element specifies a fully-qualified name of an entity class. This
approach is used to inform which classes needs to be managed by JPA. i.e. Entity
classes. There may be more than one class elements.
Note: This configuration file must be stored under META-INF directory of your
application project.
Page 03-8
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Add instructor notes
here.
An EntityManager is responsible for managing entities. It is one of the most
important types in the API.
You can get an EntityManagerFactory easily by using the Persistence class's
createEntityManagerFactory() static method. It accept string parameter which
is name of persistence unit defined in persistence.xml file.
Using the EntityManagerFactory class factory, you can create EntityManager
instances using createEntityManager() method.
Note: In your application when there is no use of EntityManagerFactory or
application shuts down then it is necessary to close the instance
of EntityManagerFactory . Once the EntityManagerFactory is closed, all its
EntityManagers are also closed.
Page 03-9
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Add instructor notes
here.
The EntityManager interface defines the methods that are used to interact with
the persistence context. The EntityManager API is used to create and remove
persistent entity instances, to find persistent entities by primary key, and to
query over persistent entities.
EntityManager important methods:
1. persist(object): Persists the entity object
2. find(class,primarykey): Retrieves a specific entity object
3. remove(object): Removes an entity object
4. refresh: Refreshes the entity instances in the persistence context from the
database
5. contains: Returns true if the entity instance is in the persistence context.
This signifies that the entity instance is managed
6. flush: forces the synchronization of the database with entities in the
persistence context
7. clear: Clears the entities from the persistence context
8. evict(object): Detaches an entity from the persistence context
9. close(): Flush entity instances first, clears persistence context and nullify
the entity manager
Page 03-10
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Add instructor notes
here.
The above example shows how to persist entity instance using EntityManager.
void persist(java.lang.Object entity)
Persists an entity. The method throws an EntityExistsException if the entity
already exists and a java.lang.IllegalArgumentException if the passed in object
is not an entity.
Important: While managing instances of entities using EntityManager like
saving, updating or removing, it is very much required to work in transaction.
Therefore you need work with
EntityTransaction getTransaction()
method that returns resource-level EntityTransaction which can be used to
begin, commit or rollback transactions.
Note: You do not need an EntityTransaction for read-only operations. For
example, finding entity with EntityManager.find() method.
Page 03-11
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Add instructor notes
here.
Page 03-12
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
The above example shows how to perform CRUD (create-read-update-delete)
operations on entity instance using EntityManager.
Page 03-13
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Please debug the demo,
don’t run it. While
debugging the Client
class, ensure to display
state of table for each
breakpoint.
The above demos shows how to perform CRUD operations on entity in an
layered architecture.
Page 03-14
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Page 03-15
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Page 03-16
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Answers:
1. Option 2 and 3
2. True
Page 03-17