Enterprise Java v120215Java Persistence: EntityManager1.

Slides:



Advertisements
Similar presentations
Copyright ©2010 Oracle Corporation Made available under Creative Commons Attribution-Share Alike 3.0 Unported EclipseLink Runtime Architecture Data Source.
Advertisements

Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
JBoss Seam: Contextual Components Jason Bechtel
Introduction to Enterprise JavaBeans. Integrating Software Development Server-side Component Model Distributed Object Architecture –CORBA –DCOM –Java.
Entities and Persistence. Entity Beans Topics to be Covered: Entities are POJOs Managed/Unmanaged Entities Persistence Unit EntityManager Basic Relational.
Pragmatic Application Building: Step by Step Jay Sissom Principal Systems Analyst Indiana University
Session-02.
1 Lecture 18 George Koutsogiannakis/Spring 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
An Introduction to Hibernate Matt Secoske
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Data Persistence and Object-Relational Mapping Slides by James Brucker, used with his permission 1.
Introduction to JPA Java Persistence API Introduction to JPA.
Hibernatification! Roadmap for Migrating from Plain Old SQL on JDBC to JPA on Hibernate Duke Banerjee Senior Developer, DrillingInfo.com.
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
Rice KRAD Data Layer JPA Design Eric Westfall July 2013.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
2007 JavaOne SM Conference | Session BOF-4181 | BOF-4181 Migrating a Spring/Hibernate Application to Java Platform, Enterprise Edition (Java EE) 5 Adrian.
Agenda What is Hibernate Spring Integration Questions Overview
Maven for building Java applications By Nalin De Zoysa
1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
Enterprise JavaBeans. What is EJB? l An EJB is a specialized, non-visual JavaBean that runs on a server. l EJB technology supports application development.
Enterprise Java v120131Intro to JavaEE1 Java EE 5 TM Introduction.
Topic : JPA Kaster Nurmukan. Overview of JPA EntityManager.
Enterprise Java Java Persistence: EntityManager. Enterprise Java v070903Java Persistence: EntityManager2 Goals Become familiar with the Java Persistence.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
EJB 3.0 Persistence Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
Hibernate Persistence. What is Persistence Persist data to database or other storage.  In OO world, persistence means persist object to external storage.
1 Architectural Overview For application developers, assembling enterprise beans requires little or no expertise in the complex system-level issues that.
Hibernate 3.0. What is Hibernate Hibernate is a free, open source Java package that makes it easy to work with relational databases. Hibernate makes it.
Enterprise Java v041102EJB QL1 EJB Query Language Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
MCS 270 Spring 2014 Object-Oriented Software Development.
A U.S. Department of Energy Office of Science Laboratory Operated by The University of Chicago Argonne National Laboratory Office of Science U.S. Department.
JPA / HIBERNATE CSCI 6370 Nilayan Bhattacharya Sanket Sable.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
Java Persistence API v1.0 a standard for ORM frameworks and POJO based Persistence Magnus Larsson
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool The problem fixed by ORM Advantage Hibernate Hibernate Basic –Hibernate sessionFactory –Hibernate Session.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Spring MVC Essentials Getting started.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
1 Persistence: EntityManager In Java EE 5, persistence has been spun off into its own specification: Java Persistence 1.0. Persistence provides an ease-of-use.
Enterprise Java v090125Dev Env Overview1 Enterprise Java ( ) Development Environment Overview.
The Java Persistence API ©SoftMoore ConsultingSlide 1.
Copyright 2007 SpringSource. Copying, publishing or distributing without express written permission is prohibited. Introduction to Data Access with Spring.
Java Persistence API part 1 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
JPA Transactions
v110912Java Persistence: EntityManager2 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
JPA The New Enterprise Persistence Standard Mike Keith
Topic : Hibernate 1 Kaster Nurmukan. An ORM tool Used in data layer of applications Implements JPA.
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Enterprise Java v121030Java EE Transactions1. Enterprise Java v121030Java EE Transactions2 Goals Understand the basic concepts behind a transaction Be.
Hibernate Thuy, Le Huu. Pentalog VN. Agenda Hibernate Annotations Improving performance – Lazy loading – Fetching Strategies – Dynamic insert, dynamic.
1 Entity Callbacks and Listeners When you execute EntityManager methods like persist( ), merge( ), remove( ), and find( ), or when you execute EJB QL queries,
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
Test Java EE applications with Arquillian Ivan St. Ivanov.
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Chengyu Sun California State University, Los Angeles
Java Persistence Architecture (JPA)
Advanced Java Programming
Java Programming Course
Developing and testing enterprise Java applications
Enterprise Java Beans.
CS4961 Software Design Laboratory Understand Aquila Backend
Topics Persistence API for JAVA EE5 Java Transaction API Entity class.
Chengyu Sun California State University, Los Angeles
Server-Side Programming
Plug-In Architecture Pattern
Presentation transcript:

