KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > persistence > JPersistenceContext


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JPersistenceContext.java 326 2006-04-14 08:20:59Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.persistence;
27
28 import javax.persistence.EntityManager;
29 import javax.persistence.EntityManagerFactory;
30
31 import org.objectweb.easybeans.persistence.xml.JPersistenceUnitInfo;
32
33 /**
34  * This class manages persistence contexts associated to a persistence unit.
35  * @author Florent Benoit
36  */

37 public class JPersistenceContext {
38
39     /**
40      * Persistence unit info used by this persistence context.
41      */

42     private JPersistenceUnitInfo jPersistenceUnitInfo;
43
44     /**
45      * EntityManager factory.
46      */

47     private EntityManagerFactory entityManagerFactory = null;
48
49     /**
50      * Tx entity manager handler.
51      */

52     private TxEntityManagerHandler txEntityManagerHandler = null;
53
54     /**
55      * Tx entity manager.
56      */

57     private TxEntityManager txEntityManager = null;
58
59     /**
60      * Build a new persistence context based on a given persistence-unit info.
61      * @param jPersistenceUnitInfo information on the persistence unit.
62      */

63     public JPersistenceContext(final JPersistenceUnitInfo jPersistenceUnitInfo) {
64         this.jPersistenceUnitInfo = jPersistenceUnitInfo;
65         init();
66     }
67
68     /**
69      * Initialize entity manager (and some factoriese) used by Java EE components.
70      */

71     private void init() {
72         this.entityManagerFactory = jPersistenceUnitInfo.getPersistenceProvider().createContainerEntityManagerFactory(
73                 jPersistenceUnitInfo, null);
74         this.txEntityManagerHandler = new TxEntityManagerHandler(entityManagerFactory);
75         this.txEntityManager = new TxEntityManager(txEntityManagerHandler);
76     }
77
78     /**
79      * Gets the EntityManager used for Transaction-Scoped.
80      * @return the EntityManager used for Transaction-Scoped
81      */

82     public EntityManager getTxEntityManager() {
83         return txEntityManager;
84     }
85
86     /**
87      * Gets the EntityManager factory.
88      * @return the EntityManager factory
89      */

90     public EntityManagerFactory getEntityManagerFactory() {
91         return entityManagerFactory;
92     }
93
94     /**
95      * Sets the current entity manager (used when to transaction is active).
96      */

97     public void addCurrent() {
98         txEntityManagerHandler.addCurrent();
99     }
100
101
102     /**
103      * Sets back to the previous entity manager and close the current entity manager.
104      */

105     public void closeCurrentAndReturnToPrevious() {
106         txEntityManagerHandler.closeCurrentAndReturnToPrevious();
107     }
108
109 }
110
Popular Tags