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.

Slides:



Advertisements
Similar presentations
J0 1 Marco Ronchetti - Basi di Dati Web e Distribuite – Laurea Specialistica in Informatica – Università di Trento.
Advertisements

11 Copyright © 2005, Oracle. All rights reserved. Creating the Business Tier: Enterprise JavaBeans.
Road Map Introduction to object oriented programming. Classes
JBoss Seam: Contextual Components Jason Bechtel
Entities and Persistence. Entity Beans Topics to be Covered: Entities are POJOs Managed/Unmanaged Entities Persistence Unit EntityManager Basic Relational.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 10 Classes Continued
Session-02.
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
Java Persistence API Maciej Adamiak. Agenda -Entity, -Entity Operations, -Query Language.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Advance Computer Programming Java Database Connectivity (JDBC) – In order to connect a Java application to a database, you need to use a JDBC driver. –
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
1 Lecture 17 George Koutsogiannakis/SUMMER 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
1 The JNDI ENC and Injection Every EJB container that is deployed in an application server has its own personal internal registry called the Enterprise.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Introduction to J2EE Architecture Portions by Kunal Mehta.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Message-Driven Beans and EJB Security Lesson 4B / Slide 1 of 37 J2EE Server Components Objectives In this lesson, you will learn about: Identify features.
Java Server Pages A JSP page is a text-based document that contains two types of text: static template data, which can be expressed in any text-based format,
JBoss Seam Presented by Andy Nguyen Truc Pham. What is JBoss Seam? Created by Gavin King Created by Gavin King A lightweight framework for Java EE 5.0.
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 Java v120215Java Persistence: EntityManager1.
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.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
MCS 270 Spring 2014 Object-Oriented Software Development.
IBM TSpaces Lab 3 Transactions Event Registration.
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Session Beans Based on: Patel, Brose, Silverman, Mastering Enterprise JavaBeans 3.0.
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
Introduction to EJB. What is an EJB ?  An enterprise java bean is a server-side component that encapsulates the business logic of an application. By.
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.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
JPA The New Enterprise Persistence Standard Mike Keith
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
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,
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
OOPSLA Lab1 Chapter 7 Java Binding Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Enterprise Java Beans. Contents  Understanding EJBs  Practice Section.
Callbacks and Interceptors. Contents  Session Beans Life Cycle  Interceptors.
Chengyu Sun California State University, Los Angeles
Java Persistence Architecture (JPA)
Advanced Java Programming
Introduction to J2EE Architecture
J2EE Application Development
JAsCo an Aspect-Oriented approach tailored for
Knowledge Byte In this section, you will learn about:
Topics Persistence API for JAVA EE5 Java Transaction API Entity class.
Chengyu Sun California State University, Los Angeles
Knowledge Byte In this section, you will learn about:
Objectives In this lesson, you will learn about:
Presentation transcript:

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 abstraction on top of JDBC so that your code can be isolated from database, vendor-specific peculiarities and optimizations.

2 Persistence: EntityManager In the new Java Persistence specification, the EntityManager is the central service for all persistence actions. The EntityManager is tightly integrated with Java EE and EJB but is not limited to this environment; it can be used in plain Java programs. You can use Java Persistence outside of an application server and in plain Java SE programs.

3 Entities Are POJOs Let's look at a simple example of a Customer entity: import public class Customer { private int id; private public int getId( ) { return id; }

4 Entities Are POJOs public void setId(int id) { this.id = id; } String getName( ) { return name; } public void setName(String name) { this.name = name; }

5 Entities Are POJOs Calling the new operator does not magically interact with some underlying service to create the Customer class in the database: Customer cust = new Customer( ); cust.setName("Bill"); Allocated instances of the Customer class remain POJOs until you ask the EntityManager to create the entity in the database.

6 Managed Versus Unmanaged Entities An entity bean instance is either managed (a.k.a. attached) by an entity manager or unmanaged (a.k.a. detached). When an entity is attached to an EntityManager, the manager tracks state changes to the entity and synchronizes those changes to the database whenever the entity manager decides to flush its state. When an entity is detached, it is unmanaged. Any state changes to an entity that is detached are not tracked by the entity manager.

