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.  [ 6 posts ] 
Author Message
 Post subject: 837 list elements loaded for an 8 element collection
PostPosted: Wed Jun 01, 2005 10:26 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Hi,

I am fetching a list that should return 8 elements but getting back a collection with 837 elements. 8 of those elements are the correct non-null instances and the rest are null elements.

The SQL looks right, the SQL runs in MSSQL with only 8 rows returned, so quite how 837 is getting into my collection I am unsure!

Hibernate version:

3.0

Mapping documents:

Code:
<list name="updates" inverse="true" lazy="true" cascade="all">
            <key column="schedule_id" />
         <index column="update_id" />
            <one-to-many class="com.qas.newmedia.intranet.schedules.dto.ScheduleUpdate"/>
        </list>

Code between sessionFactory.openSession() and session.close():

We use the hibernate template in Spring.

Code:
      Schedule schedule = (Schedule)
         getHibernateTemplate().load(Schedule.class, id);
      
      getHibernateTemplate().initialize(schedule.getUpdates());
      
      for (ScheduleUpdate u : schedule.getUpdates()) {
         logger.debug("update=" + u);
      }   


Full stack trace of any exception that occurs:

None

Name and version of the database you are using:

MSQL 2K

The generated SQL (show_sql=true):

From this output, it's clear there is no problem with the SQL. I've run this in MSSQL with the update ID and it loads fine and the update count is in fact 8, not 837.

Code:
Hibernate: select schedule0_.schedule_id as schedule1_0_, schedule0_.owner as owner31_0_, schedule0_.associates as associates31_0_, schedule0_.status as status31_0_, schedule0_.title as title31_0_, schedule0_.summary as summary31_0_, schedule0_.created as created31_0_, schedule0_.last_updated as last9_31_0_, schedule0_.development_signoff as develop10_31_0_, schedule0_.development_signoff_format as develop11_31_0_, schedule0_.qat_signoff as qat12_31_0_, schedule0_.qat_signoff_format as qat13_31_0_, schedule0_.launch as launch31_0_, schedule0_.launch_format as launch15_31_0_, schedule0_.product_release as product16_31_0_, schedule0_.data_in_house as data17_31_0_, schedule0_.data_in_house_format as data18_31_0_, schedule0_.data_reviewed as data19_31_0_, schedule0_.data_reviewed_format as data20_31_0_, schedule0_.contract_reviewed as contract21_31_0_, schedule0_.contract_reviewed_format as contract22_31_0_, schedule0_.contract_signed as contract23_31_0_, schedule0_.contract_signed_format as contract24_31_0_, schedule0_.approval as approval31_0_, schedule0_.approval_format as approval26_31_0_, schedule0_.target_completion as target27_31_0_, schedule0_.target_completion_format as target28_31_0_, schedule0_.roi as roi31_0_, schedule0_.implementor as impleme30_31_0_, schedule0_.owning_department as owning31_31_0_, schedule0_.target_start as target32_31_0_, schedule0_.target_start_format as target33_31_0_, schedule0_.actual_start as actual34_31_0_, schedule0_.actual_start_format as actual35_31_0_, schedule0_.initial_completion as initial36_31_0_, schedule0_.initial_completion_format as initial37_31_0_, schedule0_.actual_completion as actual38_31_0_, schedule0_.actual_completion_format as actual39_31_0_, schedule0_.pm_score as pm40_31_0_, schedule0_.del_score as del41_31_0_, schedule0_.schedule_type as schedule2_0_ from dbSchedules.dbo.tbl_Schedules schedule0_ where schedule0_.schedule_id=?

Code:
Hibernate: select updates0_.schedule_id as schedule6___, updates0_.update_id as update1___, updates0_.update_id as update1_0_, updates0_.created as created34_0_, updates0_.author as author34_0_, updates0_.comment as comment34_0_, updates0_.internal as internal34_0_, updates0_.schedule_id as schedule6_34_0_ from dbSchedules.dbo.tbl_ScheduleUpdates updates0_ where updates0_.schedule_id=?


Debug level Hibernate log excerpt:

Code:
2005-06-01 15:08:34,687 - DEBUG (org.hibernate.engine.TwoPhaseLoad:96) - resolving associations for [com.qas.newmedia.intranet.schedules.dto.ProductSchedule#494]
2005-06-01 15:08:34,687 - DEBUG (org.hibernate.engine.CollectionLoadContext:134) - creating collection wrapper:[com.qas.newmedia.intranet.schedules.dto.Schedule.milestones#494]
2005-06-01 15:08:34,702 - DEBUG (org.hibernate.engine.CollectionLoadContext:134) - creating collection wrapper:[com.qas.newmedia.intranet.schedules.dto.Schedule.updates#494]
2005-06-01 15:08:34,702 - DEBUG (org.hibernate.engine.CollectionLoadContext:134) - creating collection wrapper:[com.qas.newmedia.intranet.schedules.dto.Schedule.links#494]
2005-06-01 15:08:34,702 - DEBUG (org.hibernate.engine.CollectionLoadContext:134) - creating collection wrapper:[com.qas.newmedia.intranet.schedules.dto.Schedule.subscribers#494]
2005-06-01 15:08:34,702 - DEBUG (org.hibernate.engine.TwoPhaseLoad:167) - done materializing entity [com.qas.newmedia.intranet.schedules.dto.ProductSchedule#494]
2005-06-01 15:08:34,702 - DEBUG (org.hibernate.engine.PersistenceContext:738) - initializing non-lazy collections
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.loader.Loader:1278) - done entity load
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.event.def.DefaultInitializeCollectionEventListener:42) - initializing collection [com.qas.newmedia.intranet.schedules.dto.Schedule.updates#494]
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.event.def.DefaultInitializeCollectionEventListener:47) - checking second-level cache
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.event.def.DefaultInitializeCollectionEventListener:59) - collection not cached
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.loader.Loader:1336) - batch loading collection: [com.qas.newmedia.intranet.schedules.dto.Schedule.updates#494]
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.jdbc.AbstractBatcher:258) - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.jdbc.AbstractBatcher:343) - preparing statement
2005-06-01 15:08:34,718 - DEBUG (org.hibernate.type.NullableType:59) - binding '494' to parameter: 1
2005-06-01 15:08:34,734 - DEBUG (org.hibernate.jdbc.AbstractBatcher:274) - about to open ResultSet (open ResultSets: 0, globally: 0)
2005-06-01 15:08:34,734 - DEBUG (org.hibernate.loader.Loader:610) - result set contains (possibly empty) collection: [com.qas.newmedia.intranet.schedules.dto.Schedule.updates#494]
2005-06-01 15:08:34,734 - DEBUG (org.hibernate.engine.CollectionLoadContext:78) - uninitialized collection: initializing
2005-06-01 15:08:34,734 - DEBUG (org.hibernate.loader.Loader:377) - processing result set
2005-06-01 15:08:34,749 - DEBUG (org.hibernate.loader.Loader:382) - result set row: 0
2005-06-01 15:08:34,749 - DEBUG (org.hibernate.type.NullableType:86) - returning '807' as column: update1_0_


and so on until row 8

Code:
2005-06-01 15:08:34,905 - DEBUG (org.hibernate.jdbc.AbstractBatcher:363) - closing statement
2005-06-01 15:08:34,905 - DEBUG (org.hibernate.loader.Loader:450) - total objects hydrated: 8


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 10:50 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
That's just how <list> works. I think that it's so that you can use "list.get(primaryKey)" in your code and get back the correct object. (If someone could confirm or deny this, I would greatly appreciate it.)

The FAQ kind-of-almost covers this, but it skirts around the part where you'll end up with null objects in your List.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 10:54 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
I'm not so certain about that.

We've 5 other applications running off Hibernate and they load lists of the correct length. This is only an issue since trying to use initialize rather than a direct eager fetch query.

We rely on a correct count in many areas so we can summarise to the user how many items are present and also iteration does not end up printing a load of empty table rows.

Hmm. Do you have the precise FAQ location?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 11:08 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
http://www.hibernate.org/117.html#A5

When I first tried out Hibernate (3.0) I noticed that <list> inserted null values to fill in blanks between primary keys. I guess I haven't seen it work otherwise, but if you're saying that it does then I'm confused now.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 01, 2005 11:13 am 
Senior
Senior

Joined: Fri Jun 18, 2004 10:17 am
Posts: 140
Maybe it's because I misuse the id for the list index rather than a separate column then.

In H2 it did not create null elements, but perhaps you are right with this being a shift to H3. Everywhere else we are actually using a separate column so this must be a migration issue.

As a dirty fix I may have to programmatically remove these null elements but first I will see if I can specify an index colum and list-index.

Thanks, Allistair.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 24, 2005 1:28 pm 
Newbie

Joined: Fri Oct 29, 2004 4:20 pm
Posts: 16
Location: Grand Rapids, MI
Try using <bag...> instead of list and you'll get an 8 element collection without nulls.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.