001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb3.entity;
023:
024: import org.hibernate.Session;
025: import org.hibernate.ejb.HibernateEntityManager;
026: import org.jboss.ejb3.PersistenceUnitRegistry;
027:
028: import javax.persistence.*;
029: import java.io.Externalizable;
030: import java.io.IOException;
031: import java.io.ObjectInput;
032: import java.io.ObjectOutput;
033:
034: /**
035: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
036: * @version $Revision: 62768 $
037: */
038: public class TransactionScopedEntityManager implements
039: HibernateEntityManager, HibernateSession, Externalizable {
040: private static final long serialVersionUID = 4260828563883650376L;
041:
042: private transient ManagedEntityManagerFactory factory;
043:
044: public Session getSession() {
045: return getHibernateSession();
046: }
047:
048: public Session getHibernateSession() {
049: EntityManager em = factory.getTransactionScopedEntityManager();
050: if (em instanceof HibernateEntityManager) {
051: return ((HibernateEntityManager) em).getSession();
052: }
053: throw new RuntimeException(
054: "ILLEGAL ACTION: Not a Hibernate pe"
055: + "rsistence provider");
056: }
057:
058: public TransactionScopedEntityManager(
059: ManagedEntityManagerFactory factory) {
060: if (factory == null)
061: throw new NullPointerException("factory must not be null");
062: this .factory = factory;
063: }
064:
065: public TransactionScopedEntityManager() {
066: }
067:
068: public void writeExternal(ObjectOutput out) throws IOException {
069: out.writeUTF(factory.getKernelName());
070: }
071:
072: public void readExternal(ObjectInput in) throws IOException,
073: ClassNotFoundException {
074: String kernelName = in.readUTF();
075: PersistenceUnitDeployment deployment = PersistenceUnitRegistry
076: .getPersistenceUnit(kernelName);
077: if (deployment == null)
078: throw new IOException(
079: "Unable to find persistence unit in registry: "
080: + kernelName);
081: factory = deployment.getManagedFactory();
082: }
083:
084: public Object getDelegate() {
085: return factory.getTransactionScopedEntityManager()
086: .getDelegate();
087: }
088:
089: public void joinTransaction() {
090: factory.verifyInTx();
091: factory.getTransactionScopedEntityManager().joinTransaction();
092: }
093:
094: public void clear() {
095: factory.getTransactionScopedEntityManager().clear();
096: }
097:
098: public FlushModeType getFlushMode() {
099: return factory.getTransactionScopedEntityManager()
100: .getFlushMode();
101: }
102:
103: public void lock(Object entity, LockModeType lockMode) {
104: factory.verifyInTx();
105: factory.getTransactionScopedEntityManager().lock(entity,
106: lockMode);
107: }
108:
109: public <T> T getReference(Class<T> entityClass, Object primaryKey) {
110: EntityManager em = factory.getTransactionScopedEntityManager();
111: if (!factory.isInTx())
112: em.clear(); // em will be closed by interceptor
113: try {
114: return em.getReference(entityClass, primaryKey);
115: } finally {
116: if (!factory.isInTx())
117: em.clear(); // em will be closed by interceptor
118: }
119: }
120:
121: public void setFlushMode(FlushModeType flushMode) {
122: factory.getTransactionScopedEntityManager().setFlushMode(
123: flushMode);
124: }
125:
126: public Query createQuery(String ejbqlString) {
127: EntityManager em = factory.getTransactionScopedEntityManager();
128: if (!factory.isInTx())
129: em.clear(); // em will be closed by interceptor
130: return em.createQuery(ejbqlString);
131: }
132:
133: public Query createNamedQuery(String name) {
134: EntityManager em = factory.getTransactionScopedEntityManager();
135: if (!factory.isInTx())
136: em.clear(); // em will be closed by interceptor
137: return em.createNamedQuery(name);
138: }
139:
140: public Query createNativeQuery(String sqlString) {
141: EntityManager em = factory.getTransactionScopedEntityManager();
142: if (!factory.isInTx())
143: em.clear(); // em will be closed by interceptor
144: return em.createNativeQuery(sqlString);
145: }
146:
147: public Query createNativeQuery(String sqlString, Class resultClass) {
148: EntityManager em = factory.getTransactionScopedEntityManager();
149: if (!factory.isInTx())
150: em.clear(); // em will be closed by interceptor
151: return em.createNativeQuery(sqlString, resultClass);
152: }
153:
154: public Query createNativeQuery(String sqlString,
155: String resultSetMapping) {
156: EntityManager em = factory.getTransactionScopedEntityManager();
157: if (!factory.isInTx())
158: em.clear(); // em will be closed by interceptor
159: return em.createNativeQuery(sqlString, resultSetMapping);
160: }
161:
162: public <A> A find(Class<A> entityClass, Object primaryKey) {
163: EntityManager em = factory.getTransactionScopedEntityManager();
164: if (!factory.isInTx())
165: em.clear(); // em will be closed by interceptor
166: try {
167: return em.find(entityClass, primaryKey);
168: } finally {
169: if (!factory.isInTx())
170: em.clear(); // em will be closed by interceptor
171: }
172: }
173:
174: public void persist(Object entity) {
175: factory.verifyInTx();
176: factory.getTransactionScopedEntityManager().persist(entity);
177: }
178:
179: public <A> A merge(A entity) {
180: factory.verifyInTx();
181: return (A) factory.getTransactionScopedEntityManager().merge(
182: entity);
183: }
184:
185: public void remove(Object entity) {
186: factory.verifyInTx();
187: factory.getTransactionScopedEntityManager().remove(entity);
188: }
189:
190: public void refresh(Object entity) {
191: factory.verifyInTx();
192: factory.getTransactionScopedEntityManager().refresh(entity);
193: }
194:
195: public boolean contains(Object entity) {
196: return factory.getTransactionScopedEntityManager().contains(
197: entity);
198: }
199:
200: public void flush() {
201: factory.verifyInTx();
202: factory.getTransactionScopedEntityManager().flush();
203: }
204:
205: public void close() {
206: throw new IllegalStateException(
207: "Illegal to call this method from injected, managed EntityManager");
208: }
209:
210: public boolean isOpen() {
211: throw new IllegalStateException(
212: "Illegal to call this method from injected, managed EntityManager");
213: }
214:
215: public EntityTransaction getTransaction() {
216: throw new IllegalStateException(
217: "Illegal to call this method from injected, managed EntityManager");
218: }
219:
220: }
|