KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > AbstractEntityManagerImpl


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

5 package org.hibernate.ejb;
6
7 import java.io.Serializable JavaDoc;
8 import javax.persistence.EntityNotFoundException;
9 import javax.persistence.EntityTransaction;
10 import javax.persistence.FlushModeType;
11 import javax.persistence.PersistenceContextType;
12 import javax.persistence.Query;
13 import javax.persistence.TransactionRequiredException;
14
15 import org.hibernate.FlushMode;
16 import org.hibernate.LockMode;
17 import org.hibernate.MappingException;
18 import org.hibernate.ObjectNotFoundException;
19 import org.hibernate.SQLQuery;
20 import org.hibernate.Session;
21 import org.hibernate.proxy.HibernateProxy;
22 import org.hibernate.engine.SessionImplementor;
23
24 /**
25  * @author <a HREF="mailto:gavin@hibernate.org">Gavin King</a>
26  * @version $Revision: 1.21 $
27  */

28 public abstract class AbstractEntityManagerImpl implements HibernateEntityManager, Serializable JavaDoc {
29
30     protected TransactionImpl tx = new TransactionImpl( this );
31     protected PersistenceContextType type;
32
33     protected AbstractEntityManagerImpl(PersistenceContextType type) {
34         this.type = type;
35     }
36
37     public Query createQuery(String JavaDoc ejbqlString) {
38         return new QueryImpl( getSession().createQuery( ejbqlString ) );
39     }
40
41     public Query createNamedQuery(String JavaDoc name) {
42         return new QueryImpl( getSession().getNamedQuery( name ) );
43     }
44
45     public Query createNativeQuery(String JavaDoc sqlString) {
46         //FIXME for scalar?
47
throw new RuntimeException JavaDoc( "NOT YET IMPLEMENTED" );
48     }
49
50     public Query createNativeQuery(String JavaDoc sqlString, Class JavaDoc resultClass) {
51         SQLQuery q = getSession().createSQLQuery( sqlString );
52         q.addEntity( "alias1", resultClass.getName(), LockMode.READ );
53         return new QueryImpl( q );
54     }
55
56     public Query createNativeQuery(String JavaDoc sqlString, String JavaDoc resultSetMapping) {
57         SQLQuery q = getSession().createSQLQuery( sqlString );
58         q.setResultSetMapping( resultSetMapping );
59         return new QueryImpl( q );
60     }
61
62 // public Object find(String entityName, Object primaryKey) {
63
// try {
64
// String realEntityName = ( (SessionFactoryImplementor) getSession().getSessionFactory() ).getImportedClassName(entityName);
65
// if (realEntityName == null) realEntityName = entityName;
66
// Object rtn = getSession().get(realEntityName, (Serializable) primaryKey);
67
// if (rtn == null) throw new EntityNotFoundException("cannot find " + primaryKey + " of entityName: " + entityName);
68
// return rtn;
69
// }
70
// catch (ObjectNotFoundException e) {
71
// throw new EntityNotFoundException(e.getMessage(), e);
72
// }
73
// catch (MappingException e) {
74
// throw new IllegalArgumentException(e.getMessage(), e);
75
// }
76
// catch (ClassCastException e) {
77
// throw new IllegalArgumentException(e.getMessage(), e);
78
// }
79
// }
80
public <T> T getReference(Class JavaDoc<T> entityClass, Object JavaDoc primaryKey) {
81         try {
82             T rtn = (T) getSession().load( entityClass, (Serializable JavaDoc) primaryKey );
83             return rtn;
84         }
85         catch (ObjectNotFoundException e) {
86             throw new EntityNotFoundException( e.getMessage(), e );
87         }
88         catch (MappingException e) {
89             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
90         }
91         catch (ClassCastException JavaDoc e) {
92             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
93         }
94     }
95
96     public <A> A find(Class JavaDoc<A> entityClass, Object JavaDoc primaryKey) {
97         try {
98             A rtn = (A) getSession().get( entityClass, (Serializable JavaDoc) primaryKey );
99             return rtn;
100         }
101         catch (ObjectNotFoundException e) {
102             //should not happen on the entity itself with get
103
throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
104         }
105         catch (MappingException e) {
106             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
107         }
108         catch (ClassCastException JavaDoc e) {
109             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
110         }
111     }
112
113     private String JavaDoc getEntityName(Object JavaDoc entity) {
114         return getEntityName( entity.getClass() );
115     }
116
117     public String JavaDoc getEntityName(Class JavaDoc clazz) {
118         return clazz.getName();
119     }
120
121     private void checkTransactionActive() {
122         if ( !( (SessionImplementor) getSession() ).isTransactionInProgress() ) {
123             throw new TransactionRequiredException( "no transaction is in progress" );
124         }
125     }
126
127     public void persist(Object JavaDoc entity) {
128         checkTransactionActive();
129         try {
130             getSession().persist( getEntityName( entity ), entity );
131         }
132         catch (MappingException e) {
133             throw new IllegalArgumentException JavaDoc( e.getMessage() );
134         }
135     }
136
137     public <A> A merge(A entity) {
138         checkTransactionActive();
139         try {
140             return (A) getSession().merge( getEntityName( entity ), entity );
141         }
142         catch (MappingException e) {
143             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
144         }
145     }
146
147     public void remove(Object JavaDoc entity) {
148         checkTransactionActive();
149         try {
150             getSession().delete( entity );
151         }
152         catch (MappingException e) {
153             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
154         }
155     }
156
157     public void refresh(Object JavaDoc entity) {
158         checkTransactionActive();
159         try {
160             getSession().refresh( entity );
161         }
162         catch (MappingException e) {
163             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
164         }
165     }
166
167     public boolean contains(Object JavaDoc entity) {
168         //TODO: needed for spec compliance, but inconvenient
169
// for testing of EM lifecycle
170
//checkTransactionActive();
171
try {
172             if (entity != null
173                 && ! (entity instanceof HibernateProxy)
174                 && getSession().getSessionFactory().getClassMetadata( entity.getClass() ) == null ) {
175                 throw new IllegalArgumentException JavaDoc( "Not an entity:" + entity.getClass() );
176             }
177             return getSession().contains( entity );
178         }
179         catch (MappingException e) {
180             throw new IllegalArgumentException JavaDoc( e.getMessage(), e );
181         }
182     }
183
184     public void flush() {
185         getSession().flush();
186     }
187
188     public abstract Session getSession();
189
190     public EntityTransaction getTransaction() {
191         return tx;
192     }
193
194     public void setFlushMode(FlushModeType flushMode) {
195         if ( flushMode == FlushModeType.AUTO ) {
196             getSession().setFlushMode( FlushMode.AUTO );
197         }
198         else if ( flushMode == FlushModeType.COMMIT ) {
199             getSession().setFlushMode( FlushMode.COMMIT );
200         }
201         else if ( flushMode == FlushModeType.NEVER ) {
202             if ( type == PersistenceContextType.TRANSACTION ) {
203                 throw new IllegalStateException JavaDoc( "You cannot set FlushModeType to NEVER for a TRANSACTION persistence context" );
204             }
205             getSession().setFlushMode( FlushMode.NEVER );
206         }
207     }
208 }
209
Popular Tags