7 Persistence Context A persistence context is a set of managed entity object instances. Persistence contexts are managed by an entity manager. Once a persistence context is closed, all managed entity object instances become detached and are no longer managed.

8 Persistence Context Once an object is detached from a persistence context, it can no longer be managed by an entity manager, and any state changes to this object instance will not be synchronized with the database. There are two types of persistence contexts: transaction-scoped and extended persistence contexts.

9 Transaction-scoped persistence context Persistence contexts may live as long as a transaction and be closed when a transaction completes. This is called a transaction-scoped persistence context contexts can be transaction- scoped. Only application server managed persistence contexts can be transaction- scoped.

10 Transaction-scoped persistence context In other words, only EntityManager instances injected with annotation or its XML equivalent may be (unitName="titan") EntityManager public Customer someMethod( ) { Customer cust = entityManager.find(Customer.class, 1); cust.setName("new name"); return cust;}

11 Extended persistence context Persistence contexts may also be configured to live longer than a transaction. This is called an extended persistence context Here's some small pseudocode to illustrate this concept: Customer cust = null; transaction.begin( ); // start transaction 1 cust = extendedEntityManager.find(Customer.class, 1); transaction.commit( ); // transaction 1 ends transaction.begin( ); // start transaction 2

12 Extended persistence context cust.setName("Bill"); extendedEntityManager.flush( ); transaction.commit( ); // cust instance remains managed and changes are flushed In this example, a local variable, cust, is initialized by calling the find( ) method in transaction 1.

13 Extended persistence context Unlike a transaction-scoped persistence context, the Customer instance pointed to by this local variable remains managed. This is because persistence context stays alive past the completion of transaction 1. In transaction 2, the customer is updated and the changes are flushed to the database.

14 Detached entities Entity instances become unmanaged and detached when a transaction scope or extended persistence context ends. An interesting side effect is that detached entities can be serialized and sent across the network to a remote client. The client can make changes remotely to these serialized object instances and send them back to the server to be merged back and synchronized with the database.

15 Packaging a Persistence Unit A persistence unit is defined in a persistence.xml file. This file is a required deployment descriptor for the Java Persistence specification. A persistence.xml file can define one or more persistence units. This file is located in the META-INF directory of: A plain JAR file within the classpath of a regular Java SE program.

16 Packaging a Persistence Unit An EJB-JAR file. A persistence unit can be included with an EJB deployment. A JAR file in the WEB-INF/lib directory in a web archive file (.war). A JAR file in the root of an enterprise archive (.ear). A JAR file in the EAR lib directory.

17 Packaging a Persistence Unit titan-persistence.jar

18 Packaging a Persistence Unit The set of classes that belong to the persistence unit can be specified, or you can opt for the persistence provider to scan the JAR file automatically for the set of classes to deploy as entities. The set of classes that belong to the persistence unit can be specified, or you can opt for the persistence provider to scan the JAR file automatically for the set of classes to deploy as entities. Each persistence unit is tied to one and only one data source. In Java SE environments, vendor- specific configuration must be used to define and configure these data sources. Each persistence unit is tied to one and only one data source. In Java SE environments, vendor- specific configuration must be used to define and configure these data sources. In Java EE environments, specific XML elements define this association. In Java EE environments, specific XML elements define this association.

19 Packaging a Persistence Unit The root of the persistence.xml XML schema is the element, which contains one or more elements. Each has two attributes: name (required) and transaction-type (optional). The subelements of are (optional), (optional), (optional), (optional), (optional), (optional), (optional), (optional), and (optional).

20 Packaging a Persistence Unit Here's an example of a persistence.xml file: java:/OracleDS update

