diff --git a/hibernate-core/src/main/java/org/hibernate/Session.java b/hibernate-core/src/main/java/org/hibernate/Session.java index b4eb38d5f5..ad50ed1e99 100644 --- a/hibernate-core/src/main/java/org/hibernate/Session.java +++ b/hibernate-core/src/main/java/org/hibernate/Session.java @@ -796,7 +796,7 @@ public interface Session extends SharedSessionContract { * @param entityName The name of the entity that will be retrieved * @throws HibernateException If the specified entity name is not found or if the entity does not have a natural id specified */ - public NaturalIdLoadAccess byNaturalKey(String entityName); + public NaturalIdLoadAccess byNaturalId(String entityName); /** * Create an {@link NaturalIdLoadAccess} instance to retrieve the specified entity by @@ -805,7 +805,7 @@ public interface Session extends SharedSessionContract { * @param entityClass The type of the entity that will be retrieved * @throws HibernateException If the specified Class is not an entity or if the entity does not have a natural id specified */ - public NaturalIdLoadAccess byNaturalKey(Class entityClass); + public NaturalIdLoadAccess byNaturalId(Class entityClass); /** * Enable the named filter for this current session. 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 6baa5f0e3c..915535957f 100644 --- a/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java @@ -842,40 +842,19 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc } public Object load(Class entityClass, Serializable id) throws HibernateException { - return load( entityClass.getName(), id ); + return this.byId(entityClass).getReference(id); } public Object load(String entityName, Serializable id) throws HibernateException { - LoadEvent event = new LoadEvent(id, entityName, false, this); - boolean success = false; - try { - fireLoad( event, LoadEventListener.LOAD ); - if ( event.getResult() == null ) { - getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id ); - } - success = true; - return event.getResult(); - } - finally { - afterOperation(success); - } + return this.byId(entityName).getReference(id); } public Object get(Class entityClass, Serializable id) throws HibernateException { - return get( entityClass.getName(), id ); + return this.byId(entityClass).load(id); } public Object get(String entityName, Serializable id) throws HibernateException { - LoadEvent event = new LoadEvent(id, entityName, false, this); - boolean success = false; - try { - fireLoad(event, LoadEventListener.GET); - success = true; - return event.getResult(); - } - finally { - afterOperation(success); - } + return this.byId(entityName).load(id); } /** @@ -910,75 +889,67 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc } public Object load(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { - return load( entityClass.getName(), id, lockMode ); + return this.byId(entityClass).with(lockMode).getReference(id); } public Object load(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException { - return load( entityClass.getName(), id, lockOptions ); + return this.byId(entityClass).with(lockOptions).getReference(id); } public Object load(String entityName, Serializable id, LockMode lockMode) throws HibernateException { - LoadEvent event = new LoadEvent(id, entityName, lockMode, this); - fireLoad( event, LoadEventListener.LOAD ); - return event.getResult(); + return this.byId(entityName).with(lockMode).getReference(id); } public Object load(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException { - LoadEvent event = new LoadEvent(id, entityName, lockOptions, this); - fireLoad( event, LoadEventListener.LOAD ); - return event.getResult(); + return this.byId(entityName).with(lockOptions).getReference(id); } public Object get(Class entityClass, Serializable id, LockMode lockMode) throws HibernateException { - return get( entityClass.getName(), id, lockMode ); + return this.byId(entityClass).with(lockMode).load(id); } public Object get(Class entityClass, Serializable id, LockOptions lockOptions) throws HibernateException { - return get( entityClass.getName(), id, lockOptions ); + return this.byId(entityClass).with(lockOptions).load(id); } public Object get(String entityName, Serializable id, LockMode lockMode) throws HibernateException { - LoadEvent event = new LoadEvent(id, entityName, lockMode, this); - fireLoad(event, LoadEventListener.GET); - return event.getResult(); + return this.byId(entityName).with(lockMode).load(id); } public Object get(String entityName, Serializable id, LockOptions lockOptions) throws HibernateException { - LoadEvent event = new LoadEvent(id, entityName, lockOptions, this); - fireLoad( event, LoadEventListener.GET ); - return event.getResult(); + return this.byId(entityName).with(lockOptions).load(id); } /* (non-Javadoc) * @see org.hibernate.Session#byId(java.lang.String) */ @Override - public IdentifierLoadAccess byId(String entityName) { - return new IdentifierLoadAccessByName(entityName); + public IdentifierLoadAccessImpl byId(String entityName) { + return new IdentifierLoadAccessImpl(entityName, Object.class); } /* (non-Javadoc) * @see org.hibernate.Session#byId(java.lang.Class) */ @Override - public IdentifierLoadAccess byId(Class entityClass) { - return new IdentifierLoadAccessByClass(entityClass); + public IdentifierLoadAccessImpl byId(Class entityClass) { + return new IdentifierLoadAccessImpl(entityClass.getName(), entityClass); } /* (non-Javadoc) - * @see org.hibernate.Session#byNaturalKey(java.lang.String) + * @see org.hibernate.Session#byNaturalId(java.lang.String) */ @Override - public NaturalIdLoadAccess byNaturalKey(String entityName) { - return new NaturalIdLoadAccessByName(entityName); + public NaturalIdLoadAccess byNaturalId(String entityName) { + return new NaturalIdLoadAccessImpl(entityName, Object.class); } /* (non-Javadoc) - * @see org.hibernate.Session#byNaturalKey(java.lang.Class) + * @see org.hibernate.Session#byNaturalId(java.lang.Class) */ @Override - public NaturalIdLoadAccess byNaturalKey(Class entityClass) { - return new NaturalIdLoadAccessByClass(entityClass); + public NaturalIdLoadAccess byNaturalId(Class entityClass) { + return new NaturalIdLoadAccessImpl(entityClass.getName(), entityClass); } private void fireLoad(LoadEvent event, LoadType loadType) { @@ -2199,28 +2170,76 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc } } - private abstract class AbstractIdentifierLoadAccess implements IdentifierLoadAccess { + private class IdentifierLoadAccessImpl implements IdentifierLoadAccess { + 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; + this.entityClass = entityClass; + } /* (non-Javadoc) * @see org.hibernate.IdentifierLoadAccess#with(org.hibernate.LockOptions) */ @Override - public final IdentifierLoadAccess with(LockOptions lockOptions) { + 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; } + /** + * Support for legacy {@link Session#load(Class, Serializable, LockMode)} and {@link Session#load(String, Serializable, LockMode) + * @deprecated + */ + @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; + return this; + } + /* (non-Javadoc) * @see org.hibernate.IdentifierLoadAccess#getReference(java.io.Serializable) */ @Override public final T getReference(Serializable id) { if (this.lockOptions != null) { - return getReferenceInternal(id, lockOptions); + LoadEvent event = new LoadEvent(id, entityName, lockOptions, SessionImpl.this); + fireLoad( event, LoadEventListener.LOAD ); + return this.entityClass.cast(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()); } - return getReferenceInternal(id); + LoadEvent event = new LoadEvent(id, entityName, false, SessionImpl.this); + boolean success = false; + try { + fireLoad( event, LoadEventListener.LOAD ); + if ( event.getResult() == null ) { + getFactory().getEntityNotFoundDelegate().handleEntityNotFound( entityName, id ); + } + success = true; + return this.entityClass.cast(event.getResult()); + } + finally { + afterOperation(success); + } } /* (non-Javadoc) @@ -2229,102 +2248,43 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc @Override public final T load(Serializable id) { if (this.lockOptions != null) { - return loadInternal(id, lockOptions); + 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 loadInternal(id); - } - - protected abstract T getReferenceInternal(Serializable id, LockOptions lockOptions); - protected abstract T getReferenceInternal(Serializable id); - protected abstract T loadInternal(Serializable id, LockOptions lockOptions); - protected abstract T loadInternal(Serializable id); - } - - private class IdentifierLoadAccessByName extends AbstractIdentifierLoadAccess { - private final String entityName; - - public IdentifierLoadAccessByName(String entityName) { - this.entityName = entityName; - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#getReferenceInternal(java.io.Serializable, org.hibernate.LockOptions) - */ - @Override - protected Object getReferenceInternal(Serializable id, LockOptions lockOptions) { - return SessionImpl.this.load(entityName, id, lockOptions); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#getReferenceInternal(java.io.Serializable) - */ - @Override - protected Object getReferenceInternal(Serializable id) { - return SessionImpl.this.load(entityName, id); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#loadInternal(java.io.Serializable, org.hibernate.LockOptions) - */ - @Override - protected Object loadInternal(Serializable id, LockOptions lockOptions) { - return SessionImpl.this.get(entityName, id, lockOptions); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#loadInternal(java.io.Serializable) - */ - @Override - protected Object loadInternal(Serializable id) { - return SessionImpl.this.get(entityName, id); + LoadEvent event = new LoadEvent(id, entityName, false, SessionImpl.this); + boolean success = false; + try { + fireLoad(event, LoadEventListener.GET); + success = true; + return this.entityClass.cast(event.getResult()); + } + finally { + afterOperation(success); + } } } - private class IdentifierLoadAccessByClass extends AbstractIdentifierLoadAccess { + private class NaturalIdLoadAccessImpl implements NaturalIdLoadAccess { + private final String entityName; private final Class entityClass; - - public IdentifierLoadAccessByClass(Class entityName) { - this.entityClass = entityName; - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#getReferenceInternal(java.io.Serializable, org.hibernate.LockOptions) - */ - @Override - protected T getReferenceInternal(Serializable id, LockOptions lockOptions) { - return entityClass.cast(SessionImpl.this.load(entityClass, id, lockOptions)); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#getReferenceInternal(java.io.Serializable) - */ - @Override - protected T getReferenceInternal(Serializable id) { - return entityClass.cast(SessionImpl.this.load(entityClass, id)); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#loadInternal(java.io.Serializable, org.hibernate.LockOptions) - */ - @Override - protected T loadInternal(Serializable id, LockOptions lockOptions) { - return entityClass.cast(SessionImpl.this.get(entityClass, id, lockOptions)); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractIdentifierLoadAccess#loadInternal(java.io.Serializable) - */ - @Override - protected T loadInternal(Serializable id) { - return entityClass.cast(SessionImpl.this.get(entityClass, id)); - } - } - - - private abstract class AbstractNaturalIdLoadAccess implements NaturalIdLoadAccess { private final Map naturalIdParameters = new LinkedHashMap(); private LockOptions lockOptions; + + /** + * Note that the specified entity MUST be castable using {@link Class#cast(Object)} + * on the specified entityClass + */ + private NaturalIdLoadAccessImpl(String entityName, Class entityClass) { + this.entityName = entityName; + this.entityClass = entityClass; + } /* (non-Javadoc) * @see org.hibernate.IdentifierLoadAccess#with(org.hibernate.LockOptions) @@ -2350,10 +2310,12 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc @Override public final T getReference() { if (this.lockOptions != null) { - return getReferenceInternal(naturalIdParameters, lockOptions); + //TODO } - return getReferenceInternal(naturalIdParameters); + //TODO + + throw new UnsupportedOperationException(); } /* (non-Javadoc) @@ -2362,95 +2324,12 @@ public final class SessionImpl extends AbstractSessionImpl implements EventSourc @Override public final T load() { if (this.lockOptions != null) { - return loadInternal(naturalIdParameters, lockOptions); + //TODO } - return loadInternal(naturalIdParameters); - } - - protected abstract T getReferenceInternal(Map naturalIdParameters, LockOptions lockOptions); - protected abstract T getReferenceInternal(Map naturalIdParameters); - protected abstract T loadInternal(Map naturalIdParameters, LockOptions lockOptions); - protected abstract T loadInternal(Map naturalIdParameters); - } - - private class NaturalIdLoadAccessByName extends AbstractNaturalIdLoadAccess { - private final String entityName; - - public NaturalIdLoadAccessByName(String entityName) { - this.entityName = entityName; - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#getReferenceInternal(java.util.Map, org.hibernate.LockOptions) - */ - @Override - protected Object getReferenceInternal(Map naturalIdParameters, LockOptions lockOptions) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#getReferenceInternal(java.util.Map) - */ - @Override - protected Object getReferenceInternal(Map naturalIdParameters) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#loadInternal(java.util.Map, org.hibernate.LockOptions) - */ - @Override - protected Object loadInternal(Map naturalIdParameters, LockOptions lockOptions) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#loadInternal(java.util.Map) - */ - @Override - protected Object loadInternal(Map naturalIdParameters) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - } - - private class NaturalIdLoadAccessByClass extends AbstractNaturalIdLoadAccess { - private final Class entityClass; - - public NaturalIdLoadAccessByClass(Class entityName) { - this.entityClass = entityName; - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#getReferenceInternal(java.util.Map, org.hibernate.LockOptions) - */ - @Override - protected T getReferenceInternal(Map naturalIdParameters, LockOptions lockOptions) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#getReferenceInternal(java.util.Map) - */ - @Override - protected T getReferenceInternal(Map naturalIdParameters) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#loadInternal(java.util.Map, org.hibernate.LockOptions) - */ - @Override - protected T loadInternal(Map naturalIdParameters, LockOptions lockOptions) { - throw new UnsupportedOperationException("Not Implemented Yet"); - } - - /* (non-Javadoc) - * @see org.hibernate.internal.SessionImpl.AbstractNaturalIdLoadAccess#loadInternal(java.util.Map) - */ - @Override - protected T loadInternal(Map naturalIdParameters) { - throw new UnsupportedOperationException("Not Implemented Yet"); + //TODO + + throw new UnsupportedOperationException(); } } }