KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, the OpenSource EJB server
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.ejb3.entity;
8
9 import java.io.Serializable JavaDoc;
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.NamingException JavaDoc;
12 import javax.persistence.EntityManager;
13 import javax.persistence.EntityTransaction;
14 import javax.persistence.FlushModeType;
15 import javax.persistence.Query;
16 import org.hibernate.Session;
17 import org.hibernate.ejb.HibernateEntityManager;
18
19
20 /**
21  * @author <a HREF="mailto:gavine@hibernate.org">Gavin King</a>
22  * @version $Revision: 1.1.2.3 $
23  */

24 public class InjectedEntityManager implements EntityManager, HibernateSession, Serializable JavaDoc
25 {
26    private transient ManagedEntityManagerFactory factory;
27    private String JavaDoc jndiName;
28
29    public Session getHibernateSession()
30    {
31       if (getSession() instanceof HibernateEntityManager)
32       {
33          return ((HibernateEntityManager)getSession()).getSession();
34       }
35       throw new RuntimeException JavaDoc("ILLEGAL ACTION: Not a Hibernate persistence provider");
36    }
37
38    public InjectedEntityManager(ManagedEntityManagerFactory factory)
39    {
40       if (factory == null) throw new NullPointerException JavaDoc("factory must not be null");
41       this.factory = factory;
42       this.jndiName = factory.getJndiName();
43    }
44
45    public InjectedEntityManager()
46    {
47       
48    }
49
50    public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey)
51    {
52       return getSession().getReference(entityClass, primaryKey);
53    }
54
55    public void setFlushMode(FlushModeType flushMode)
56    {
57       getSession().setFlushMode(flushMode);
58    }
59
60    public Query createQuery(String JavaDoc ejbqlString)
61    {
62       return getSession().createQuery(ejbqlString);
63    }
64
65    public Query createNamedQuery(String JavaDoc name)
66    {
67       return getSession().createNamedQuery(name);
68    }
69
70    public Query createNativeQuery(String JavaDoc sqlString)
71    {
72       return getSession().createNativeQuery(sqlString);
73    }
74
75    public Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass)
76    {
77       return getSession().createNativeQuery(sqlString, resultClass);
78    }
79
80    public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping)
81    {
82       return getSession().createNativeQuery(sqlString, resultSetMapping);
83    }
84
85    public <A> A find(Class JavaDoc<A> entityClass, Object JavaDoc primaryKey)
86    {
87       return getSession().find(entityClass, primaryKey);
88    }
89
90    public void persist(Object JavaDoc entity)
91    {
92       getSession().persist(entity);
93    }
94
95    public <A> A merge(A entity)
96    {
97       return (A) getSession().merge(entity);
98    }
99
100    public void remove(Object JavaDoc entity)
101    {
102       getSession().remove(entity);
103    }
104
105    public void refresh(Object JavaDoc entity)
106    {
107       getSession().refresh(entity);
108    }
109
110    public boolean contains(Object JavaDoc entity)
111    {
112       return getSession().contains(entity);
113    }
114
115    public void flush()
116    {
117       getSession().flush();
118    }
119
120    public void close()
121    {
122       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
123    }
124
125    public boolean isOpen()
126    {
127       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
128    }
129
130    public EntityTransaction getTransaction()
131    {
132       throw new IllegalStateException JavaDoc("Illegal to call this method from injected, managed EntityManager");
133    }
134
135    protected EntityManager getSession()
136    {
137       if (factory == null)
138       {
139          try
140          {
141             InitialContext JavaDoc ctx = new InitialContext JavaDoc();
142             factory = (ManagedEntityManagerFactory)ctx.lookup(jndiName);
143          }
144          catch (NamingException JavaDoc e)
145          {
146             throw new RuntimeException JavaDoc(e);
147          }
148       }
149       return factory.getSession();
150    }
151
152 }
153
Popular Tags