21 The Persistence Unit Class Set Scanning JAR files is guaranteed to work in Java EE environments but is not portable in Java SE applications. In theory, it may not be possible to determine the set of JAR files that must be scanned. In practice, however, this is not the case. All major vendors in the EJB 3.0 Expert Group were polled unofficially and said they would have no problems supporting this feature in SE.

22 The Persistence Unit Class Set Whether you do or do not rely on a JAR scan, classes can be listed explicitly with the element: java:/OracleDS com.titan.domain.Cabin com.titan.domain.Customer update

23 The Persistence Unit Class Set The Cabin and Customer classes listed within the elements are added to the persistence unit set along with any other classes scanned in the persistence unit's archive. The final set of classes is determined by a union of all of the following metadata:

24 The Persistence Unit Class Set Classes annotated in the persistence.xml file's JAR file (unless is specified) Classes annotated that are contained within any JARs listed with any elements Classes mapped in the META-INF/orm.xml file if it exists Classes mapped in any XML files referenced with the element Classes listed with any elements

25 Obtaining an EntityManager Now that you have packaged and deployed your persistence units, you need to obtain access to an EntityManager so that you can persist, update, remove, and query your entity beans within your databases. In Java SE, entity managers are created using a javax.persistence.EntityManagerFactory.

26 Obtaining an EntityManager Although you can use the factory interface in Java EE, this platform provides some additional features that make it easier and less verbose to manage entity manager instances. Although you can use the factory interface in Java EE, this platform provides some additional features that make it easier and less verbose to manage entity manager instances.

27 EntityManagerFactory EntityManagers may be created or obtained from an EntityManagerFactory. In a Java SE application, you must use an EntityManagerFactory to create instances of an EntityManager. Using the factory isn't a requirement in Java EE. package javax.persistence; public interface EntityManagerFactory { EntityManager createEntityManager( ); EntityManager createEntityManager(java.util.Map map); void close( ); boolean isOpen( ); }

