JavaJEEJPA
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. JEE#EAR-Files)
<persistence> <persistence-unit name="<Name>"> // s. annotation PersistenceContext </persistence-unit> </persistence>
Annotations
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.