Session.load(Class, id) fails with MappingException if entity names mapped

Description

If map an entity with entity-name Session.load(Class clazz, Serializable id) fails with MappingException:

org.hibernate.MappingException: Unknown entity: test.AImpl
at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:580)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:91)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:822)
at org.hibernate.impl.SessionImpl.load(SessionImpl.java:815)
...

load(String entityName, Serializable id) works though.

It also appears inconsistent with e.g. Session.createCriteria(...) which, in its various forms, works for entity names, class names or classes. (On a separate note, the entity name override of createCriteria can be slow as a result of its reliance on a thrown ClassNotFoundException (at least with the locking behaviour of the JBoss RepositoryClassLoader)).

A simple test case is attached.

Environment

Windows XP

Activity

Show:
Jeremy Stone
April 8, 2014, 4:20 PM

Yes, I see that a class should be mappable to multiple tables: which explains the exception.

However I would have thought that the following would be essentially equivalent:
session.createCriteria(AImpl.class).add(Restrictions.idEq(1)).uniqueResult()
session.load(AImpl.class, 1)

(not that I would typically go for the former) ...but I guess that's not the case.

It is a little confusing that createCriteria and load/get appear in the API accepting both a string and a class to determine the 'type of entity' to load but the meaning in each case is subtly different. Maybe a comment in the API doc is warranted (or am I the only one to be caught by this?).

Thanks.

Steve Ebersole
April 9, 2014, 5:53 PM

So here are some pertinent copied javadocs:

Session.java

SharedSessionContract.java

So the #createCriteria docs do do a decent job of denoting the difference in my opinion. If you'd like to contribute some different wording that you think makes the difference more pronounced, I'm certainly ok with that..

Jeremy Stone
April 10, 2014, 8:39 AM

OK...
(1) Doc for load(String entityName, Serializable id) says:
...
@param entityName a persistent class
...
Presumably this should say (as is the case for the corresponding get(String, Serializable) method):
...
@param entityName The entity name
...

(2) Maybe add a note for the get(Class,...) and load(Class...) overloads against the clazz/theClass parameter, for example:
...
@param clazz a persistent class. Note that if the persistent class is mapped with an entity-name, {@link #get(String, java.io.Serializable)} should instead be used, passing the specific entity name (and not the class name).
...

[Not sure how all this plays out with annotation mapped classes where each @Entity annotated class that can have only a single entity 'name'. I guess that's another story.]

Steve Ebersole
October 27, 2015, 7:16 PM

This bug report does not indicate that the reported issue affects version 5.x. Versions prior to 5.x are no longer maintained. It would be a great help to the Hibernate team and community for someone to verify that the reported issue still affects version 5.x. If so, please add the 5.x version that you verified with to the list of affected-versions and attach the (preferably SSCCE) test case you used to do the verification to the report; from there the issues will be looked at during our triage meetings.

For details, see http://in.relation.to/2015/10/27/great-jira-cleanup-2015/

Steve Ebersole
October 28, 2015, 3:26 AM

As part of verifying that this issue affects 5.0, please just set the "Affects version". Leave the "verify-affects-5.0" label and leave the issue in "Awaiting Response" status; these are critical for us to be able to track these verifications and triage them. Thanks.

Assignee

Unassigned

Reporter

Jeremy Stone

Fix versions

None

Labels

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure