SCBCD Notes: Java Persistence (Part 2)
Published November 30th, 2006 in Java.An EntityManager can be injected directly into an EJB using the @javax.persistence.PersistenceContext annotation. By default, a transaction-scoped persistence context is injected. You can override the default with type() attribute. When the transaction is completed, the EJB container will perform the cleanup. The extended entity manager can only be injected into a stateful session bean.

As mentioned in part 1 of the article, the EntityManager is the central service for all persistence actions. The persistence actions can be summarized as follow:
- Persisting Entities:
persist()method takes any entity bean instance and create a record in the database. Thepersist()method throwsIllegalArgumentExceptionif the parameter is not an entity bean. It also throwsTransactionRequiredExceptionif the method is invoked on atransaction-scopedpersistence context. However, it is legal to callpersist()outside of a transaction scope forextendedentity manager. - Finding Entities by Primary Key: Both
find()andgetReference()can be used to locate an entity. Thefind()method returnsnullif entity is not found in the database. It initializes the state based on the lazy-loading policies of each property.getReference()on the other hand throws ajavax.persistence.EntityNotFoundand the state may not be initialized. Both methods are safe to invoke outside transaction scope. - Finding Entities by Query: Entities can also be located using EJB QL. You must first construct a
javax.persistence.Queryobject using one of the five methods provided byEntityManager. - Updating Entities: Updates to the state of an entity bean is automatically synchronized to the database depending on the flush mode or you can call
flush()method directly. - Merging Entities: State changes to a detached entity bean can be saved to the database by invoking the
merge()method. - Removing Entities: The
remove()method deletes the entity bean from the database. - Refreshing Entities: The
refresh()method refreshes the state of the entity from the database, overwriting any state changes that are not flushed. If the entity bean is no longer in the database,EntityNotFoundExceptionis thrown. - Locking Entities: The
EntityManagerAPIs support read and write locks. - Determining Persistence Context: The
contains()method returnstrueif the entity bean is managed by the persistence context. - Detaching All Entities: The
clear()method detaches all entity beans from the persistence context, losing changes that are not flushed. - Saving Entities’ States: A method call of
persist(),merge()orremove()takes effect when theEntityManagerdecides to flush or by manually invokingflush()method. Auto flushing only happens before a correlated query is executed and at transaction commit time. The behavior can be changed usingFlushModeType, which takes valid values ofAUTOandCOMMIT.AUTOis the default flush mode as described before.COMMITlimits the flushing only at transaction commit time for performance reasons. - Vendor-specific APIs: The
getDelegate()method obtains a reference to the Java Persistence provider


0 Responses to “SCBCD Notes: Java Persistence (Part 2)”
Please Wait
Leave a Reply