-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: <load-collection> in <sql-query> doesn't add obj
PostPosted: Wed Mar 02, 2005 6:24 pm 
Newbie

Joined: Sun Oct 10, 2004 2:19 pm
Posts: 7
Hi,

I have the following problem. I defined below mappings for a SQL query that would normally be like:

select r.*, l.* from reports r, locations l where r.id=? and (r.location=l.location or substring(r.location,1,6)=l.location or substring(r.location,1,2)=l.location or substring(r.location,1,2)=l.location)

Hence there's no ordinary foreign-key constraint between report.location and locations.location.

I ended up with two queries: one for the Report, and one for the (multiple) locations to be found. Imagine the locations to be hierarchical where 2 digits map to a state, 4 digits to a city and 6 digits to a neighbourhood. Additionally one location can have multiple names.

Everything works just fine, except that the report.getLocations() in my Java code below returns a List with 0 elements, wherease the SQL statement returns 3 rows (Location objects).

Some poking around with a debugger shows that when trying to add members to the list org.hibernate.engine.TwoPhaseLoad.initializeEntity's call to Type[] types = persister.getPropertyTypes() (line 101) returns a zero-length Type-array and hence no properties of my Location object are copied. I expected this array to be of size 2.

Is this a bug?

Regards,


Jeroen

Hibernate version:
3.0RC

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.dutchbirding.rarebirds">
<class name="Report" table="reports" proxy="Report">
<id name="id">
<generator class="native">
<param name="sequence">seq_reports</param>
</generator>
</id>

<bag name="locations" inverse="true">
<key column="location"/>
<one-to-many class="Location"/>
<loader query-ref="query-locations"/>
</bag>
<!-- More property and many-to-one elements that are not important -->
</class>

<sql-query name="query-locations">
<load-collection alias="l" role="Report.locations"/>
select {l.*} from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2))
</sql-query>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="nl.dutchbirding.rarebirds">
<class name="Location" table="locations" proxy="Location">
<composite-id>
<key-property name="location"/>
<key-property name="name"/>
</composite-id>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Report report = (Report) session.get(Report.class, 8034);
List list = report.getLocations();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
Location location = (Location) list.get(i);
System.out.println("- " + location.getName());
}
}

Full stack trace of any exception that occurs:
None

Name and version of the database you are using:
PostgreSQL 8.0

The generated SQL (show_sql=true):
select l.location as location__, l.name as name__, l.location as location0_, l.name as name0_ from reports r, locations l where r.id=? and l.location in (r.location, substring(r.location,1,6), substring(r.location,1,4), substring(r.location,1,2))

