JavaJEEJPA: Difference between revisions

From Wiki RB4
Line 1: Line 1:
=Introduction=
=Introduction=
The Java Persistence API is part of the Java SE. To search for entities there is JPQL. The API defines the interface and is no '''persistence provider'''. This is for example JBoss or Glassfish.  
The Java Persistence API is part of the Java SE. To search for entities there is JPQL. The API defines the interface and is no '''persistence provider'''. This is for example JBoss or Glassfish.  
The central instance of the JPA is the entity manager, which is provided by the persistence provider. The '''entity manager''' is responsible for saving, actualizing or deleting of entities. The entity manager provides a persistence context.  
The central instance of the JPA is the '''entity manager''', which is provided by the persistence provider. The '''entity manager''' is responsible for saving, actualizing or deleting of entities. The entity manager provides a persistence context.  
An entity in a persistence context is '''attached''' or '''managed''', otherwise '''detached'''. It can also reattached.  The entity manager is accessed via JNDI or in an EJB container by injection (javax.persistence.PeristenceContext).  
An entity in a persistence context is '''attached''' or '''managed''', otherwise '''detached'''. It can also reattached.  The entity manager is accessed via JNDI or in an EJB container by injection (javax.persistence.PeristenceContext).  
A '''persistence unit''' is the group of classes which is managed by one entity manager and references one database. A persistence unit is defined in [[#persistence.xml|persistence.xml]].
A '''persistence unit''' is the group of classes which is managed by one entity manager and references one database. A persistence unit is defined in [[#persistence.xml|persistence.xml]].

Revision as of 22:37, 7 October 2008

Introduction

The Java Persistence API is part of the Java SE. To search for entities there is JPQL. The API defines the interface and is no persistence provider. This is for example JBoss or Glassfish. The central instance of the JPA is the entity manager, which is provided by the persistence provider. The entity manager is responsible for saving, actualizing or deleting of entities. The entity manager provides a persistence context. An entity in a persistence context is attached or managed, otherwise detached. It can also reattached. The entity manager is accessed via JNDI or in an EJB container by injection (javax.persistence.PeristenceContext). A persistence unit is the group of classes which is managed by one entity manager and references one database. A persistence unit is defined in persistence.xml. The information regarding persistence is specified via annotations or deployment descriptor.

persistence.xml

  • META-INF directory of the beans jar file (s. EAR file)
<persistence>
  <persistence-unit name="<Name>"> // s. annotation PersistenceContext
  </persistence-unit>
</persistence>

Annotations

The location of the annotations e.g. before the attributes/fields or before the methods is important.

  1. Access to attributes: JPA allows for two types of access to the data of a persistent class, field/property access or method access. What access type it will be used is decided by where you put the @Id annotation (on the id field or the getId() method). Which type to use depends on: performance (reflection is faster?), logic inside the getter and/or setter, validation, independent refactoring, exception handling (see. Java Persistance with Hibernate, 3.25).
  2. Names of the db columns: Field access which means that it maps the instance variables (fields) to columns in the database and Property access which means that is uses the getters to determine the property names that will be mapped to the db.

javax.persistence.Basic

@javax.persistence.Basic([fetch=(FetchType.EAGER|FetchType.LAZY)])
<method declaration>

The Basic annotation maps the java type to a database type. It is standard and has to be used seldomly. It can be used to specify the fetch strategy. Normally all attributes are fetched while loading the entity. Lazy delays it to the first use.

javax.persistence.Column

@javax.persistence.Column[([name="<Name>"])]
<attribute declaration>


javax.persistence.Entity

  • if no table specified, tablename is classname
  • all members are persistent, unless speficied with javax.persistence.Transient

java.persistence.GeneratedValue

javax.persistence.Id

javax.persistence.Lob

@javax.persistence.Lob
<method declaration>
  • large object for large data
  • the concrete type is derived from the java datatype

javax.persistence.PersistenceContext

@javax.persistence.PersistenceContext[([type=(TRANSACTION|EXTENDED)][,][unitName="<Name>"])]
<attribute declaration>

injection via

@javax.persistence.PersistenceContext(unitName="testejbapplication") // s. persistence.xml <persistence-unit>
private javax.persistence.EntityManager em;
  • default type TRANSACTION: persistence context is closed after transaction, all persistence entities are detached
  • type EXTENDED: starts with creating entity manager and end when it is destroyed
  • object state is synchronized with database without persist() or merge() call

javax.persistance.Table

@javax.persistance.Table[([name="<Name>"])]
<class declaration>

javax.persistance.Temporal

@javax.persistance.Temporal[(TemporalType.DATE|TemporalType.TIME|TemporalType.TIMESTAMP)]
<method declaration>

Specifies the database type for the java type java.util.Date or java.util.Calendar.

Java Persistence Query Language (JPQL)

JPQL is an extension of EJBQL, which was introduced as part of the EJB 2.0 specification. The query language allows you to write portable queries that work regardless of the underlying data store.