28 Getting an EntityManagerFactory in Java SE In Java SE, the javax.persistence.Persistence class is responsible for bootstrapping an EntityManagerFactory : In Java SE, the javax.persistence.Persistence class is responsible for bootstrapping an EntityManagerFactory : public class Persistence { public static EntityManagerFactory createEntityManagerFactory( String unitName ); public static EntityManagerFactory createEntityManagerFactory( String unitName ); public static EntityManagerFactory public static EntityManagerFactory createEntityManagerFactory( String unitName, createEntityManagerFactory( String unitName, java.util.Map properties );

29 Getting an EntityManagerFactory in Java SE The javax.persistence.Persistence class looks for persistence.xml deployment descriptors within your Java classpath. The javax.persistence.Persistence class looks for persistence.xml deployment descriptors within your Java classpath. The unitName parameter you pass in will allow the Persistence implementation to locate an EntityManagerFactory that matches the given name. The unitName parameter you pass in will allow the Persistence implementation to locate an EntityManagerFactory that matches the given name.

30 Getting an EntityManagerFactory in Java SE Additionally, you can override or add any vendor-specific properties defined in the element of the persistence.xml file by passing in a java.util.Map as a second parameter: EntityManagerFactory factory = Persistence.createEntityManagerFactory("CRM");... factory.close( );

31 Getting an EntityManagerFactory in Java SE In Java SE, it is recommended that you close( ) the EntityManagerFactory. This frees up any resources that are being held by the factory.

32 Getting an EntityManagerFactory in Java EE In Java EE, it is a bit easier to get an EntityManagerFactory. It can be injected directly into a field or setter method of your EJBs using annotation : package FIELD, PersistenceUnit { String name( ) default ""; String unitName( ) default ""; }

33 Getting an EntityManagerFactory in Java EE The unitName( ) is the identity of the PersistenceUnit. The unitName( ) is the identity of the PersistenceUnit. When the PersistenceUnit is used, it not only injects the EntityManagerFactory, it also registers a reference to it within the JNDI ENC of the EJB. When the PersistenceUnit is used, it not only injects the EntityManagerFactory, it also registers a reference to it within the JNDI ENC of the EJB. The EJB container is responsible for noticing annotation and injecting the correct factory: The EJB container is responsible for noticing annotation and injecting the correct factory:

34 Getting an EntityManagerFactory in Java EE import javax.persistence.*; import public MyBean implements MyBusinessInterface private EntityManagerFactory factory; private EntityManagerFactory public void setFactory2(EntityManagerFactory f) { this.factory2 = f; }

35 Getting an EntityManagerFactory in Java EE When an instance of the stateless session bean is created, the EJB container sets the factory field to the persistence unit identified by "CRM". When an instance of the stateless session bean is created, the EJB container sets the factory field to the persistence unit identified by "CRM". It also calls the setFactory2( ) method with the "CUSTDB" persistence unit. It also calls the setFactory2( ) method with the "CUSTDB" persistence unit.

36 Obtaining a Persistence Context A persistence context can be created by calling the EntityManagerFactory.createEnti tyManager( ) method. A persistence context can be created by calling the EntityManagerFactory.createEnti tyManager( ) method. The returned EntityManager instance represents an extended persistence context. The returned EntityManager instance represents an extended persistence context.

37 Obtaining a Persistence Context If the EntityManagerFactory is JTA-enabled, then you have to explicitly enlist the EntityManager instance within a transaction by calling the EntityManager.joinTransaction( ) method. If you do not enlist the EntityManager within the JTA transaction, then changes you make to your entities are not synchronized with the database.

38 Interacting with an EntityManager The EntityManager API has methods to insert and remove entities from a database as well as merge updates from detached entity instances. There is also a rich query API that you can access by creating query objects from certain EntityManager methods: package javax.persistence; public interface EntityManager { public void persist(Object entity); public T find(Class entityClass, Object primaryKey); public T getReference(Class entityClass, Object primaryKey); public T merge(T entity);

39 Interacting with an EntityManager public void remove(Object entity); public void lock(Object entity, LockModeType lockMode); public void lock(Object entity, LockModeType lockMode); public void refresh(Object entity); public void refresh(Object entity); public boolean contains(Object entity); public boolean contains(Object entity); public void clear( ); public void clear( ); public void joinTransaction( ); public void joinTransaction( ); public void flush( ); public void flush( ); public FlushModeType getFlushMode( ); public FlushModeType getFlushMode( ); public void setFlushMode(FlushModeType type); public void setFlushMode(FlushModeType type);

40 Interacting with an EntityManager public Query createQuery(String queryString); public Query createNamedQuery(String name); public Query createNamedQuery(String name); public Query createNativeQuery(String sqlString); public Query createNativeQuery(String sqlString); public Query createNativeQuery(String sqlString, String resultSetMapping); public Query createNativeQuery(String sqlString, String resultSetMapping); public Query createNativeQuery(String sqlString, Class resultClass); public Query createNativeQuery(String sqlString, Class resultClass); public Object getDelegate( ); public Object getDelegate( ); public void close( ); public void close( ); public boolean isOpen( ); public boolean isOpen( );}

41 Persisting Entities Persisting an entity is the act of inserting it within a database. You persist entities that have not yet been created in the database. Persisting an entity is the act of inserting it within a database. You persist entities that have not yet been created in the database. You interact with the entity manager service by calling the EntityManager.persist( ) method: You interact with the entity manager service by calling the EntityManager.persist( ) method: Custom cust = new Customer( ); cust.setName("Bill");entityManager.persist(cust);

42 Persisting Entities When this method is called, the entity manager queues the Customer for insertion into the database, and the object instance becomes managed. When the actual insertion happens depends on a few variables. If persist( ) is called within a transaction, the insert may happen immediately, or it may be queued until the end of the transaction, depending on the flush mode (described later in this chapter).

43 Persisting Entities You can always force the insertion manually within a transaction by calling the flush( ) method. You can always force the insertion manually within a transaction by calling the flush( ) method. You may call persist( ) outside of a transaction if and only if the entity manager is an EXTENDED persistence context. You may call persist( ) outside of a transaction if and only if the entity manager is an EXTENDED persistence context. When you call persist( ) outside of a transaction with an EXTENDED persistence context, the insert is queued until the persistence context is associated with a transaction. When you call persist( ) outside of a transaction with an EXTENDED persistence context, the insert is queued until the persistence context is associated with a transaction.

44 Persisting Entities If the entity has any relationships with other entities, these entities may also be created within the database if you have the appropriate cascade policies set up. If the entity has any relationships with other entities, these entities may also be created within the database if you have the appropriate cascade policies set up. The persist( ) method throws an IllegalArgumentException if its parameter is not an entity type. The persist( ) method throws an IllegalArgumentException if its parameter is not an entity type. TransactionRequiredException is thrown if this method is invoked on a transaction-scoped persistence context. TransactionRequiredException is thrown if this method is invoked on a transaction-scoped persistence context.

45 Finding Entities The entity manager provides two mechanisms for locating objects in your database. The entity manager provides two mechanisms for locating objects in your database. One way is with simple entity manager methods that locate an entity by its primary key. One way is with simple entity manager methods that locate an entity by its primary key. The other is by creating and executing queries. The other is by creating and executing queries.

46 find( ) and getReference( ) The EntityManager has two different methods that allow you to find an entity by its primary key: The EntityManager has two different methods that allow you to find an entity by its primary key: public interface EntityManager { T find(Class entityClass, Object primaryKey); T find(Class entityClass, Object primaryKey); T getReference(Class entityClass, Object primaryKey); T getReference(Class entityClass, Object primaryKey);}

47 find( ) and getReference( ) The find( ) method returns null if the entity is not found in the database. It also initializes the state based on the lazy-loading policies of each property. Customer cust = entityManager.find(Customer.class, 2); Customer cust = null; try { cust = entityManager.getReference(Customer.class, 2); } catch (EntityNotFoundException notFound) { // recovery logic }

48 find( ) and getReference( ) getReference( ) differs from find( ) in that if the entity is not found in the database, this method throws a javax.persistence.EntityNotFoundException and there is no guarantee that the entity's state will be initialized.

49 Queries Persistent objects can also be located by using EJB QL. Persistent objects can also be located by using EJB QL. Unlike EJB 2.1, there are no finder methods, and you must create a Query object by calling the EntityManager 's createQuery( ), createNamedQuery( ), or createNativeQuery( ) method: Unlike EJB 2.1, there are no finder methods, and you must create a Query object by calling the EntityManager 's createQuery( ), createNamedQuery( ), or createNativeQuery( ) method:

50 Queries public interface EntityManager { Query createQuery(String queryString); Query createQuery(String queryString); Query createNamedQuery(String name); Query createNamedQuery(String name); Query createNativeQuery(String sqlString); Query createNativeQuery(String sqlString); Query createNativeQuery(String sqlString, Class resultClass); Query createNativeQuery(String sqlString, Class resultClass); Query createNativeQuery(String sqlString, String resultSetMapping); Query createNativeQuery(String sqlString, String resultSetMapping);}

51 Updating EntityManager public void updateBedCount(int id, int newCount) { Cabin cabin = entityManager.find(Cabin.class, id); cabin.setBedCount(newCount); } In this code, the Cabin entity returned by the find( ) method is still managed by the EntityManager because an active persistence context is still associated with the transaction.

52 Updating Entities In this code, the Cabin entity returned by the find( ) method is still managed by the EntityManager because an active persistence context is still associated with the transaction. This means that you can modify the object instance and the database will be updated automatically when the EntityManager decides to flush changes from memory to your database.

53 Merging Entities The Java Persistence spec allows you to merge state changes made to a detached entity back into persistence storage using the entity manager's merge( ) method. The Java Persistence spec allows you to merge state changes made to a detached entity back into persistence storage using the entity manager's merge( ) method. Consider a remote Swing client. This client calls a method on our remote TravelAgent session bean to find a cabin in the database: Consider a remote Swing client. This client calls a method on our remote TravelAgent session bean to find a cabin in the EntityManager public Cabin findCabin(int id) { return entityManager.find(Cabin.class, id); }

54 Merging Entities The Swing client makes a few changes to the Cabin instance and sends them back to the server. The Swing client makes a few changes to the Cabin instance and sends them back to the server. Cabin cabin = travelAgent.findCabin(1); cabin.setBedCount(4);travelAgent.updateCabin(cabin); The TravelAgentBean.updateCabin( ) method takes its cabin parameter and merges it back into the current persistence context of the entity manager by calling the merge( ) operation: The TravelAgentBean.updateCabin( ) method takes its cabin parameter and merges it back into the current persistence context of the entity manager by calling the merge( ) operation:

55 Merging EntityManager public void updateCabin(Cabin cabin) { Cabin copy = entityManager.merge(cabin); Cabin copy = entityManager.merge(cabin);} The changes made by the remote Swing client will now be reflected in persistence storage when the entity manager decides to flush to the database. The changes made by the remote Swing client will now be reflected in persistence storage when the entity manager decides to flush to the database.

56 Merging Entities If the entity manager isn't already managing a Cabin instance with the same ID, a full copy of the cabin parameter is made and returned from the merge( ) method. This copy is managed by the entity manager, and any additional setter methods called on this copy will be synchronized with the database when the EntityManager decides to flush.

57 Merging Entities If the entity manager is already managing a Cabin instance with the same primary key, then the contents of the cabin parameter are copied into this managed object instance. The merge( ) operation will return this managed instance. The cabin parameter remains detached and unmanaged.

58 Removing Entities An entity can be removed from the database by calling the EntityManager.remove( ) method. The remove( ) operation does not immediately delete the cabin from the database. When the entity manager decides to flush, based on the flush rules described later in this chapter, an SQL DELETE is EntityManager public void removeCabin(int id) { Cabin cabin = entityManager.find(Cabin.class, id); entityManager.remove(cabin); }

59 refresh( ) If you are concerned that a current managed entity is not up-to-date with the database, then you can use the EntityManager.refresh( ) method. The refresh( ) method refreshes the state of the entity from the database, overwriting any changes made to that EntityManager public void removeCabin(int id) { Cabin cabin = entityManager.find(Cabin.class, id); entityManager.refresh(cabin); }

60 contains( ) and clear( ) The contains( ) method takes an entity instance as a parameter. If this particular object instance is currently being managed by the persistence context, it returns true. It throws an IllegalArgumentException if the parameter is not an entity.

61 contains( ) and clear( ) If you need to detach all managed entity instances from a persistence context, you can invoke the clear( ) method of the EntityManager. If you need to detach all managed entity instances from a persistence context, you can invoke the clear( ) method of the EntityManager. Be aware that when you call clear( ) any changes you have made to managed entities are lost. Be aware that when you call clear( ) any changes you have made to managed entities are lost. It is wise to call flush( ) before clear( ) is invoked so you don't lose your changes. It is wise to call flush( ) before clear( ) is invoked so you don't lose your changes.

62 flush( ) and FlushModeType When you call persist( ), merge( ), or remove( ), these changes are not synchronized with the database until the entity manager decides to flush. When you call persist( ), merge( ), or remove( ), these changes are not synchronized with the database until the entity manager decides to flush. You can force synchronization anytime by calling flush( ). You can force synchronization anytime by calling flush( ). By default, flushing automatically happens before a correlated query is executed (inefficient implementations may even flush before any query) and at transaction commit time. By default, flushing automatically happens before a correlated query is executed (inefficient implementations may even flush before any query) and at transaction commit time.

63 flush( ) and FlushModeType The exception to this default rule is find( ). A flush does not need to happen when find( ) or getReference( ) is called because finding by a primary key is not something that would be affected by any updates. The exception to this default rule is find( ). A flush does not need to happen when find( ) or getReference( ) is called because finding by a primary key is not something that would be affected by any updates. You can control and change this default behavior by using the javax.persistence.FlushModeType enumeration: You can control and change this default behavior by using the javax.persistence.FlushModeType enumeration: public enum FlushModeType { AUTO, AUTO, COMMIT COMMIT}

64 flush( ) and FlushModeType AUTO is the default behavior described in the preceding code snippet. AUTO is the default behavior described in the preceding code snippet. COMMIT means that changes are flushed only when the transaction commits, not before any query. COMMIT means that changes are flushed only when the transaction commits, not before any query. You can set the FlushModeType by calling the setFlushMode( ) method on the EntityManager. You can set the FlushModeType by calling the setFlushMode( ) method on the EntityManager.

65 Locking The EntityManager API supports both read and write locks. Because locking behavior is closely related to the concept of transactions, using the lock( ) method is discussed in detail in Chapter 16. The EntityManager API supports both read and write locks. Because locking behavior is closely related to the concept of transactions, using the lock( ) method is discussed in detail in Chapter 16.Chapter 16Chapter 16

66 Resource Local Transactions An entity manager's persistence context is usually managed by a JTA transaction in a Java EE environment. An entity manager's persistence context is usually managed by a JTA transaction in a Java EE environment. When running in a non-Java EE environment, JTA is not available, so the Java Persistence API specification has provided a transaction-like API through the EntityTransaction interface. When running in a non-Java EE environment, JTA is not available, so the Java Persistence API specification has provided a transaction-like API through the EntityTransaction interface. You can obtain access to an Entity-Transaction through the EntityManager.getTransaction( ) operation: You can obtain access to an Entity-Transaction through the EntityManager.getTransaction( ) operation:

67 Resource Local Transactions public interface EntityTransaction { public void begin( ); public void begin( ); public void commit( ); public void commit( ); public void rollback( ); public void rollback( ); public boolean isActive( ); public boolean isActive( );} Let's take our example from Lesson 4 and convert it to a standalone Java application using the javax.persistence.Persistence API and EntityTransaction: Let's take our example from Lesson 4 and convert it to a standalone Java application using the javax.persistence.Persistence API and EntityTransaction:

68 Resource Local Transactions import javax.persistence.*; public class StandaloneClient { public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception { EntityManagerFactory factory = EntityManagerFactory factory = Persistence.createEntityManagerFactory("titan"); Persistence.createEntityManagerFactory("titan"); EntityManager manager = factory.createEntityManager( ); EntityManager manager = factory.createEntityManager( ); try { try { createCabin(manager); createCabin(manager); Cabin cabin_2 = manager.find(Cabin.class, 1); Cabin cabin_2 = manager.find(Cabin.class, 1); System.out.println(cabin_2.getName( )); System.out.println(cabin_2.getName( )); System.out.println(cabin_2.getDeckLevel( )); System.out.println(cabin_2.getDeckLevel( )); System.out.println(cabin_2.getShipId( )); System.out.println(cabin_2.getShipId( )); System.out.println(cabin_2.getBedCount( )); System.out.println(cabin_2.getBedCount( )); } finally { } finally { manager.close( ); manager.close( ); factory.close( ); factory.close( ); } }

69 Resource Local Transactions public static void createCabin(EntityManager manager) { Cabin cabin_1 = new Cabin( ); Cabin cabin_1 = new Cabin( ); cabin_1.setId(1); cabin_1.setId(1); cabin_1.setName("Master Suite"); cabin_1.setName("Master Suite"); cabin_1.setDeckLevel(1); cabin_1.setDeckLevel(1); cabin_1.setShipId(1); cabin_1.setShipId(1); cabin_1.setBedCount(3); cabin_1.setBedCount(3); EntityTransaction transaction = manager.getTransaction( ); EntityTransaction transaction = manager.getTransaction( ); transaction.begin( ); transaction.begin( ); manager.persist(cabin_1); manager.persist(cabin_1); transaction.commit( ); transaction.commit( ); }}