Enterprise Java v120215Java Persistence: EntityManager1

Enterprise Java v120215Java Persistence: EntityManager2 Goals Become familiar with the Java Persistence API and EntityManager Become familiar with how to setup a project using JPA and a provider (Hibernate)

Enterprise Java v120215Java Persistence: EntityManager3 Objectives Provide an Overview of the Java Persistence API Go through Details of the EntityManager Go through some implementation details associated with class projects

Enterprise Java v120215Java Persistence: EntityManager4 Overview Earlier versions of EJB Specification defined the persistence layer –javax.ejb.EntityBean Java EE 5 moved persistence to its own specification –Java Persistence API (JPA) version 1.0 –javax.persistence ease of use API above JDBC Provides –Object/Relational Mapping (ORM) Engine –Query Language (SQL-Like, based on former EJB-QL) Java EE 6 uses JPA 2.0

Enterprise Java v120215Java Persistence: EntityManager5 javax.persistence.EntityManager Replaces much of the EJB 2.x “Home” functionality Handles O/R Mapping of Entities to the database Provides APIs –inserting objects into database –getting objects from database –synchronizing objects with database –querying database Provides caching Coordinates with transactional services (JTA) Tightly integrated with Java EE and EJB, but not limited to that environment

Enterprise Java v120215Java Persistence: EntityManager6 Entities (formerly and sometimes still called Entity Beans) are now Plain Old Java Objects (POJOs) –nothing special happens when calling new Author author = new Author(); are not persistent until associated with an EntityManager em.persist(author);

