Java Reference
In-Depth Information
In the test() method, we shall create a Catalog entity instances and persist the
entities to the database. We shall also query the entity instances. In the test()
method, set the flush mode to COMMIT , which implies that the changes to the entity
manager are synchronized to the database when the transaction commits:
em.setFlushMode(FlushModeType.COMMIT);
Create a Catalog entity instance and persist it to the database. We need to merge
the entity instance with the entity manager using the merge() method before
persisting it.
Catalog catalog1 =new Catalog("Kimberly Floss", "Nov-Dec 2004",
new Integer(1), Oracle Magazine", "Oracle Publishing",
"Database Resource Manager");
Catalog c1 = em.merge(catalog1);
persistEntity(c1);
Similarly, create and persist two more entity instances. An entity instance may be
found with the Java persistence query language. For example, find a catalog entity
instance by author name. First, create an instance of the Query object using the
createQuery method to run a Java persistence query language statement. Bind the
author name to a named parameter name using the setParameter method of the
Query object and run the Java persistence query statement using the getResultList
method, which returns a List :
List catalogList =em.createQuery("SELECT c from Catalog c
where c.author=:name").setParameter
("name","Jonas Jacobi").
getResultList();
Iterate over the List , which is actually just one catalog entry, to retrieve the Catalog
entity instance. Create a String , which will be returned by the test() method, from
the Catalog instance properties:
for (Iterator iter = catalogList.iterator(); iter.hasNext(); )
{
Catalog element = (Catalog)iter.next();
catalogEntry =catalogEntry + " Journal: " + element.getJournal()
+ "Publisher: " + element.getPublisher() + " Edition: "
+ element.getEdition() + " Title: "
+ element.getTitle() + " Author: "
+ element.getAuthor();
}
 
Search WWH ::




Custom Search