From 8796fe5ed6a250885e16c17c61037258937fd963 Mon Sep 17 00:00:00 2001 From: edalquist Date: Fri, 30 Sep 2011 16:44:43 -0500 Subject: [PATCH] HHH-2879 Remove use of generics due to proxy issues --- .../org/hibernate/IdentifierLoadAccess.java | 36 +++++++++++++------ .../org/hibernate/internal/SessionImpl.java | 35 +++++------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/IdentifierLoadAccess.java b/hibernate-core/src/main/java/org/hibernate/IdentifierLoadAccess.java index 26fa2260d4..a6e7bb613d 100644 --- a/hibernate-core/src/main/java/org/hibernate/IdentifierLoadAccess.java +++ b/hibernate-core/src/main/java/org/hibernate/IdentifierLoadAccess.java @@ -38,19 +38,33 @@ public interface IdentifierLoadAccess { public IdentifierLoadAccess with(LockOptions lockOptions); /** - * Same behavior as {@link Session#load(Class, java.io.Serializable)} - * - * @param id The primary key of the entity - * @return The entity - * @throws HibernateException if the entity does not exist + * Return the persistent instance of the given entity class with the given identifier, + * assuming that the instance exists. This method might return a proxied instance that + * is initialized on-demand, when a non-identifier method is accessed. + *

+ * You should not use this method to determine if an instance exists (use get() + * instead). Use this only to retrieve an instance that you assume exists, where non-existence + * would be an actual error. + *

+ * Due to the nature of the proxy functionality the return type of this method cannot use + * the generic type. + * + * @param theClass a persistent class + * @param id a valid identifier of an existing persistent instance of the class + * @return the persistent instance or proxy + * @throws HibernateException */ - public T getReference(Serializable id); + public Object getReference(Serializable id); /** - * Same behavior as {@link Session#get(Class, java.io.Serializable)} - * - * @param id The primary key of the entity - * @return The entity or null if it does not exist + * Return the persistent instance of the given entity class with the given identifier, + * or null if there is no such persistent instance. (If the instance is already associated + * with the session, return that instance. This method never returns an uninitialized instance.) + * + * @param clazz a persistent class + * @param id an identifier + * @return a persistent instance or null + * @throws HibernateException */ - public T load(Serializable id); + public Object load(Serializable id); } diff --git a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java index 915535957f..ed7b5d94cc 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -2174,12 +2174,8 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc private final String entityName; private final Class entityClass; private LockOptions lockOptions; - @Deprecated - private LockMode lockMode; /** - * Note that the specified entity MUST be castable using {@link Class#cast(Object)} - * on the specified entityClass */ private IdentifierLoadAccessImpl(String entityName, Class entityClass) { this.entityName = entityName; @@ -2191,9 +2187,6 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc */ @Override public final IdentifierLoadAccessImpl with(LockOptions lockOptions) { - if (this.lockMode != null) { - throw new IllegalArgumentException("Cannot specify by LockOptions and LockMode on a single IdentifierLoadAccessImpl"); - } this.lockOptions = lockOptions; return this; } @@ -2204,10 +2197,8 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc */ @Deprecated public final IdentifierLoadAccessImpl with(LockMode lockMode) { - if (this.lockOptions != null) { - throw new IllegalArgumentException("Cannot specify by LockOptions and LockMode on a single IdentifierLoadAccessImpl"); - } - this.lockMode = lockMode; + this.lockOptions = new LockOptions(); + this.lockOptions.setLockMode(lockMode); return this; } @@ -2215,17 +2206,12 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc * @see org.hibernate.IdentifierLoadAccess#getReference(java.io.Serializable) */ @Override - public final T getReference(Serializable id) { + public final Object getReference(Serializable id) { if (this.lockOptions != null) { LoadEvent event = new LoadEvent(id, entityName, lockOptions, SessionImpl.this); fireLoad( event, LoadEventListener.LOAD ); - return this.entityClass.cast(event.getResult()); + return event.getResult(); } - if (this.lockMode != null) { - LoadEvent event = new LoadEvent(id, entityName, lockMode, SessionImpl.this); - fireLoad( event, LoadEventListener.LOAD ); - return this.entityClass.cast(event.getResult()); - } LoadEvent event = new LoadEvent(id, entityName, false, SessionImpl.this); boolean success = false; @@ -2235,7 +2221,7 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id ); } success = true; - return this.entityClass.cast(event.getResult()); + return event.getResult(); } finally { afterOperation(success); @@ -2246,16 +2232,11 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc * @see org.hibernate.IdentifierLoadAccess#load(java.io.Serializable) */ @Override - public final T load(Serializable id) { + public final Object load(Serializable id) { if (this.lockOptions != null) { LoadEvent event = new LoadEvent(id, entityName, lockOptions, SessionImpl.this); fireLoad( event, LoadEventListener.GET ); - return this.entityClass.cast(event.getResult()); - } - if (this.lockMode != null) { - LoadEvent event = new LoadEvent(id, entityName, lockMode, SessionImpl.this); - fireLoad( event, LoadEventListener.GET ); - return this.entityClass.cast(event.getResult()); + return event.getResult(); } LoadEvent event = new LoadEvent(id, entityName, false, SessionImpl.this); @@ -2263,7 +2244,7 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc try { fireLoad(event, LoadEventListener.GET); success = true; - return this.entityClass.cast(event.getResult()); + return event.getResult(); } finally { afterOperation(success);