Debug level Hibernate log excerpt:
10936 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - preparing statement
10946 [main] DEBUG org.hibernate.type.IntegerType - binding '8034' to parameter: 1
14281 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to open ResultSet (open ResultSets: 0, globally: 0)
14281 [main] DEBUG org.hibernate.loader.Loader - result set contains (possibly empty) collection: [nl.dutchbirding.rarebirds.Report.locations#8034]
14281 [main] DEBUG org.hibernate.engine.CollectionLoadContext - uninitialized collection: initializing
14281 [main] DEBUG org.hibernate.loader.Loader - processing result set
14281 [main] DEBUG org.hibernate.loader.Loader - result set row: 0
14281 [main] DEBUG org.hibernate.type.StringType - returning '02' as column: location0_
14281 [main] DEBUG org.hibernate.type.StringType - returning 'Schiermonnikoog' as column: name0_
14281 [main] DEBUG org.hibernate.loader.Loader - result row: EntityKey[nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14291 [main] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14291 [main] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14291 [main] DEBUG org.hibernate.type.IntegerType - returning '2' as column: location__
14291 [main] DEBUG org.hibernate.loader.Loader - found row of collection: [nl.dutchbirding.rarebirds.Report.locations#2]
14291 [main] DEBUG org.hibernate.engine.CollectionLoadContext - new collection: instantiating
14291 [main] DEBUG org.hibernate.type.StringType - returning '02' as column: location__
14291 [main] DEBUG org.hibernate.type.StringType - returning 'Schiermonnikoog' as column: name__
14291 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14291 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache
14291 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14291 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14291 [main] DEBUG org.hibernate.loader.Loader - result set row: 1
14291 [main] DEBUG org.hibernate.type.StringType - returning '025435' as column: location0_
14291 [main] DEBUG org.hibernate.type.StringType - returning 'Nes gemeente dongeradeel' as column: name0_
14291 [main] DEBUG org.hibernate.loader.Loader - result row: EntityKey[nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14291 [main] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14301 [main] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14301 [main] DEBUG org.hibernate.type.IntegerType - returning '25435' as column: location__
14301 [main] DEBUG org.hibernate.loader.Loader - found row of collection: [nl.dutchbirding.rarebirds.Report.locations#25435]
14301 [main] DEBUG org.hibernate.engine.CollectionLoadContext - new collection: instantiating
14301 [main] DEBUG org.hibernate.type.StringType - returning '025435' as column: location__
14301 [main] DEBUG org.hibernate.type.StringType - returning 'Nes gemeente dongeradeel' as column: name__
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14301 [main] DEBUG org.hibernate.loader.Loader - result set row: 2
14301 [main] DEBUG org.hibernate.type.StringType - returning '025435' as column: location0_
14301 [main] DEBUG org.hibernate.type.StringType - returning 'Nes waddenkust' as column: name0_
14301 [main] DEBUG org.hibernate.loader.Loader - result row: EntityKey[nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14301 [main] DEBUG org.hibernate.loader.Loader - Initializing object from ResultSet: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14301 [main] DEBUG org.hibernate.persister.entity.BasicEntityPersister - Hydrating entity: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14301 [main] DEBUG org.hibernate.type.IntegerType - returning '25435' as column: location__
14301 [main] DEBUG org.hibernate.loader.Loader - found row of collection: [nl.dutchbirding.rarebirds.Report.locations#25435]
14301 [main] DEBUG org.hibernate.engine.CollectionLoadContext - reading row
14301 [main] DEBUG org.hibernate.type.StringType - returning '025435' as column: location__
14301 [main] DEBUG org.hibernate.type.StringType - returning 'Nes waddenkust' as column: name__
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - loading entity: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - entity found in session cache
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - attempting to resolve: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14301 [main] DEBUG org.hibernate.event.def.DefaultLoadEventListener - resolved object in session cache: [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14301 [main] DEBUG org.hibernate.loader.Loader - done processing result set (3 rows)
14301 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close ResultSet (open ResultSets: 1, globally: 1)
14311 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
14311 [main] DEBUG org.hibernate.jdbc.AbstractBatcher - closing statement
14311 [main] DEBUG org.hibernate.loader.Loader - total objects hydrated: 3
14311 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14311 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [nl.dutchbirding.rarebirds.Location#component[location,name]{location=02, name=Schiermonnikoog}]
14311 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14311 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes gemeente dongeradeel}]
14311 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - resolving associations for [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14311 [main] DEBUG org.hibernate.engine.TwoPhaseLoad - done materializing entity [nl.dutchbirding.rarebirds.Location#component[location,name]{location=025435, name=Nes waddenkust}]
14311 [main] DEBUG org.hibernate.engine.CollectionLoadContext - 3 collections were found in result set
19498 [main] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [nl.dutchbirding.rarebirds.Report.locations#25435]
20009 [main] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [nl.dutchbirding.rarebirds.Report.locations#2]
20480 [main] DEBUG org.hibernate.engine.CollectionLoadContext - collection fully initialized: [nl.dutchbirding.rarebirds.Report.locations#8034]
20480 [main] DEBUG org.hibernate.engine.CollectionLoadContext - 3 collections initialized
20480 [main] DEBUG org.hibernate.engine.PersistenceContext - initializing non-lazy collections


Top
 Profile  
 
 Post subject: RE: <load-collection> in <sql-query> doesn't add
PostPosted: Thu Mar 10, 2005 7:09 am 
Newbie

Joined: Sun Oct 10, 2004 2:19 pm
Posts: 7
Still no takers.

Should I open a JIRA issue?

Regards,


Jeroen


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 02, 2007 10:43 am 
Newbie

Joined: Fri Nov 03, 2006 5:21 am
Posts: 14
Hi,

I do have exactely th esame problem.
The custom loader retruns 2 elements that get hydrated (DEBUG org.hibernate.loader.Loader:839 - total objects hydrated: 2) but the collection is empty.

I found this problem several times in this Forum, but never an answer.

Does anybody have an idea what is actually happening here?

Regards,
Joern


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.