Issue Details (XML | Word | Printable)

Key: HHH-3108
Type: Bug Bug
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Marcin Mościcki
Votes: 1
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
Hibernate Core

<load-collection role="entity_name.property"> doesn't work

Created: 14/Feb/08 09:28 AM   Updated: 26/Mar/08 03:15 AM
Component/s: metamodel
Affects Version/s: 3.2.6
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. Zip Archive hibernatebug.zip (21 kB)
2. Java Source File ResultSetMappingBinder.java (14 kB)

Environment: Hibernate 3.2.6, Oracle 9 (insignificant)


 Description  « Hide
I use an <sql-query> for a <loader> in a collection owned by a named entity. The role attribute is set to "<entity-name>.<property-name>" - exactly the key under witch the collection's meta data is registered in SessionFactoryImpl.collectionPersisters map - but when the query is being added, SessionFactoryImpl looks for it under the key "<package>.<entity-name>.<property-name>" in this map, and doesn't find it.


When entity-name attribute is removed from the entity mapping and unqualified class name is used to refer to it instead, everything works fine.
The easiest, and probably most uniform solution would be to introduce a second attribute "entity-role" to the load-collection element.


The following exception results from mapping:

<hibernate-mapping package="hibernatedemo.model">
....
<class table="persons_test" entity-name="person" name="Person">
....
        <set name="personMap" inverse="true">
<key/>
<one-to-many entity-name="person_map"/>
<loader query-ref="fetch_person_map_by_person"/>
</set>
</class>

<sql-query name="fetch_person_map_by_person">
     <load-collection alias="map" role="person.personMap" />
select {map.*} from persons_map_test map where map.person_id=:id
</sql-query>


org.hibernate.MappingException: Unknown collection role: hibernatedemo.model.person.personMap
at org.hibernate.impl.SessionFactoryImpl.getCollectionPersister(SessionFactoryImpl.java:558)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.addCollection(SQLQueryReturnProcessor.java:370)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processCollectionReturn(SQLQueryReturnProcessor.java:405)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:335)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:148)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:64)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:114)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:446)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:352)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
at hibernatedemo.Test.getHibernateSession(Test.java:44)
at hibernatedemo.Test.main(Test.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)

 All   Comments   Work Log   Change History   FishEye      Sort Order: Ascending order - Click to sort in descending order
Marcin Mościcki added a comment - 14/Feb/08 09:44 AM
> When entity-name attribute is removed from the entity mapping and unqualified class name is used to refer to it instead, everything works fine.

I meant here that there is no exception thrown - the collection returned is incorrectly empty, but I believe there already is a bug report on that.

Marcin Mościcki added a comment - 14/Feb/08 10:50 AM
I attach a simple test case to show the problem.
Contains two classes - Parent and Child, tested using three mappings:
1) custom sql-query as a set loader using entity names: unresolved collection exception
2) custom sql-query as a set loader using class names: returns empty collections
3) standard mapping without a specific loader as a reference: works ok.

db.sql contains schema with two tables and example data. Run on an Oracle database.

Maybe 2) is the same as HHH-420, or maybe it's not even a bug, but it looks just as in hibernate reference...

Marcin Mościcki added a comment - 18/Feb/08 12:03 PM
Ok,
the problem with issue 2) (empty collection) was caused by missing 'column' attribute for <key> element inside the <set>.
This is, however, how it was presented in the hibernate docs...
Issue 1) remains - I'll have a deeper look at it later.

Marcin Mościcki added a comment - 14/Mar/08 11:20 AM
Ok, I think I fixed it:
In org.hibernate.cfg.ResultSetMappingBinder, the entity name of the parent side of the relationship was automatically
prefixed with default package:

    String ownerClassName = HbmBinder.getClassName(collectionAttribute.substring( 0, dot ), mappings );

I changed this to:
String ownerClassName = collectionAttribute.substring( 0, dot );
if (mappings.getClass(ownerClassName)==null) {
ownerClassName = HbmBinder.getClassName( ownerClassName, mappings );
}

Below there's a comment:
//FIXME: get the PersistentClass
java.util.Map propertyResults = bindPropertyResults(alias, returnElem, null, mappings );

which might have alluded to this, or some other issue, I don't know.
Changed file attached - I used official 3.2.6.ga version available in bundled distribution - if you'd like a patch, please let me know to which tag.

Marcin Mościcki added a comment - 14/Mar/08 11:23 AM
Fix for the problem of registering a named sql query for <default-package>.<owning-entity-name>.<property-name>.
Based on sources bundles with 3.2.6.ga

Max Rydahl Andersen added a comment - 26/Mar/08 03:15 AM
have you looked at the existing test cases for native sql query usage ?

Isn't there a test for this exact thing that actually passes?