ClassBook-Lesson03-Java Persistence API (1)
ClassBook-Lesson03-Java Persistence API (1)
Instructor Notes:
Page 03-1
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
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.
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:
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.
Page 03-4
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
The class must have a public or protected, no-argument constructor. The class
may have other constructors.
Entities may extend both entity and non-entity classes, and non-entity classes
may extend entity classes.
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.
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.
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:
2. <provider>: Specifies the fully-qualified name of the JMS provider class. E.g.
hibernate.
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:
Page 03-9
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
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.
Page 03-10
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
The above example shows how to persist entity instance using EntityManager.
EntityTransaction getTransaction()
Page 03-11
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Page 03-12
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
Page 03-13
JPA with Hibernate 3.0 Java persistence API
Instructor Notes:
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