Enterprise Java v120215Java Persistence: EntityManager7 Example Author POJO public class Author { private long id; private long version=0; private String firstName; private String lastName; private String subject; private Date publishDate; public Author() {} public Author(long id) { this.id = public long getId() { return id;} private void setId(long id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; }... } Warning: Using GeneratedValue without specifying a specific strategy should only be used when you have no intention of controlling how the provider manages primary keys for this object type. This would be rare.

Enterprise Java v120215Java Persistence: EntityManager8 Creating Entity in Database Author author = new Author(); //primary key will be gen author.setFirstName("dr"); author.setLastName("seuss"); author.setSubject("children"); author.setPublishDate(new Date()); log_.info("creating author:" + author); em.persist(author); log_.info("created author:" + author); //output -creating author:id=0, fn=dr, ln=seuss, subject=children, pdate=Fri Sep 15 11:54:15 EDT created author:id=50, fn=dr, ln=seuss, subject=children, pdate=Fri Sep 15 11:54:15 EDT 2006

Enterprise Java v120215Java Persistence: EntityManager9 Managed and Unmanaged Entities Unmanaged state (detached) –instance not associated with an EntityManager –state changes are not tracked –can be serialized to client and returned to be synchronized with database –nothing equivalent to this state in EJB 2.1 entity beans Managed state (attached) –instance associated with an EntityManager –state changes are tracked within a Persistence Context –EJB 2.1 entity beans were always managed client interfaced with data through a proxy or state transferred through a Data Transfer Object

Enterprise Java v120215Java Persistence: EntityManager10 Persistence Context A set of attached entity instances managed by an EntityManager All entities become detached once closed Two types –Transaction-scoped Persistence Contexts begin/end at transaction boundaries only made available through container managed persistence contexts –Extended Persistence Contexts live beyond any single transaction allow longer-lived interactions with database without lengthy transactions tying up database resources

Enterprise Java v120215Java Persistence: EntityManager11 Persistence Context Examples Transaction-scoped (inside server EntityManager public void update(long authorId, String type) { Author author = em.find(Author.class, authorId); author.setType(type); } Extended (inside or outside server container) EntityManager em = Persistence. createEntityManagerFactory(“jpaDemo”).createEntityManager(); tx.begin(); //tx 1 begins Author author = em.find(Author.class, authorId); tx.commit(); //tx 1 ends, but author remains managed... tx.begin(); //tx 2 begins author.setType(type); tx.commit(); //tx 2 ends, and author is still managed until close

Enterprise Java v120215Java Persistence: EntityManager12 Persistence Unit A set of classes that are mapped to the database defined in META-INF/persistence.xml must have an identity –“” is a valid identity Classes –may be named in persistence.xml file –may be automatically scanned for in the classpath orm.xml –optionally provided to augment, provide, or replace class persistence metadata –(more on orm.xml in Core ORM topic)

Enterprise Java v120215Java Persistence: EntityManager13 Example Component Layout META-INF/ +---persistence.xml ejava + ---examples +---… +---DAOException.class +---AuthorDAO.class +---jpa | +---JPAAuthorDAO.class +--bo +---Author.class

Enterprise Java v120215Java Persistence: EntityManager14 Example persistence.xml <persistence xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="1.0"> java:/ejavaDS <property name="hibernate.show_sql" value="true"/ referenced by name global JNDI name by which provider references resource (will be used when deployed within server) may use properties element in Java SE environments that lack JNDI vendor-specific way to configure persistence provider

Enterprise Java v120215Java Persistence: EntityManager15 Another Example persistence.xml <persistence xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="1.0"> org.hibernate.ejb.HibernatePersistence <property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.Provider"/> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.username" value="sa"/> </persistence

Enterprise Java v120215Java Persistence: EntityManager16 Solution-specific property option: hibernate.properties Easier to provide unit testing options that do not get propagated into production artifacts $ cat hibernate.properties hibernate.dialecte=org.hibernate.dialect.HSQLDialect hibernate.connection.url=jdbc:hsqldb:hsql://localhost:9001 hibernate.connection.driver_class=org.hsqldb.jdbcDriver hibernate.connection.password= hibernate.connection.username=sa hibernate.hbm2ddl.auto=create #hibernate.show_sql=true #hibernate.format_sql=true #hibernate.jdbc.batch_size=0

Enterprise Java v120215Java Persistence: EntityManager17 persistence.xml elements name – identity to reference Persistence Unit provider – fully qualified name of javax.persistence.PersistenceProvider –not needed if provider found in classpath acceptable mapping-file – resource path to optional mapping file –can be used to specify es or details jta-data-source –vendor-specific reference to data source using JTA transactions non-jta-data-source –vendor-specific reference to data source using RESOURCE_LOCAL transactions jar-file –optional/additional jar file to scan for classes class –specifies entity classes not automatically scanned by provider exclude-unlisted-classes –if set, provider will not automatically scan archive for entity classes properties –may be used to provide vendor-specific properties to configure persistence providers

Enterprise Java v120215Java Persistence: EntityManager18 Specifying an optional orm.xml file <persistence xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="1.0">... META-INF/orm.xml... </persistence

Enterprise Java v120215Java Persistence: EntityManager19 Optional orm.xml overrides <entity-mappings xmlns=" xmlns:xsi=" xsi:schemaLocation=" version="1.0"> <entity class="ejava.examples.daoex.bo.Author" metadata-complete="false">

Enterprise Java v120215Java Persistence: EntityManager20 Entities Discovered classes annotation –in the persistence.xml's JAR file –contained in any JAR file listed in jar-file element classes mapped –with META-INF/orm.xml –with custom mapping files classes listed in persistence.xml's “ ” element

Enterprise Java v120215Java Persistence: EntityManager21 Java SE Steps Startup –Get EntityManagerFactory Runtime –Create EntityManager –Start Transaction –Interact with Entity Manager –Commit Transaction –Close EntityManager Shutdown –Close EntityManagerFactory

Enterprise Java v120215Java Persistence: EntityManager22 javax.persistence.Persistence used to bootstrap persistence in Java SE environments public class javax.persistence.Persistence { public static java.lang.String PERSISTENCE_PROVIDER; public javax.persistence.Persistence(); public static EntityManagerFactory createEntityManagerFactory(String puName); public static EntityManagerFactory createEntityManagerFactory(String puName, Map props); } example usage EntityManagerFactory emf = Persistence.createEntityManagerFactory(“jpaDemo”);

Enterprise Java v120215Java Persistence: EntityManager23 javax.persistence.EntityManagerFactory used to obtain reference to EntityManager in standard Java SE environments package javax.persistence; public interface EntityManagerFactory{ EntityManager createEntityManager(); EntityManager createEntityManager(Map props); void close(); boolean isOpen(); } Map used to supply or override properties in persistence.xml close() and isOpen() only valid for non-injected EMFs example usage – EntityManager em = emf.createEntityManager();

Enterprise Java v120215Java Persistence: EntityManager24 javax.persistence.EntityManager package javax.persistence; public interface EntityManager{ EntityTransaction getTransaction(); void persist(java.lang.Object); Object find(Class, Object pKey); Object getReference(Class, Object pKey); boolean contains(Object); Object merge(java.lang.Object); void refresh(java.lang.Object); void remove(java.lang.Object); Object find(java.lang.Class, java.lang.Object); void flush(); void clear(); void close(); isOpen(); --- Query createQuery(java.lang.String); Query createNamedQuery(java.lang.String); Query createNativeQuery(java.lang.String); Query createNativeQuery(java.lang.String, java.lang.Class); Query createNativeQuery(java.lang.String, java.lang.String);...

Enterprise Java v120215Java Persistence: EntityManager25 persist() Author author = new Author(); author.setFirstName("dr"); author.setLastName("seuss"); author.setSubject("children"); author.setPublishDate(new Date()); em.persist(author); Extended persistence contexts –queues write until associated with transaction Transaction-scoped persistence contexts –illegal to call outside the scope of a transaction Actual write to the database depends on FlushMode –manually controlled with flush() call

Enterprise Java v120215Java Persistence: EntityManager26 find() Author author2=null; author2 = em.find(Author.class, id); log_.info("got author author:" + author2); got author author:id=51, fn=thing, ln=one, subject=children, pdate=Fri Sep 15 11:54:15 EDT 2006 Returns an instance of the class associated with the specified primary key value –relationships are instantiated according to lazy-loading policies Returns null if primary key not found Uses generics, so no casting is necessary Ids can be autoboxed without a manual wrapper can be called outside the scope of a transaction will be attached to open persistence context –second find() will return same object

Enterprise Java v120215Java Persistence: EntityManager27 getReference() Author author2=null; author2 = em.getReference(Author.class, id); log_.info("got author author:" + author2); Similar to find() –Returns an instance of the class associated with the specified primary key value no guarantee that object state initialized –Throws EntityNotFoundException if primary key not found

Enterprise Java v120215Java Persistence: EntityManager28 createQuery() 5 createQuery() methods Query createQuery(String ejbqlString); Query createNamedQuery(String name); Query createNativeQuery(String sqlString); Query createNativeQuery(String sqlString, Class resultClass); Query createNativeQuery(String sqlString, String resultSetMap); example usage Query query = em.createQuery("from jpaAuthor where id=" + id); Author author = (Author)query.getSingleResult(); use EJB-QL and native (SQL) query languages similar to find/getReference() –returned objects attached to open persistence context

Enterprise Java v120215Java Persistence: EntityManager29 updating entities Updates to managed entities automatically get propagated to database according to flush policy public Author update(Author author) { Author dbAuthor = em.find(Author.class,author.getId()); dbAuthor.setFirstName(author.getFirstName()); dbAuthor.setLastName(author.getLastName()); dbAuthor.setSubject(author.getSubject()); dbAuthor.setPublishDate(author.getPublishDate()); return dbAuthor; } –Note that if author passed in was already managed... the changes have already been queued the dbAuthor returned from the find() will be the same object as author the sets are unnecessarily changing the values of the Author to their current values

Enterprise Java v120215Java Persistence: EntityManager30 merge() merges state changes to detached objects back into persistent storage public Author updateByMerge(Author author) { Author managedAuthor = em.merge(author); return managedAuthor; } Original is left detached Object returned is managed Returned object is added to persistence if did not already exist Updates are made to existing object if already exists

Enterprise Java v120215Java Persistence: EntityManager31 remove() public void remove(Author author) { em.remove(author); } –removes object from database physically performed in database according to flush policy cascades to related objects according to cascade policy –object will be detached

Enterprise Java v120215Java Persistence: EntityManager32 refresh() and contains() refresh() Author author = em.find(Author.class, id); em.refresh(author); –used to make sure entity is in sync with database cascades to related entities depending on cascade policy –entity must be currently managed by entity manager instance contains() if (em.contains(author)) {... } –used to test if instance is being managed by entity manager instance

Enterprise Java v120215Java Persistence: EntityManager33 clear() and flush() clear() –detaches all entities –does not commit queued changes call flush() prior to clear() flush() –changes not synchronized with database until entity manager flushed persist(), merge(), remove() –occurs automatically before executing correlated queries –permits query to reflect changes in persistence context transaction commit –not impacted by primary key finders find(), getReference() –FlushMode AUTO – default and most sane COMMIT – an optimization to only flush and end of transaction. May limit amount of database locking that occurs

Enterprise Java v120215Java Persistence: EntityManager34 lock() and getDelegate() lock() –provides a pessimistic write lock for entity –will be covered with later during transaction topics getDelegate() –returns vendor object that implements EntityManager interface –used to expose vendor-specific extension API

Enterprise Java v120215Java Persistence: EntityManager35 EntityTransactions Only available for Entity Managers with an extended persistence context –Transaction-scoped persistence contexts are only available with containers that support JTA transactions –Extended persistence contexts generally pertain to Java SE applications using javax.persistence.Persistence class to get EntityManagerFactory transaction-like API for managing transactions within the single resource transaction context obtained from EntityManager javax.persistence.EntityTransaction tx = em.getTransaction(); tx.begin() tx.commit() tx.isActive() tx.rollback()

Enterprise Java v120215Java Persistence: EntityManager36 Example private EntityManager em =... public void testQuery() throws Exception { Author author = new Author(); author.setFirstName("test"); author.setLastName("Query"); author.setSubject("testing"); author.setPublishDate(new Date()); em.persist(author); //need to associate em with Tx to allow query to see entity in DB try { em.getTransaction().begin(); //note that the persist does not have to be within the tx em.getTransaction().commit(); } catch (Exception ex) { em.getTransaction().rollback(); fail("" + ex); } Author author2 = null; Query query = em.createQuery( "from jpaAuthor where id=" + author.getId()); author2 = (Author)query.getSingleResult(); assertNotNull(author2); assertEquals(author.getFirstName(), author2.getFirstName()); assertEquals(author.getLastName(), author2.getLastName()); assertEquals(author.getSubject(), author2.getSubject()); assertEquals(author.getPublishDate(), author2.getPublishDate()); }

Enterprise Java v120215Java Persistence: EntityManager37 Implementation/Testing Details

Enterprise Java v120215Java Persistence: EntityManager38 Add JPA Dependencies JPA API –JPA 1.0 javax.persistence persistence-api 1.0 provided –JPA 2.0 org.hibernate.javax.persistence hibernate-jpa-2.0-api Final provided –Both available from the JBoss Maven/Nexus repository

Enterprise Java v120215Java Persistence: EntityManager39 Add Provider Dependencies Identify the JBoss repository jboss-nexus JBoss Nexus Repository Add hibernate-entitymanager –primary dependency we need to pull in hibernate –maven will pull in many other dependencies based on hibernate- entitymanager's pom.xml org.hibernate hibernate-entitymanager Final test

Enterprise Java v120215Java Persistence: EntityManager40 Add Dependency Overrides We have to pull in a library that will allow hibernate to use log4j org.slf4j slf4j-log4j test Otherwise, we’ll get the following error Tests run: 12, Failures: 0, Errors: 12, Skipped: 0, Time elapsed: sec <<< FAILURE! testCreate(ejava.examples.dao.jpa.JPAAuthorDAODemo) Time elapsed: sec <<< ERROR! java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder at org.slf4j.LoggerFactory. (LoggerFactory.java:60)

Enterprise Java v120215Java Persistence: EntityManager41 Add Standard DB Properties Identical to what was needed for JDBC hsql jdbcdb hsql org.hsqldb.jdbcDriver jdbc:hsqldb:hsql://localhost:9001 sa org.hibernate.dialect.HSQLDialect hsqldb test

Enterprise Java v120215Java Persistence: EntityManager42 Design Mechanism to Pass DB Properties Compile-time property filtering –default values end up in.jar file; not ideal –much less work; good for prototypes –demonstrated here within JPA examples Runtime property passing –more realistic –more work could delegate details to a Test Utility Class –demonstrated in JDBC examples

Enterprise Java v120215Java Persistence: EntityManager43 src/main/resources/META-INF/persistence.xml org.hibernate.ejb.HibernatePersistence <property name="hibernate.dialect" value="${hibernate.dialect}"/> <property name="hibernate.connection.url" value="${jdbc.url}"/> <property name="hibernate.connection.driver_class" value="${jdbc.driver}"/> <property name="hibernate.connection.password" value="${jdbc.password}"/> <property name="hibernate.connection.username" value="${jdbc.user}"/> <!–- used in special cases -->

Enterprise Java v120215Java Persistence: EntityManager44 src/test/resources/hibernate.properties hibernate.dialecte=${hibernate.dialect} hibernate.connection.url=${jdbc.url} hibernate.connection.driver_class=${jdbc.driver} hibernate.connection.password=${jdbc.password} hibernate.connection.username=${jdbc.user} #only use auto create for quick prototypes #hibernate.hbm2ddl.auto=create hibernate.show_sql=true hibernate.format_sql=true # eliminate batching to help debug DB errors #hibernate.jdbc.batch_size=0

Enterprise Java v120215Java Persistence: EntityManager45 Add Resource Filtering Spec to pom.xml This overrides the default behavior of resource handling <!--tell the resource plugin to perform filtering on resources to fill in JDBC URL, etc. --> src/main/resources true <!-- if $vars in src/test/resources branch src/test/resources true...

Enterprise Java v120215Java Persistence: EntityManager46 target/classes/META-INF/persistence.xml org.hibernate.ejb.HibernatePersistence <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.connection.url" value="jdbc:hsqldb:hsql://localhost:9001"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.password" value=""/> <property name="hibernate.connection.username" value="sa"/>

Enterprise Java v120215Java Persistence: EntityManager47 target/test-classes/hibernate.properties hibernate.dialecte=org.hibernate.dialect.HSQLDialect hibernate.connection.url=jdbc:hsqldb:hsql://localhost:9001 hibernate.connection.driver_class=org.hsqldb.jdbcDriver hibernate.connection.password= hibernate.connection.username=sa hibernate.hbm2ddl.auto=create hibernate.show_sql=false #hibernate.format_sql=true #hibernate.jdbc.batch_size=0

Enterprise Java v120215Java Persistence: EntityManager48 Setup JUnit TestCase import static org.junit.Assert.*; import org.junit.*; public class JPAAccountDAOTest public static void setUpClass() throws Exception { … public void setUp() throws Exception { … public void tearDown() throws Exception { … public static void tearDownClass() throws Exception { … public void testJPACreate() throws Exception { … public void testXXX() throws Exception { … }

Enterprise Java v120215Java Persistence: EntityManager49 Setup EntityManager within TestCase public class JPAAccountDAOTest { private static Log log = LogFactory.getLog(JPAAccountDAO.class); private EntityManagerFactory emf; private EntityManager em; private AccountDAO public void setUp() throws Exception { emf = Persistence.createEntityManagerFactory("eSalesBO"); em = emf.createEntityManager(); dao = new JPAAccountDAO(); ((JPAAccountDAO)dao).setEntityManager(em); cleanup(); //get env to a known state before starting tests //may want in its own transaction em.getTransaction().begin(); //optional //have transaction started prior to test }

Enterprise Java v120215Java Persistence: EntityManager50 Teardown EntityManager within public void tearDown() throws Exception { if (em != null) { EntityTransaction tx = em.getTransaction(); if (tx.isActive()) { if (tx.getRollbackOnly()) { tx.rollback(); } else { tx.commit(); } } em.close(); } if (emf != null) { emf.close(); }

Enterprise Java v120215Java Persistence: EntityManager51 (optional!)Potential Utiltity Class package ejava.examples.dao.jpa; import java.util.HashMap; import java.util.Map; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; public class JPAUtil { private static final Map factories = new HashMap (); public static EntityManagerFactory getEntityManagerFactory(String puName) { EntityManagerFactory emf = factories.get(puName); if (emf == null) { synchronized(factories) { emf = factories.get(puName); if (emf == null) { emf = Persistence.createEntityManagerFactory(puName); factories.put(puName, emf); } return emf; } public static void close() { synchronized(factories) { for(String puName : factories.keySet()) { factories.get(puName).close(); } factories.clear(); }

Enterprise Java v120215Java Persistence: EntityManager52 (optional!) Junit Test Suites Supports the one-time startup or shutdown of components public class AllTest extends TestCase { private static Log log_ = LogFactory.getLog(AllTest.class); public static Test suite() { TestSuite tests = new TestSuite(); tests.addTestSuite(JPAAuthorDAODemo.class); tests.addTestSuite(JPANoDAODemo.class); TestSetup wrapper = new TestSetup(tests) { //one-time setup public void setUp() throws Exception { } //one-time tear down public void tearDown() throws Exception { JPAUtil.close(); } }; return wrapper; }

Enterprise Java v120215Java Persistence: EntityManager53 JUnit public void testJPACreate() throws Exception { log.info("*** testJPACreate ***"); … Account account = new Account(userId); account.setFirstName(firstName); account.getAddresses().add(new Address(0, "Shipping", "Laurel")); account.getAddresses().add(new Address(0, "Billing", "Columbia")); log.debug("instantiated Account:" + account); dao.createAccount(account); log.debug("dao created Account:" + account); … Account account2 = em.find(Account.class, userId); assertNotNull(account2); assertEquals("unexpected first name", firstName, account2.getFirstName()); assertEquals("unexpected number of addresses", account.getAddresses().size(), account2.getAddresses().size());

Enterprise Java v120215Java Persistence: EntityManager54 Summary Java Persistence API –Java standard for mapping objects to relational databases –currently part of Java EE; will be in Java SE 6 –eliminates the need or desire for EJB 2.1 entity Entity –a POJO –operates both detached and managed by OR Mapping Persistence Unit –a set of entity classes mapped to the database schema Persistence Context –a set of objects that managed –Transaction-scoped or Extended javax.persistence.EntityManager –manages objects in a persistence context javax.persistence.EntityManagerFactory –used to create individual entity managers javax.persistence.Persistence –used to bootstrap EntityManagerFactory into Java SE applications

Enterprise Java v120215Java Persistence: EntityManager55 References “Enterprise JavaBeans 3.0, 5 th Edition”; Burke & Monsen-Haefel; ISBN X; O'Reilly