First Look at Java Persistence API
The Java Persistence API page on java.sun.com describes the Persictence API as:
The Java Persistence API provides a POJO persistence model for object-relational mapping. The Java Persistence API was developed by the EJB 3.0 software expert group as part of JSR 220, but its use is not limited to EJB software components. It can also be used directly by web applications and application clients, and even outside the Java EE platform, for example, in Java SE applications
So let’s look a bit deeper into the fetures of new API.
Persistense is dealing with entities. An Entitiy is a lightweight domain object, that shold be persistent. Typically an entity represents a table in a relational database, and each entity instance corresponds to a row in that table.
In the Java Persistens API the persistent state of an entity is represented either through persistent fields or persistent properties. These fields or properties use object/relational mapping annotations to map the entities and entity relationships to the relational data in the underlying data store.
So Entities are marked with the @Entity (javax.persistence.Entity) annotation and the entitiy-class shold implement Serializable interface. Further requirements for entitiy classes can be found in the Java EE 5 Tutorial.
Here ist just little examle of Persisten entitiy “User”.
{[.java1 /enzymes/s_java.php]}
If the entity name is the same as that of the table name, @Table is not required. The same with @Column Attribute. Not Persisten Fields or methods shold be marked with @Transient. Here more Information about persistent field and properties and especialy Primary Keys.
So far its quite simple.
Entities can have relations to each other. Java Persistence API knows four types of multiplicities: one-to-one, one-to-many, many-to-one, and many-to-many, which are marked with appropriate anotations. Furthermore relationships can be unidirectional or bidirectional. In a unidirectional relationship, only one entity has a relationship field or property that refers to the other. Read more about it.
This looks really intresting to me. But it gone to be better then, the Java Persistence API has an important new feature that was not covered bu SUn before: support for inheritance and polymorphism. An entity may inherit from another entity class. By default, the queries are polymorphic and are applicable against the entire entity hierarchy. Read more about inheritance posibilities.
An fionely ist better Query language
Queries and query language
The Java Persistence query language (JPQL) is an extension of the EJB QL, but overcomes EJB QL’s limitations and provides more features such as:
- Bulk update and delete operations
- Join operations
- Group-by operations
- Having operations
- Projection and subqueries
- Support for dynamic queries
- Use of named parameters (named parameters are parameters in a query that are prefixed with a colon (
:))
Management of entities
Entities are managed by the entity manager. The entity manager is represented by javax.persistence.EntityManager instances. Each EntityManager instance is associated with a persistence context. A persistence context is a set of managed entity instances that exist in a particular data store and defines the scope under which particular entity instances are created, persisted, and removed.
As expected the are two kinds of managers an container managed and one application managed.
With a container-managed entity manager, an EntityManager instance’s persistence context is automatically propagated by the container to all application components that use the EntityManager instance within a single Java Transaction Architecture (JTA) transaction.
{[.java2 /enzymes/s_java.php]}
With application-managed entity managers, on the other hand, the persistence context is not propagated to application components, and the lifecycle of EntityManager instances is managed by the application.
{[.java3 /enzymes/s_java.php]}
Persisten context is described in persistense.xml file
Conclusion
At first look is the Java Persistence API an real break trough. (Ofcourse i should try it out in praxis befor i’m shure
)
But the points are:
- standardizes the persistence API for the Java platform
- It simplificastion through a simple POJO-based persistence model
- annotations should makes entity coding and deployment easier
- And ofcourse features such as support for inheritance and polymorphism, simplistic entity relationship declaration,
- JPQL
Furthermore Java Persistence API can be used for Java SE environments too. This is possible because of the Java Persistence API’s support outside the EJB container. This is rely an advansed feature.
- Java Persistence API on java.sun.com
- Java Persistence API in the Java EE 5 Tototial
- Good Article goes detailed on inheritance at JavaWorld
- Extended fetures in Examples of using Persistence API in EJB tier
- Using Persistens with JSF
- Java WebStart Persistence
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.


Comments
No comments yet.
Leave a comment