This commit is contained in:
Steve Ebersole 2011-04-17 15:26:20 -05:00
parent 5c4f817255
commit 28a3e38f7a
7 changed files with 94 additions and 10 deletions

View File

@ -8,7 +8,7 @@
<xi:include href="Preface.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Preface.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Database_Access.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Database_Access.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Transactions.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Transactions.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Data_Operations.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Persistence_Context.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Batch_Processing.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Batch_Processing.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Locking.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Locking.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="Caching.xml" xmlns:xi="http://www.w3.org/2001/XInclude" /> <xi:include href="Caching.xml" xmlns:xi="http://www.w3.org/2001/XInclude" />

View File

@ -8,8 +8,9 @@
<para> <para>
Both the <interfacename>org.hibernate.Session</interfacename> API and Both the <interfacename>org.hibernate.Session</interfacename> API and
<interfacename>javax.persistence.EntityManager</interfacename> API provide methods for working with entities. <interfacename>javax.persistence.EntityManager</interfacename> API represent a context for dealing with
This chapter will explore the available operations in each. persistent data. This concept is called a <literal>persistence context</literal>. Persistent data has a
state in relation to both a persistence context and the underlying database.
</para> </para>
<itemizedlist> <itemizedlist>
@ -307,4 +308,79 @@ return managed;]]>
</section> </section>
</section> </section>
<section>
<title>Checking persistent state</title>
<para>
An application can verify the state of entities and collections in relation to the persistence context.
</para>
<example>
<title>Examples of verifying managed state</title>
<programlisting><![CDATA[assert session.contains( cat );]]></programlisting>
<programlisting><![CDATA[assert entityManager.contains( cat );]]></programlisting>
</example>
<example>
<title>Examples of verifying laziness</title>
<programlisting>
<![CDATA[if ( Hibernate.isInitialized( customer.getAddress() ) {
//display address if loaded
}
if ( Hibernate.isInitialized( customer.getOrders ) ) {
//display orders if loaded
}
if (Hibernate.isPropertyInitialized(customer, "detailedBio") ) {
//display property detailedBio if loaded
}]]>
</programlisting>
<programlisting>
<![CDATA[javax.persistence.PersistenceUnitUtil jpaUtil = entityManager.getEntityManagerFactory().getPersistenceUnitUtil();
if ( jpaUtil.isLoaded( customer.getAddress() ) {
//display address if loaded
}
if ( jpaUtil.isLoaded( customer.getOrders ) ) {
//display orders if loaded
}
if (jpaUtil.isLoaded(customer, "detailedBio") ) {
//display property detailedBio if loaded
}]]>
</programlisting>
</example>
<para>
In JPA there is an alternative means to check laziness using the following
<interfacename>javax.persistence.PersistenceUtil</interfacename> pattern. However, the
<interfacename>javax.persistence.PersistenceUnitUtil</interfacename> is recommended where ever possible
</para>
<example>
<title>Alternative JPA means to verify laziness</title>
<programlisting>
<![CDATA[javax.persistence.PersistenceUtil jpaUtil = javax.persistence.Persistence.getPersistenceUtil();
if ( jpaUtil.isLoaded( customer.getAddress() ) {
//display address if loaded
}
if ( jpaUtil.isLoaded( customer.getOrders ) ) {
//display orders if loaded
}
if (jpaUtil.isLoaded(customer, "detailedBio") ) {
//display property detailedBio if loaded
}]]>
</programlisting>
</example>
</section>
<section>
<title>Accessing Hibernate APIs from JPA</title>
<para>
JPA defines an incredibly useful method to allow applications access to the APIs of the underlying provider.
</para>
<example>
<title>Usage of EntityManager.unwrap</title>
<programlisting>
<![CDATA[Session session = entityManager.unwrap( Session.class );
SessionImplementor sessionImplementor = entityManager.unwrap( SessionImplementor.class );]]>
</programlisting>
</example>
</section>
</chapter> </chapter>

View File

@ -1,7 +1,15 @@
sessionFactory.evict(Cat.class, catId); //evict a particular Cat sessionFactory.getCache().containsEntity(Cat.class, catId); // is this particular Cat currently in the cache
sessionFactory.evict(Cat.class); //evict all Cats sessionFactory.getCache().evictEntity(Cat.class, catId); // evict a particular Cat
sessionFactory.evictCollection("Cat.kittens", catId); //evict a particular collection of kittens sessionFactory.getCache().evictEntityRegion(Cat.class); // evict all Cats
sessionFactory.evictCollection("Cat.kittens"); //evict all kitten collections sessionFactory.getCache().evictEntityRegions(); // evict all entity data
sessionFactory.getCache().containsCollection("Cat.kittens", catId); // is this particular collection currently in the cache
sessionFactory.getCache().evictCollection("Cat.kittens", catId); // evict a particular collection of kittens
sessionFactory.getCache().evictCollectionRegion("Cat.kittens"); // evict all kitten collections
sessionFactory.getCache().evictCollectionRegions(); // evict all collection data

View File

@ -102,10 +102,10 @@
<xi:include href="modules/architecture.xml" <xi:include href="modules/architecture.xml"
xmlns:xi="http://www.w3.org/2001/XInclude" /> xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="modules/configuration.xml" <xi:include href="modules/migrated/configuration.xml"
xmlns:xi="http://www.w3.org/2001/XInclude" /> xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="modules/entitymanagerapi.xml" <xi:include href="modules/migrated/entitymanagerapi.xml"
xmlns:xi="http://www.w3.org/2001/XInclude" /> xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="modules/metamodel.xml" <xi:include href="modules/metamodel.xml"
@ -117,7 +117,7 @@
<xi:include href="modules/listeners.xml" <xi:include href="modules/listeners.xml"
xmlns:xi="http://www.w3.org/2001/XInclude" /> xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="modules/batch.xml" <xi:include href="modules/migrated/batch.xml"
xmlns:xi="http://www.w3.org/2001/XInclude" /> xmlns:xi="http://www.w3.org/2001/XInclude" />
<xi:include href="modules/query_ejbql.xml" <xi:include href="modules/query_ejbql.xml"