1 5 package org.hibernate.ejb; 6 7 import java.io.Serializable ; 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 28 public abstract class AbstractEntityManagerImpl implements HibernateEntityManager, Serializable { 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 ejbqlString) { 38 return new QueryImpl( getSession().createQuery( ejbqlString ) ); 39 } 40 41 public Query createNamedQuery(String name) { 42 return new QueryImpl( getSession().getNamedQuery( name ) ); 43 } 44 45 public Query createNativeQuery(String sqlString) { 46 throw new RuntimeException ( "NOT YET IMPLEMENTED" ); 48 } 49 50 public Query createNativeQuery(String sqlString, Class 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 sqlString, String resultSetMapping) { 57 SQLQuery q = getSession().createSQLQuery( sqlString ); 58 q.setResultSetMapping( resultSetMapping ); 59 return new QueryImpl( q ); 60 } 61 62 public <T> T getReference(Class <T> entityClass, Object primaryKey) { 81 try { 82 T rtn = (T) getSession().load( entityClass, (Serializable ) 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 ( e.getMessage(), e ); 90 } 91 catch (ClassCastException e) { 92 throw new IllegalArgumentException ( e.getMessage(), e ); 93 } 94 } 95 96 public <A> A find(Class <A> entityClass, Object primaryKey) { 97 try { 98 A rtn = (A) getSession().get( entityClass, (Serializable ) primaryKey ); 99 return rtn; 100 } 101 catch (ObjectNotFoundException e) { 102 throw new IllegalArgumentException ( e.getMessage(), e ); 104 } 105 catch (MappingException e) { 106 throw new IllegalArgumentException ( e.getMessage(), e ); 107 } 108 catch (ClassCastException e) { 109 throw new IllegalArgumentException ( e.getMessage(), e ); 110 } 111 } 112 113 private String getEntityName(Object entity) { 114 return getEntityName( entity.getClass() ); 115 } 116 117 public String getEntityName(Class 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 entity) { 128 checkTransactionActive(); 129 try { 130 getSession().persist( getEntityName( entity ), entity ); 131 } 132 catch (MappingException e) { 133 throw new IllegalArgumentException ( 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 ( e.getMessage(), e ); 144 } 145 } 146 147 public void remove(Object entity) { 148 checkTransactionActive(); 149 try { 150 getSession().delete( entity ); 151 } 152 catch (MappingException e) { 153 throw new IllegalArgumentException ( e.getMessage(), e ); 154 } 155 } 156 157 public void refresh(Object entity) { 158 checkTransactionActive(); 159 try { 160 getSession().refresh( entity ); 161 } 162 catch (MappingException e) { 163 throw new IllegalArgumentException ( e.getMessage(), e ); 164 } 165 } 166 167 public boolean contains(Object entity) { 168 try { 172 if (entity != null 173 && ! (entity instanceof HibernateProxy) 174 && getSession().getSessionFactory().getClassMetadata( entity.getClass() ) == null ) { 175 throw new IllegalArgumentException ( "Not an entity:" + entity.getClass() ); 176 } 177 return getSession().contains( entity ); 178 } 179 catch (MappingException e) { 180 throw new IllegalArgumentException ( 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 ( "You cannot set FlushModeType to NEVER for a TRANSACTION persistence context" ); 204 } 205 getSession().setFlushMode( FlushMode.NEVER ); 206 } 207 } 208 } 209 | Popular Tags |