KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > entity > ExtendedEntityManager


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.entity;
23
24 import java.io.Serializable JavaDoc;
25 import javax.persistence.EntityManager;
26 import javax.persistence.EntityTransaction;
27 import javax.persistence.FlushModeType;
28 import javax.persistence.LockModeType;
29 import org.hibernate.Session;
30 import org.hibernate.ejb.HibernateEntityManager;
31 import org.jboss.ejb3.stateful.StatefulBeanContext;
32
33 /**
34  * EntityManager a managed extended persistence context.
35  *
36  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
37  */

38 public class ExtendedEntityManager implements EntityManager, HibernateSession, Serializable JavaDoc, ExtendedPersistenceContext
39 {
40    private String JavaDoc identity;
41
42    public ExtendedEntityManager(String JavaDoc name)
43    {
44       this.identity = name;
45    }
46
47    public ExtendedEntityManager()
48    {
49    }
50
51    public ManagedEntityManagerFactory getFactory()
52    {
53       throw new RuntimeException JavaDoc("NOT IMPLEMENTED");
54    }
55
56    public EntityManager getPersistenceContext()
57    {
58       StatefulBeanContext beanContext = StatefulBeanContext.currentBean.get();
59       EntityManager persistenceContext = beanContext.getExtendedPersistenceContext(identity);
60       if (persistenceContext == null)
61          throw new RuntimeException JavaDoc("Unable to determine persistenceContext: " + identity
62                                     + " in injected SFSB: " + beanContext.getContainer().getObjectName());
63       return persistenceContext;
64    }
65
66    // delegates
67

68    public Session getHibernateSession()
69    {
70       if (getPersistenceContext() instanceof HibernateEntityManager)
71       {
72          return ((HibernateEntityManager) getPersistenceContext()).getSession();
73       }
74       throw new RuntimeException JavaDoc("ILLEGAL ACTION: Not a Hibernate persistence provider");
75    }
76
77    public void joinTransaction()
78    {
79       getPersistenceContext().joinTransaction();
80    }
81
82    public void clear()
83    {
84       getPersistenceContext().clear();
85    }
86
87    public void lock(Object JavaDoc entity, LockModeType lockMode)
88    {
89       getPersistenceContext().lock(entity, lockMode);
90    }
91
92    public FlushModeType getFlushMode()
93    {
94       return getPersistenceContext().getFlushMode();
95    }
96
97    public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey)
98    {
99       return getPersistenceContext().getReference(entityClass, primaryKey);
100    }
101
102    public void persist(Object JavaDoc entity)
103    {
104       getPersistenceContext().persist(entity);
105    }
106
107    public <T> T merge(T entity)
108    {
109       return getPersistenceContext().merge(entity);
110    }
111
112    public void remove(Object JavaDoc entity)
113    {
114       getPersistenceContext().remove(entity);
115    }
116
117    public <T> T find(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey)
118    {
119       return getPersistenceContext().find(entityClass, primaryKey);
120    }
121
122    public void flush()
123    {
124       getPersistenceContext().flush();
125    }
126
127    public javax.persistence.Query createQuery(String JavaDoc ejbqlString)
128    {
129       return getPersistenceContext().createQuery(ejbqlString);
130    }
131
132    public javax.persistence.Query createNamedQuery(String JavaDoc name)
133    {
134       return getPersistenceContext().createNamedQuery(name);
135    }
136
137    public javax.persistence.Query createNativeQuery(String JavaDoc sqlString)
138    {
139       return getPersistenceContext().createNativeQuery(sqlString);
140    }
141
142    public javax.persistence.Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass)
143    {
144       return getPersistenceContext().createNativeQuery(sqlString, resultClass);
145    }
146
147    public javax.persistence.Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping)
148    {
149       return getPersistenceContext().createNativeQuery(sqlString, resultSetMapping);
150    }
151
152    public void refresh(Object JavaDoc entity)
153    {
154       getPersistenceContext().refresh(entity);
155    }
156
157    public boolean contains(Object JavaDoc entity)
158    {
159       return getPersistenceContext().contains(entity);
160    }
161
162    public void close()
163    {
164       throw new IllegalStateException JavaDoc("It is illegal to close an injected EntityManager");
165    }
166
167    public boolean isOpen()
168    {
169       return getPersistenceContext().isOpen();
170    }
171
172    public EntityTransaction getTransaction()
173    {
174       return getPersistenceContext().getTransaction();
175    }
176
177    public void setFlushMode(FlushModeType flushMode)
178    {
179       getPersistenceContext().setFlushMode(flushMode);
180    }
181
182    public Object JavaDoc getDelegate()
183    {
184       return getPersistenceContext();
185    }
186
187 }
188
189
Popular Tags