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 java.io.Serializable;
025: import javax.persistence.EntityManager;
026: import javax.persistence.EntityTransaction;
027: import javax.persistence.FlushModeType;
028: import javax.persistence.LockModeType;
029: import org.hibernate.Session;
030: import org.hibernate.ejb.HibernateEntityManager;
031: import org.jboss.ejb3.stateful.StatefulBeanContext;
032:
033: /**
034: * EntityManager a managed extended persistence context.
035: *
036: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
037: */
038: public class ExtendedEntityManager implements EntityManager,
039: HibernateSession, Serializable, ExtendedPersistenceContext {
040: private static final long serialVersionUID = -2221892311301499591L;
041:
042: private String identity;
043:
044: public ExtendedEntityManager(String name) {
045: this .identity = name;
046: }
047:
048: public ExtendedEntityManager() {
049: }
050:
051: public EntityManager getPersistenceContext() {
052: StatefulBeanContext beanContext = StatefulBeanContext.currentBean
053: .get();
054: EntityManager persistenceContext = beanContext
055: .getExtendedPersistenceContext(identity);
056: if (persistenceContext == null)
057: throw new RuntimeException(
058: "Unable to determine persistenceContext: "
059: + identity
060: + " in injected SFSB: "
061: + beanContext.getContainer()
062: .getObjectName());
063: return persistenceContext;
064: }
065:
066: // delegates
067:
068: public Session getHibernateSession() {
069: if (getPersistenceContext() instanceof HibernateEntityManager) {
070: return ((HibernateEntityManager) getPersistenceContext())
071: .getSession();
072: }
073: throw new RuntimeException(
074: "ILLEGAL ACTION: Not a Hibernate persistence provider");
075: }
076:
077: public void joinTransaction() {
078: getPersistenceContext().joinTransaction();
079: }
080:
081: public void clear() {
082: getPersistenceContext().clear();
083: }
084:
085: public void lock(Object entity, LockModeType lockMode) {
086: getPersistenceContext().lock(entity, lockMode);
087: }
088:
089: public FlushModeType getFlushMode() {
090: return getPersistenceContext().getFlushMode();
091: }
092:
093: public <T> T getReference(Class<T> entityClass, Object primaryKey) {
094: return getPersistenceContext().getReference(entityClass,
095: primaryKey);
096: }
097:
098: public void persist(Object entity) {
099: getPersistenceContext().persist(entity);
100: }
101:
102: public <T> T merge(T entity) {
103: return getPersistenceContext().merge(entity);
104: }
105:
106: public void remove(Object entity) {
107: getPersistenceContext().remove(entity);
108: }
109:
110: public <T> T find(Class<T> entityClass, Object primaryKey) {
111: return getPersistenceContext().find(entityClass, primaryKey);
112: }
113:
114: public void flush() {
115: getPersistenceContext().flush();
116: }
117:
118: public javax.persistence.Query createQuery(String ejbqlString) {
119: return getPersistenceContext().createQuery(ejbqlString);
120: }
121:
122: public javax.persistence.Query createNamedQuery(String name) {
123: return getPersistenceContext().createNamedQuery(name);
124: }
125:
126: public javax.persistence.Query createNativeQuery(String sqlString) {
127: return getPersistenceContext().createNativeQuery(sqlString);
128: }
129:
130: public javax.persistence.Query createNativeQuery(String sqlString,
131: Class resultClass) {
132: return getPersistenceContext().createNativeQuery(sqlString,
133: resultClass);
134: }
135:
136: public javax.persistence.Query createNativeQuery(String sqlString,
137: String resultSetMapping) {
138: return getPersistenceContext().createNativeQuery(sqlString,
139: resultSetMapping);
140: }
141:
142: public void refresh(Object entity) {
143: getPersistenceContext().refresh(entity);
144: }
145:
146: public boolean contains(Object entity) {
147: return getPersistenceContext().contains(entity);
148: }
149:
150: public void close() {
151: throw new IllegalStateException(
152: "It is illegal to close an injected EntityManager");
153: }
154:
155: public boolean isOpen() {
156: return getPersistenceContext().isOpen();
157: }
158:
159: public EntityTransaction getTransaction() {
160: return getPersistenceContext().getTransaction();
161: }
162:
163: public void setFlushMode(FlushModeType flushMode) {
164: getPersistenceContext().setFlushMode(flushMode);
165: }
166:
167: public Object getDelegate() {
168: return getPersistenceContext().getDelegate();
169: }
170:
171: }
|