Persistence context of the container-managed entitymanager

Source: Internet
Author: User

1. In the container, entitymanager is managed by the container, and its transactions are managed by JTA. When entitymanager is called, the container first checks whether it is in the persistent context.

If yes, entitymanager uses the persistent context and associated transactions;

If no persistence context is associated, a new persistence context is created and the entitymanager is associated with the transaction.

 

2. The persistence context of the entitymanager managed by the container has two scopes:

(1) Transaction type

By default, stateless session beans are all in transaction units.

By default, Session Bean uses a persistent context in the unit of a transaction.

By default, a method in bean is a transaction.. ----- A method is the scope of action of a persistent context.

A persistence context is established in a general transaction method. When a transaction ends, the persistence context ends.

(2) extended type

The stateless Session Bean creates a new persistent context each time it is called. In the stateful Session Bean, the client needs to save the customer information, even if multiple calls are required.

In the same persistent context, set the persistence context type to extended.

 
@ Persistencecontext (Type = persistencecontexttype. Extended)PrivateEntitymanager em;PrivateCustomer customer;Public VoidInit (integer customerid) {customer = em. Find (customer.Class, Customerid );}Public VoidPlaceorder (integer customerid, order ){// The same persistent context as the init method. All methods that can be directly used by the customerCustomer. getorders (). Add (order); em. Merge (customer );}
 
 
 
Bytes ------------------------------------------------------------------------------------------------------------------------
 
Configure persistence. xml
<? XML version = "1.0" encoding = "UTF-8" ?>  <  Persistence   Xmlns = Http://java.sun.com/xml/ns/persistence"      Xmlns : Xsi = Http://www.w3.org/2001/XMLSchema-instance"      Xsi : Schemalocation = Http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"  Version = "1.0"  >      <  Persistence - Unit   Name = "Bookstorepu"   Transaction - Type = "JTA"  >    <  JTA - Data -Source  > Java:/JPA </  JTA -Data-Source >    <  Properties  >    <  Property   Name = "Hibernate. dialect"   Value = "Org. hibernate. dialect. mysqldialect"  />   <  Property   Name = "Hibernate. hbm2ddl. Auto"   Value = "Create-drop"  />    <  Property   Name = "Hibernate. show_ SQL"   Value = "True"  />    <  Property  Name = "Hibernate. format_ SQL"   Value = "True"  />    </  Properties  >  </  Persistence -Unit >    </  Persistence  > 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.