HHH-7957 - Integrate Latest draft(s) of the JPA 2.1 spec

This commit is contained in:
Steve Ebersole 2013-03-31 10:43:28 -05:00
parent bc85168015
commit c694c26c1d
2 changed files with 176 additions and 88 deletions

View File

@ -27,7 +27,6 @@ import javax.persistence.EntityGraph;
import javax.persistence.PersistenceContextType; import javax.persistence.PersistenceContextType;
import javax.persistence.PersistenceException; import javax.persistence.PersistenceException;
import javax.persistence.SynchronizationType; import javax.persistence.SynchronizationType;
import javax.persistence.metamodel.EntityType;
import javax.persistence.spi.PersistenceUnitTransactionType; import javax.persistence.spi.PersistenceUnitTransactionType;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -97,17 +96,27 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
} }
@Override @Override
public Session getSession() { protected void checkOpen() {
if ( !open ) { if( !isOpen() ) {
throw new IllegalStateException( "EntityManager is closed" ); throw new IllegalStateException( "EntityManager is closed" );
} }
return getRawSession(); }
@Override
public Session getSession() {
checkOpen();
return internalGetSession();
} }
@Override @Override
protected Session getRawSession() { protected Session getRawSession() {
return internalGetSession();
}
@Override
protected Session internalGetSession() {
if ( session == null ) { if ( session == null ) {
SessionBuilderImplementor sessionBuilder = getEntityManagerFactory().getSessionFactory().withOptions(); SessionBuilderImplementor sessionBuilder = internalGetEntityManagerFactory().getSessionFactory().withOptions();
sessionBuilder.owner( this ); sessionBuilder.owner( this );
if (sessionInterceptorClass != null) { if (sessionInterceptorClass != null) {
try { try {
@ -135,9 +144,8 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
public void close() { public void close() {
checkEntityManagerFactory(); checkEntityManagerFactory();
if ( !open ) { checkOpen();
throw new IllegalStateException( "EntityManager is closed" );
}
if ( discardOnClose || !isTransactionInProgress() ) { if ( discardOnClose || !isTransactionInProgress() ) {
//close right now //close right now
if ( session != null ) { if ( session != null ) {
@ -153,7 +161,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
checkEntityManagerFactory(); checkEntityManagerFactory();
try { try {
if ( open ) { if ( open ) {
getSession().isOpen(); //to force enlistment in tx internalGetSession().isOpen(); //to force enlistment in tx
} }
return open; return open;
} }
@ -165,11 +173,13 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
@Override @Override
public <T> EntityGraph<T> createEntityGraph(Class<T> rootType) { public <T> EntityGraph<T> createEntityGraph(Class<T> rootType) {
checkOpen();
return new EntityGraphImpl<T>( null, getMetamodel().entity( rootType ), getEntityManagerFactory() ); return new EntityGraphImpl<T>( null, getMetamodel().entity( rootType ), getEntityManagerFactory() );
} }
@Override @Override
public EntityGraph<?> createEntityGraph(String graphName) { public EntityGraph<?> createEntityGraph(String graphName) {
checkOpen();
final EntityGraphImpl named = getEntityManagerFactory().findEntityGraphByName( graphName ); final EntityGraphImpl named = getEntityManagerFactory().findEntityGraphByName( graphName );
if ( named == null ) { if ( named == null ) {
return null; return null;
@ -180,6 +190,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> EntityGraph<T> getEntityGraph(String graphName) { public <T> EntityGraph<T> getEntityGraph(String graphName) {
checkOpen();
final EntityGraphImpl named = getEntityManagerFactory().findEntityGraphByName( graphName ); final EntityGraphImpl named = getEntityManagerFactory().findEntityGraphByName( graphName );
if ( named == null ) { if ( named == null ) {
throw new IllegalArgumentException( "Could not locate EntityGraph with given name : " + graphName ); throw new IllegalArgumentException( "Could not locate EntityGraph with given name : " + graphName );
@ -189,6 +200,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
@Override @Override
public <T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass) { public <T> List<EntityGraph<? super T>> getEntityGraphs(Class<T> entityClass) {
checkOpen();
return getEntityManagerFactory().findEntityGraphsByType( entityClass ); return getEntityManagerFactory().findEntityGraphsByType( entityClass );
} }
@ -198,7 +210,7 @@ public class EntityManagerImpl extends AbstractEntityManagerImpl implements Sess
} }
private void checkEntityManagerFactory() { private void checkEntityManagerFactory() {
if (! getEntityManagerFactory().isOpen()) { if ( ! internalGetEntityManagerFactory().isOpen() ) {
open = false; open = false;
} }
} }

View File

@ -322,19 +322,25 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public Query createQuery(String jpaqlString) { public Query createQuery(String jpaqlString) {
checkOpen();
try { try {
return applyProperties( new QueryImpl<Object>( getSession().createQuery( jpaqlString ), this ) ); return applyProperties( new QueryImpl<Object>( internalGetSession().createQuery( jpaqlString ), this ) );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
throw convert( he ); throw convert( he );
} }
} }
protected abstract void checkOpen();
@Override
public <T> TypedQuery<T> createQuery(String jpaqlString, Class<T> resultClass) { public <T> TypedQuery<T> createQuery(String jpaqlString, Class<T> resultClass) {
checkOpen();
try { try {
// do the translation // do the translation
org.hibernate.Query hqlQuery = getSession().createQuery( jpaqlString ); org.hibernate.Query hqlQuery = internalGetSession().createQuery( jpaqlString );
resultClassChecking( resultClass, hqlQuery ); resultClassChecking( resultClass, hqlQuery );
@ -529,13 +535,14 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public <T> QueryImpl<T> createQuery( public <T> QueryImpl<T> createQuery(
String jpaqlString, String jpaqlString,
Class<T> resultClass, Class<T> resultClass,
Selection selection, Selection selection,
Options options) { Options options) {
try { try {
org.hibernate.Query hqlQuery = getSession().createQuery( jpaqlString ); org.hibernate.Query hqlQuery = internalGetSession().createQuery( jpaqlString );
if ( options.getValueHandlers() == null ) { if ( options.getValueHandlers() == null ) {
if ( options.getResultMetadataValidator() != null ) { if ( options.getResultMetadataValidator() != null ) {
@ -675,23 +682,29 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return criteriaCompiler; return criteriaCompiler;
} }
@Override
public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) { public <T> TypedQuery<T> createQuery(CriteriaQuery<T> criteriaQuery) {
checkOpen();
return (TypedQuery<T>) criteriaCompiler().compile( (CompilableCriteria) criteriaQuery ); return (TypedQuery<T>) criteriaCompiler().compile( (CompilableCriteria) criteriaQuery );
} }
@Override @Override
public Query createQuery(CriteriaUpdate criteriaUpdate) { public Query createQuery(CriteriaUpdate criteriaUpdate) {
checkOpen();
return criteriaCompiler().compile( (CompilableCriteria) criteriaUpdate ); return criteriaCompiler().compile( (CompilableCriteria) criteriaUpdate );
} }
@Override @Override
public Query createQuery(CriteriaDelete criteriaDelete) { public Query createQuery(CriteriaDelete criteriaDelete) {
checkOpen();
return criteriaCompiler().compile( (CompilableCriteria) criteriaDelete ); return criteriaCompiler().compile( (CompilableCriteria) criteriaDelete );
} }
@Override
public Query createNamedQuery(String name) { public Query createNamedQuery(String name) {
checkOpen();
try { try {
org.hibernate.Query namedQuery = getSession().getNamedQuery( name ); org.hibernate.Query namedQuery = internalGetSession().getNamedQuery( name );
try { try {
return new QueryImpl( namedQuery, this ); return new QueryImpl( namedQuery, this );
} }
@ -704,7 +717,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) { public <T> TypedQuery<T> createNamedQuery(String name, Class<T> resultClass) {
checkOpen();
try { try {
/* /*
* Get the named query. * Get the named query.
@ -712,7 +727,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
* or its associated result set mapping * or its associated result set mapping
* If the named query is a HQL query, use getReturnType() * If the named query is a HQL query, use getReturnType()
*/ */
org.hibernate.Query namedQuery = getSession().getNamedQuery( name ); org.hibernate.Query namedQuery = internalGetSession().getNamedQuery( name );
//TODO clean this up to avoid downcasting //TODO clean this up to avoid downcasting
final SessionFactoryImplementor factoryImplementor = entityManagerFactory.getSessionFactory(); final SessionFactoryImplementor factoryImplementor = entityManagerFactory.getSessionFactory();
final NamedSQLQueryDefinition queryDefinition = factoryImplementor.getNamedSQLQuery( name ); final NamedSQLQueryDefinition queryDefinition = factoryImplementor.getNamedSQLQuery( name );
@ -776,9 +791,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
); );
} }
@Override
public Query createNativeQuery(String sqlString) { public Query createNativeQuery(String sqlString) {
checkOpen();
try { try {
SQLQuery q = getSession().createSQLQuery( sqlString ); SQLQuery q = internalGetSession().createSQLQuery( sqlString );
return new QueryImpl( q, this ); return new QueryImpl( q, this );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
@ -786,9 +803,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public Query createNativeQuery(String sqlString, Class resultClass) { public Query createNativeQuery(String sqlString, Class resultClass) {
checkOpen();
try { try {
SQLQuery q = getSession().createSQLQuery( sqlString ); SQLQuery q = internalGetSession().createSQLQuery( sqlString );
q.addEntity( "alias1", resultClass.getName(), LockMode.READ ); q.addEntity( "alias1", resultClass.getName(), LockMode.READ );
return new QueryImpl( q, this ); return new QueryImpl( q, this );
} }
@ -797,9 +816,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public Query createNativeQuery(String sqlString, String resultSetMapping) { public Query createNativeQuery(String sqlString, String resultSetMapping) {
checkOpen();
try { try {
SQLQuery q = getSession().createSQLQuery( sqlString ); SQLQuery q = internalGetSession().createSQLQuery( sqlString );
q.setResultSetMapping( resultSetMapping ); q.setResultSetMapping( resultSetMapping );
return new QueryImpl( q, this ); return new QueryImpl( q, this );
} }
@ -810,13 +831,15 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
@Override @Override
public StoredProcedureQuery createNamedStoredProcedureQuery(String name) { public StoredProcedureQuery createNamedStoredProcedureQuery(String name) {
checkOpen();
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
@Override @Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName) { public StoredProcedureQuery createStoredProcedureQuery(String procedureName) {
checkOpen();
try { try {
ProcedureCall procedureCall = getSession().createStoredProcedureCall( procedureName ); ProcedureCall procedureCall = internalGetSession().createStoredProcedureCall( procedureName );
return new StoredProcedureQueryImpl( procedureCall, this ); return new StoredProcedureQueryImpl( procedureCall, this );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
@ -826,8 +849,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
@Override @Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) { public StoredProcedureQuery createStoredProcedureQuery(String procedureName, Class... resultClasses) {
checkOpen();
try { try {
ProcedureCall procedureCall = getSession().createStoredProcedureCall( procedureName, resultClasses ); ProcedureCall procedureCall = internalGetSession().createStoredProcedureCall( procedureName, resultClasses );
return new StoredProcedureQueryImpl( procedureCall, this ); return new StoredProcedureQueryImpl( procedureCall, this );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
@ -837,13 +861,16 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
@Override @Override
public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) { public StoredProcedureQuery createStoredProcedureQuery(String procedureName, String... resultSetMappings) {
checkOpen();
throw new NotYetImplementedException(); throw new NotYetImplementedException();
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getReference(Class<T> entityClass, Object primaryKey) { public <T> T getReference(Class<T> entityClass, Object primaryKey) {
checkOpen();
try { try {
return ( T ) getSession().load( entityClass, ( Serializable ) primaryKey ); return ( T ) internalGetSession().load( entityClass, ( Serializable ) primaryKey );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage(), e ); throw new IllegalArgumentException( e.getMessage(), e );
@ -859,35 +886,44 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <A> A find(Class<A> entityClass, Object primaryKey) { public <A> A find(Class<A> entityClass, Object primaryKey) {
checkOpen();
return find( entityClass, primaryKey, null, null ); return find( entityClass, primaryKey, null, null );
} }
@Override
public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) { public <T> T find(Class<T> entityClass, Object primaryKey, Map<String, Object> properties) {
checkOpen();
return find( entityClass, primaryKey, null, properties ); return find( entityClass, primaryKey, null, properties );
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <A> A find(Class<A> entityClass, Object primaryKey, LockModeType lockModeType) { public <A> A find(Class<A> entityClass, Object primaryKey, LockModeType lockModeType) {
checkOpen();
return find( entityClass, primaryKey, lockModeType, null ); return find( entityClass, primaryKey, lockModeType, null );
} }
@Override
public <A> A find(Class<A> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) { public <A> A find(Class<A> entityClass, Object primaryKey, LockModeType lockModeType, Map<String, Object> properties) {
CacheMode previousCacheMode = getSession().getCacheMode(); checkOpen();
Session session = internalGetSession();
CacheMode previousCacheMode = session.getCacheMode();
CacheMode cacheMode = determineAppropriateLocalCacheMode( properties ); CacheMode cacheMode = determineAppropriateLocalCacheMode( properties );
LockOptions lockOptions = null; LockOptions lockOptions = null;
try { try {
getSession().setCacheMode( cacheMode ); session.setCacheMode( cacheMode );
if ( lockModeType != null ) { if ( lockModeType != null ) {
lockOptions = getLockRequest( lockModeType, properties ); lockOptions = getLockRequest( lockModeType, properties );
return ( A ) getSession().get( return ( A ) session.get(
entityClass, ( Serializable ) primaryKey, entityClass, ( Serializable ) primaryKey,
lockOptions lockOptions
); );
} }
else { else {
return ( A ) getSession().get( entityClass, ( Serializable ) primaryKey ); return ( A ) session.get( entityClass, ( Serializable ) primaryKey );
} }
} }
catch ( EntityNotFoundException ignored ) { catch ( EntityNotFoundException ignored ) {
@ -921,7 +957,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
throw convert( he, lockOptions ); throw convert( he, lockOptions );
} }
finally { finally {
getSession().setCacheMode( previousCacheMode ); session.setCacheMode( previousCacheMode );
} }
} }
@ -952,10 +988,12 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public void persist(Object entity) { public void persist(Object entity) {
checkOpen();
checkTransactionNeeded(); checkTransactionNeeded();
try { try {
getSession().persist( entity ); internalGetSession().persist( entity );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage() ); throw new IllegalArgumentException( e.getMessage() );
@ -965,11 +1003,13 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <A> A merge(A entity) { public <A> A merge(A entity) {
checkOpen();
checkTransactionNeeded(); checkTransactionNeeded();
try { try {
return ( A ) getSession().merge( entity ); return ( A ) internalGetSession().merge( entity );
} }
catch ( ObjectDeletedException sse ) { catch ( ObjectDeletedException sse ) {
throw new IllegalArgumentException( sse ); throw new IllegalArgumentException( sse );
@ -982,10 +1022,12 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public void remove(Object entity) { public void remove(Object entity) {
checkOpen();
checkTransactionNeeded(); checkTransactionNeeded();
try { try {
getSession().delete( entity ); internalGetSession().delete( entity );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage(), e ); throw new IllegalArgumentException( e.getMessage(), e );
@ -995,34 +1037,41 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public void refresh(Object entity) { public void refresh(Object entity) {
refresh( entity, null, null ); refresh( entity, null, null );
} }
@Override
public void refresh(Object entity, Map<String, Object> properties) { public void refresh(Object entity, Map<String, Object> properties) {
refresh( entity, null, properties ); refresh( entity, null, properties );
} }
@Override
public void refresh(Object entity, LockModeType lockModeType) { public void refresh(Object entity, LockModeType lockModeType) {
refresh( entity, lockModeType, null ); refresh( entity, lockModeType, null );
} }
@Override
public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) { public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
checkOpen();
checkTransactionNeeded(); checkTransactionNeeded();
CacheMode previousCacheMode = getSession().getCacheMode();
Session session = internalGetSession();
CacheMode previousCacheMode = session.getCacheMode();
CacheMode localCacheMode = determineAppropriateLocalCacheMode( properties ); CacheMode localCacheMode = determineAppropriateLocalCacheMode( properties );
LockOptions lockOptions = null; LockOptions lockOptions = null;
try { try {
getSession().setCacheMode( localCacheMode ); session.setCacheMode( localCacheMode );
if ( !getSession().contains( entity ) ) { if ( !session.contains( entity ) ) {
throw new IllegalArgumentException( "Entity not managed" ); throw new IllegalArgumentException( "Entity not managed" );
} }
if ( lockModeType != null ) { if ( lockModeType != null ) {
lockOptions = getLockRequest( lockModeType, properties ); lockOptions = getLockRequest( lockModeType, properties );
getSession().refresh( entity, lockOptions ); session.refresh( entity, lockOptions );
} }
else { else {
getSession().refresh( entity ); session.refresh( entity );
} }
} }
catch ( MappingException e ) { catch ( MappingException e ) {
@ -1032,18 +1081,21 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
throw convert( he, lockOptions ); throw convert( he, lockOptions );
} }
finally { finally {
getSession().setCacheMode( previousCacheMode ); session.setCacheMode( previousCacheMode );
} }
} }
@Override
public boolean contains(Object entity) { public boolean contains(Object entity) {
checkOpen();
try { try {
if ( entity != null if ( entity != null
&& !( entity instanceof HibernateProxy ) && !( entity instanceof HibernateProxy )
&& getSession().getSessionFactory().getClassMetadata( entity.getClass() ) == null ) { && internalGetSession().getSessionFactory().getClassMetadata( entity.getClass() ) == null ) {
throw new IllegalArgumentException( "Not an entity:" + entity.getClass() ); throw new IllegalArgumentException( "Not an entity:" + entity.getClass() );
} }
return getSession().contains( entity ); return internalGetSession().contains( entity );
} }
catch ( MappingException e ) { catch ( MappingException e ) {
throw new IllegalArgumentException( e.getMessage(), e ); throw new IllegalArgumentException( e.getMessage(), e );
@ -1053,30 +1105,43 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
@Override
public LockModeType getLockMode(Object entity) { public LockModeType getLockMode(Object entity) {
checkOpen();
if ( !contains( entity ) ) { if ( !contains( entity ) ) {
throw new IllegalArgumentException( "entity not in the persistence context" ); throw new IllegalArgumentException( "entity not in the persistence context" );
} }
return getLockModeType( getSession().getCurrentLockMode( entity ) ); return getLockModeType( internalGetSession().getCurrentLockMode( entity ) );
} }
@Override
public void setProperty(String s, Object o) { public void setProperty(String s, Object o) {
checkOpen();
if ( entityManagerSpecificProperties.contains( s ) ) { if ( entityManagerSpecificProperties.contains( s ) ) {
properties.put( s, o ); properties.put( s, o );
applyProperties(); applyProperties();
} else LOG.debugf("Trying to set a property which is not supported on entity manager level"); }
else {
LOG.debugf("Trying to set a property which is not supported on entity manager level");
}
} }
@Override
public Map<String, Object> getProperties() { public Map<String, Object> getProperties() {
return Collections.unmodifiableMap( properties ); return Collections.unmodifiableMap( properties );
} }
@Override
public void flush() { public void flush() {
checkOpen();
if ( !isTransactionInProgress() ) { if ( !isTransactionInProgress() ) {
throw new TransactionRequiredException( "no transaction is in progress" ); throw new TransactionRequiredException( "no transaction is in progress" );
} }
try { try {
getSession().flush(); internalGetSession().flush();
} }
catch ( RuntimeException e ) { catch ( RuntimeException e ) {
throw convert( e ); throw convert( e );
@ -1094,9 +1159,19 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
* Return a Session (even if the entity manager is closed). * Return a Session (even if the entity manager is closed).
* *
* @return A session. * @return A session.
* @deprecated Deprecated in favor of {@link #getRawSession()}
*/ */
@Deprecated
protected abstract Session getRawSession(); protected abstract Session getRawSession();
/**
* Return a Session without any validation checks.
*
* @return A session.
*/
protected abstract Session internalGetSession();
@Override
public EntityTransaction getTransaction() { public EntityTransaction getTransaction() {
if ( transactionType == PersistenceUnitTransactionType.JTA ) { if ( transactionType == PersistenceUnitTransactionType.JTA ) {
throw new IllegalStateException( "A JTA EntityManager cannot use getTransaction()" ); throw new IllegalStateException( "A JTA EntityManager cannot use getTransaction()" );
@ -1104,58 +1179,63 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return tx; return tx;
} }
/** @Override
* {@inheritDoc}
*/
public EntityManagerFactoryImpl getEntityManagerFactory() { public EntityManagerFactoryImpl getEntityManagerFactory() {
checkOpen();
return internalGetEntityManagerFactory();
}
protected EntityManagerFactoryImpl internalGetEntityManagerFactory() {
return entityManagerFactory; return entityManagerFactory;
} }
/** @Override
* {@inheritDoc}
*/
public HibernateEntityManagerFactory getFactory() { public HibernateEntityManagerFactory getFactory() {
return entityManagerFactory; return entityManagerFactory;
} }
/** @Override
* {@inheritDoc}
*/
public CriteriaBuilder getCriteriaBuilder() { public CriteriaBuilder getCriteriaBuilder() {
checkOpen();
return getEntityManagerFactory().getCriteriaBuilder(); return getEntityManagerFactory().getCriteriaBuilder();
} }
/** @Override
* {@inheritDoc}
*/
public Metamodel getMetamodel() { public Metamodel getMetamodel() {
checkOpen();
return getEntityManagerFactory().getMetamodel(); return getEntityManagerFactory().getMetamodel();
} }
@Override
public void setFlushMode(FlushModeType flushModeType) { public void setFlushMode(FlushModeType flushModeType) {
checkOpen();
if ( flushModeType == FlushModeType.AUTO ) { if ( flushModeType == FlushModeType.AUTO ) {
getSession().setFlushMode( FlushMode.AUTO ); internalGetSession().setFlushMode( FlushMode.AUTO );
} }
else if ( flushModeType == FlushModeType.COMMIT ) { else if ( flushModeType == FlushModeType.COMMIT ) {
getSession().setFlushMode( FlushMode.COMMIT ); internalGetSession().setFlushMode( FlushMode.COMMIT );
} }
else { else {
throw new AssertionFailure( "Unknown FlushModeType: " + flushModeType ); throw new AssertionFailure( "Unknown FlushModeType: " + flushModeType );
} }
} }
@Override
public void clear() { public void clear() {
checkOpen();
try { try {
getSession().clear(); internalGetSession().clear();
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
throw convert( he ); throw convert( he );
} }
} }
@Override
public void detach(Object entity) { public void detach(Object entity) {
checkOpen();
try { try {
getSession().evict( entity ); internalGetSession().evict( entity );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
throw convert( he ); throw convert( he );
@ -1168,8 +1248,11 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
* If it returns null, do em.unwrap(Session.class).getFlushMode() to get the * If it returns null, do em.unwrap(Session.class).getFlushMode() to get the
* Hibernate flush mode * Hibernate flush mode
*/ */
@Override
public FlushModeType getFlushMode() { public FlushModeType getFlushMode() {
FlushMode mode = getSession().getFlushMode(); checkOpen();
FlushMode mode = internalGetSession().getFlushMode();
if ( mode == FlushMode.AUTO ) { if ( mode == FlushMode.AUTO ) {
return FlushModeType.AUTO; return FlushModeType.AUTO;
} }
@ -1187,6 +1270,8 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
public void lock(Object entity, LockModeType lockModeType, Map<String, Object> properties) { public void lock(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
checkOpen();
LockOptions lockOptions = null; LockOptions lockOptions = null;
if ( !isTransactionInProgress() ) { if ( !isTransactionInProgress() ) {
throw new TransactionRequiredException( "no transaction is in progress" ); throw new TransactionRequiredException( "no transaction is in progress" );
@ -1197,7 +1282,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
throw new IllegalArgumentException( "entity not in the persistence context" ); throw new IllegalArgumentException( "entity not in the persistence context" );
} }
lockOptions = getLockRequest( lockModeType, properties ); lockOptions = getLockRequest( lockModeType, properties );
getSession().buildLockRequest( lockOptions ).lock( entity ); internalGetSession().buildLockRequest( lockOptions ).lock( entity );
} }
catch ( HibernateException he ) { catch ( HibernateException he ) {
throw convert( he, lockOptions ); throw convert( he, lockOptions );
@ -1226,20 +1311,22 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
public boolean isTransactionInProgress() { public boolean isTransactionInProgress() {
return ( ( SessionImplementor ) getRawSession() ).isTransactionInProgress(); return ( ( SessionImplementor ) internalGetSession() ).isTransactionInProgress();
} }
private SessionFactoryImplementor sfi() { private SessionFactoryImplementor sfi() {
return (SessionFactoryImplementor) getRawSession().getSessionFactory(); return (SessionFactoryImplementor) internalGetSession().getSessionFactory();
} }
@Override @Override
public <T> T unwrap(Class<T> clazz) { public <T> T unwrap(Class<T> clazz) {
checkOpen();
if ( Session.class.isAssignableFrom( clazz ) ) { if ( Session.class.isAssignableFrom( clazz ) ) {
return ( T ) getSession(); return ( T ) internalGetSession();
} }
if ( SessionImplementor.class.isAssignableFrom( clazz ) ) { if ( SessionImplementor.class.isAssignableFrom( clazz ) ) {
return ( T ) getSession(); return ( T ) internalGetSession();
} }
if ( EntityManager.class.isAssignableFrom( clazz ) ) { if ( EntityManager.class.isAssignableFrom( clazz ) ) {
return ( T ) this; return ( T ) this;
@ -1275,7 +1362,9 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
@Override @Override
public boolean isJoinedToTransaction() { public boolean isJoinedToTransaction() {
final SessionImplementor session = (SessionImplementor) getSession(); checkOpen();
final SessionImplementor session = (SessionImplementor) internalGetSession();
final TransactionCoordinator transactionCoordinator = session.getTransactionCoordinator(); final TransactionCoordinator transactionCoordinator = session.getTransactionCoordinator();
final TransactionImplementor transaction = transactionCoordinator.getTransaction(); final TransactionImplementor transaction = transactionCoordinator.getTransaction();
@ -1284,9 +1373,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
@Override @Override
public void joinTransaction() { public void joinTransaction() {
if( !isOpen() ){ checkOpen();
throw new IllegalStateException( "EntityManager is closed" );
}
joinTransaction( true ); joinTransaction( true );
} }
@ -1298,7 +1385,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return; return;
} }
final SessionImplementor session = (SessionImplementor) getSession(); final SessionImplementor session = (SessionImplementor) internalGetSession();
final TransactionCoordinator transactionCoordinator = session.getTransactionCoordinator(); final TransactionCoordinator transactionCoordinator = session.getTransactionCoordinator();
final TransactionImplementor transaction = transactionCoordinator.getTransaction(); final TransactionImplementor transaction = transactionCoordinator.getTransaction();
@ -1350,7 +1437,8 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
* returns the underlying session * returns the underlying session
*/ */
public Object getDelegate() { public Object getDelegate() {
return getSession(); checkOpen();
return internalGetSession();
} }
private void writeObject(ObjectOutputStream oos) throws IOException { private void writeObject(ObjectOutputStream oos) throws IOException {
@ -1362,9 +1450,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
tx = new TransactionImpl( this ); tx = new TransactionImpl( this );
} }
/** @Override
* {@inheritDoc}
*/
public void handlePersistenceException(PersistenceException e) { public void handlePersistenceException(PersistenceException e) {
if ( e instanceof NoResultException ) { if ( e instanceof NoResultException ) {
return; return;
@ -1388,19 +1474,15 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
/** @Override
* {@inheritDoc}
*/
public void throwPersistenceException(PersistenceException e) { public void throwPersistenceException(PersistenceException e) {
handlePersistenceException( e ); handlePersistenceException( e );
throw e; throw e;
} }
/** @Override
* {@inheritDoc}
*/
//FIXME should we remove all calls to this method and use convert(RuntimeException) ?
public RuntimeException convert(HibernateException e) { public RuntimeException convert(HibernateException e) {
//FIXME should we remove all calls to this method and use convert(RuntimeException) ?
return convert( e, null ); return convert( e, null );
} }
@ -1415,9 +1497,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
return result; return result;
} }
/** @Override
* {@inheritDoc}
*/
public RuntimeException convert(HibernateException e, LockOptions lockOptions) { public RuntimeException convert(HibernateException e, LockOptions lockOptions) {
if ( e instanceof StaleStateException ) { if ( e instanceof StaleStateException ) {
PersistenceException converted = wrapStaleStateException( ( StaleStateException ) e ); PersistenceException converted = wrapStaleStateException( ( StaleStateException ) e );
@ -1484,16 +1564,12 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
} }
} }
/** @Override
* {@inheritDoc}
*/
public void throwPersistenceException(HibernateException e) { public void throwPersistenceException(HibernateException e) {
throw convert( e ); throw convert( e );
} }
/** @Override
* {@inheritDoc}
*/
public PersistenceException wrapStaleStateException(StaleStateException e) { public PersistenceException wrapStaleStateException(StaleStateException e) {
PersistenceException pe; PersistenceException pe;
if ( e instanceof StaleObjectStateException ) { if ( e instanceof StaleObjectStateException ) {
@ -1501,7 +1577,7 @@ public abstract class AbstractEntityManagerImpl implements HibernateEntityManage
Serializable identifier = sose.getIdentifier(); Serializable identifier = sose.getIdentifier();
if ( identifier != null ) { if ( identifier != null ) {
try { try {
Object entity = getRawSession().load( sose.getEntityName(), identifier ); Object entity = internalGetSession().load( sose.getEntityName(), identifier );
if ( entity instanceof Serializable ) { if ( entity instanceof Serializable ) {
//avoid some user errors regarding boundary crossing //avoid some user errors regarding boundary crossing
pe = new OptimisticLockException( null, e, entity ); pe = new OptimisticLockException( null, e, entity );