-->
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.  [ 18 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Set one-to-many pulling back size of 1 every time.
PostPosted: Wed Sep 07, 2005 5:23 pm 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
I am having a problem with using set mapping in hibernate 3.0
I have a relationship between a sdrl and a schedule.
The sdrl can have multiple schedules.
I am trying to use a set to get the schedules loaded into the sdrl.
Inside my sdrl.xml file I have this:
<set name="schedules" inverse="true" lazy="true">
<key column="sdrl_id"></key>
<one-to-many class="IdmsScheduleTable" />
</set>
Inside my schedule.xml file I have this:
<many-to-one name="sdrl" column="sdrl_id" class="IdmsSdrlTable" not-null="true" />

The code to initialize the sdrl/schedules looks like this:
Transaction tx = s.beginTransaction();
IdmsSdrlTable sdrl = (IdmsSdrlTable)s.load(IdmsSdrlTable.class, sdrl_id);
Hibernate.initialize(sdrl.getSchedules());
tx.commit();
s.close();
System.out.println("sdrl sched="+sdrl.getSchedules().size());

My problem is even when I have multiple schedules on the sdrl it always pulls back 1 schedule in the set. I have spent alot of time trying to fix this with no luck. Anyone have an idea why sdrl would always pull back a set of 1 schedule?

Thanks,
Dave

Hibernate version: Hibernate 3.0

Code between sessionFactory.openSession() and session.close():
Transaction tx = s.beginTransaction();
IdmsSdrlTable sdrl = (IdmsSdrlTable)s.load(IdmsSdrlTable.class, sdrl_id);
Hibernate.initialize(sdrl.getSchedules());
tx.commit();
s.close();
System.out.println("sdrl sched="+sdrl.getSchedules().size());

Full stack trace of any exception that occurs: No exception

Name and version of the database you are using: Oracle 9i

The generated SQL (show_sql=true):
Hibernate: select idmssdrlta0_.SDRL_ID as SDRL1_0_, idmssdrlta0_.DATA_ITEM_DESC as DATA2_11_0_, idmssdrlta0_.CREATE_DATE as CREATE3_11_0_, idmssdrlta0_.UPDATE_DATE as UPDATE4_11_0_, idmssdrlta0_.DATA_MGMT_SIG as DATA5_11_0_, idmssdrlta0_.PRIM_REV_SIG as PRIM6_11_0_, idmssdrlta0_.BUYER_SIG as BUYER7_11_0_, idmssdrlta0_.SCHEDULE_TYPE as SCHEDULE8_11_0_, idmssdrlta0_.REPRODUCE_QTY as REPRODUCE9_11_0_, idmssdrlta0_.NONREPRODUCE_QTY as NONREPR10_11_0_, idmssdrlta0_.TASK_REFERENCE as TASK11_11_0_, idmssdrlta0_.COMMENTS as COMMENTS11_0_, idmssdrlta0_.FIRST_SUBMITTAL as FIRST13_11_0_, idmssdrlta0_.SUBSEQUENT_SUBMITTAL as SUBSEQUENT14_11_0_, idmssdrlta0_.REMARKS as REMARKS11_0_, idmssdrlta0_.TYPE as TYPE11_0_, idmssdrlta0_.STATUS as STATUS11_0_, idmssdrlta0_.ARCHIVE_FLAG as ARCHIVE18_11_0_, idmssdrlta0_.DATA_MGMT_SIG2 as DATA19_11_0_, idmssdrlta0_.NOT_REQUIRED_REASON as NOT20_11_0_, idmssdrlta0_.SDRL_NO as SDRL21_11_0_, idmssdrlta0_.DATA_ITEM as DATA22_11_0_, idmssdrlta0_.PART_ID as PART23_11_0_ from IDMS_SDRL_TABLE idmssdrlta0_ where idmssdrlta0_.SDRL_ID=?
Hibernate: select schedules0_.sdrl_id as sdrl69_1_, schedules0_.PART_ID as PART1_1_, schedules0_.PART_ID as PART1_0_, schedules0_.SDRL_ID as SDRL2_3_0_, schedules0_.SCHEDULE_ID as SCHEDULE3_3_0_, schedules0_.OPEN_DATE as OPEN4_3_0_, schedules0_.CLOSE_DATE as CLOSE5_3_0_, schedules0_.STATUS as STATUS3_0_, schedules0_.SUPPLIER_EST_DUE_DATE as SUPPLIER7_3_0_, schedules0_.SUPPLIER_DUE_DATE as SUPPLIER8_3_0_, schedules0_.SUPPLIER_SLIP_DATE_CNT as SUPPLIER9_3_0_, schedules0_.SUPPLIER_COMPLETE_DATE as SUPPLIER10_3_0_, schedules0_.SUPPLIER_SIG as SUPPLIER11_3_0_, schedules0_.SUPPLIER_ORG_CODE as SUPPLIER12_3_0_, schedules0_.SUPPLIER_STATUS as SUPPLIER13_3_0_, schedules0_.DATA_MGMT_EST_DUE_DATE as DATA14_3_0_, schedules0_.DATA_MGMT_DUE_DATE as DATA15_3_0_, schedules0_.DATA_MGMT_SLIP_DATE_CNT as DATA16_3_0_, schedules0_.DATA_MGMT_COMPLETE_DATE as DATA17_3_0_, schedules0_.DATA_MGMT_SIG as DATA18_3_0_, schedules0_.DATA_MGMT_STATUS as DATA19_3_0_, schedules0_.PRIMARY_EST_DUE_DATE as PRIMARY20_3_0_, schedules0_.PRIMARY_DUE_DATE as PRIMARY21_3_0_, schedules0_.PRIMARY_SLIP_DATE_CNT as PRIMARY22_3_0_, schedules0_.PRIMARY_COMPLETE_DATE as PRIMARY23_3_0_, schedules0_.PRIMARY_SIG as PRIMARY24_3_0_, schedules0_.PRIMARY_STATUS as PRIMARY25_3_0_, schedules0_.BUYER_EST_DUE_DATE as BUYER26_3_0_, schedules0_.BUYER_DUE_DATE as BUYER27_3_0_, schedules0_.BUYER_SLIP_DATE_CNT as BUYER28_3_0_, schedules0_.BUYER_COMPLETE_DATE as BUYER29_3_0_, schedules0_.BUYER_SIG as BUYER30_3_0_, schedules0_.BUYER_STATUS as BUYER31_3_0_, schedules0_.BOEING_EST_DUE_DATE as BOEING32_3_0_, schedules0_.BOEING_DUE_DATE as BOEING33_3_0_, schedules0_.BOEING_COMPLETE_DATE as BOEING34_3_0_, schedules0_.BOEING_STATUS as BOEING35_3_0_, schedules0_.RESUBMIT_DATE as RESUBMIT36_3_0_, schedules0_.RESUBMIT_SCHEDULE_ID as RESUBMIT37_3_0_, schedules0_.DATA_MGMT_EST_DUE_DATE2 as DATA38_3_0_, schedules0_.DATA_MGMT_DUE_DATE2 as DATA39_3_0_, schedules0_.DATA_MGMT_SLIP_DATE_CNT2 as DATA40_3_0_, schedules0_.DATA_MGMT_COMPLETE_DATE2 as DATA41_3_0_, schedules0_.DATA_MGMT_SIG2 as DATA42_3_0_, schedules0_.DATA_MGMT_STATUS2 as DATA43_3_0_, schedules0_.CREATE_HOW as CREATE44_3_0_, schedules0_.CREATED_BY as CREATED45_3_0_, schedules0_.ARCHIVE_FLAG as ARCHIVE46_3_0_, schedules0_.MILESTONE_ID as MILESTONE47_3_0_, schedules0_.EICO_SIG as EICO48_3_0_, schedules0_.EICO_DUE_DATE as EICO49_3_0_, schedules0_.EICO_EST_DUE_DATE as EICO50_3_0_, schedules0_.EICO_SLIP_DATE_CNT as EICO51_3_0_, schedules0_.EICO_STATUS as EICO52_3_0_, schedules0_.EICO_COMPLETE_DATE as EICO53_3_0_, schedules0_.SUPPLIER_ECD_DATE as SUPPLIER54_3_0_, schedules0_.SUPPLIER_ECD_COMMENT as SUPPLIER55_3_0_, schedules0_.SUPPLIER_ECD_CNT as SUPPLIER56_3_0_, schedules0_.DATA_MGMT_ECD_DATE as DATA57_3_0_, schedules0_.DATA_MGMT_ECD_COMMENT as DATA58_3_0_, schedules0_.DATA_MGMT_ECD_CNT as DATA59_3_0_, schedules0_.PRIMARY_ECD_DATE as PRIMARY60_3_0_, schedules0_.PRIMARY_ECD_COMMENT as PRIMARY61_3_0_, schedules0_.PRIMARY_ECD_CNT as PRIMARY62_3_0_, schedules0_.VERIFIER_ECD_DATE as VERIFIER63_3_0_, schedules0_.VERIFIER_ECD_COMMENT as VERIFIER64_3_0_, schedules0_.VERIFIER_ECD_CNT as VERIFIER65_3_0_, schedules0_.BUYER_ECD_DATE as BUYER66_3_0_, schedules0_.BUYER_ECD_COMMENT as BUYER67_3_0_, schedules0_.BUYER_ECD_CNT as BUYER68_3_0_, schedules0_.sdrl_id as sdrl69_3_0_ from IDMS_SCHEDULE_TABLE schedules0_ where schedules0_.sdrl_id=?


Top
 Profile  
 
 Post subject: Re: Set one-to-many pulling back size of 1 every time.
PostPosted: Wed Sep 07, 2005 8:41 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Check to make sure your equals() method has been implemented to properly identify the unique instances in the Set. A Set doesn't allow duplicates.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 10:10 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
My equals was generated by hibernate. It uses the schedule_id to compare which is a primary key for the schedule table.
I changed the equals so that is always returns false to make sure it includes everything. That still did not work.

One more thing, I do not have a foreign key relationship in my database for sdrl_id between the schedule and sdrl table. Would this cause the problem?

Thanks,
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 10:19 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
dnewman3131 wrote:
My equals was generated by hibernate. It uses the schedule_id to compare which is a primary key for the schedule table.
I changed the equals so that is always returns false to make sure it includes everything. That still did not work.

One more thing, I do not have a foreign key relationship in my database for sdrl_id between the schedule and sdrl table. Would this cause the problem?

Thanks,
Dave


The FK relationship in the database shouldn't effect this.

How is your DB defined for these two tables.. Something like this ?

IDMS_SDLR table
-----------------
SDLR_ID
MORE_COLUMNS



IDMS_SCHEDULE table
-----------------------
SCHEDULE_ID
SDLR_ID
MORE_COLUMNS

And you definitely have more than 1 row in the IDMS_SCHEDULE with the same value for SDLR_ID ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 10:22 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
pksiv wrote:
The FK relationship in the database shouldn't effect this.

How is your DB defined for these two tables.. Something like this ?

IDMS_SDLR table
-----------------
SDLR_ID
MORE_COLUMNS



IDMS_SCHEDULE table
-----------------------
SCHEDULE_ID
SDLR_ID
MORE_COLUMNS

And you definitely have more than 1 row in the IDMS_SCHEDULE with the same value for SDLR_ID ?


Correct,
I have a primary key of SDRL_ID in the SDRL table
I have a SDRL_ID in the schedule table
more columns on both.
When I run the generated SQL it is pulling back multiple rows of schedules.
But when I check the set there is only 1 schedule per sdrl.

Thanks,
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 10:28 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I do this all the time and everything you're doing seems correct. Have you tried using a simple Criteria just to see if maybe it's something with the load() behavior ?


Code:
IdmsSdrlTable sdrl = (IdmsSdrlTable)s.createCriteria(IdmsSdrlTable.class).add(Restrictions.idEq(srld_id).uniqueResult();

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 10:54 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
I tried running this:
Session s = HibernateSessionFactory.currentSession();
Transaction tx = s.beginTransaction();
IdmsSdrlTable sdrl = (IdmsSdrlTable)s.createCriteria(IdmsSdrlTable.class).add(Restrictions.idEq(sdrl_id)).uniqueResult();
//IdmsSdrlTable sdrl = (IdmsSdrlTable)s.load(IdmsSdrlTable.class, sdrl_id);
Hibernate.initialize(sdrl.getSchedules());
tx.commit();
s.close();
System.out.println("sdrl sched="+sdrl.getSchedules().size());
return sdrl;

The schedules size was still 1.
When I run the generated sql for the schedules pull it pulls back 2 rows.

Thanks,
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 10:58 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Sorry but I'm stumped. The only thing that makes sense to me is that the Set is removing them but if you schedule.equals() method is correct....

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:01 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
Yeah it seems that it is pulling them but the Set is consolidating them to 1 everytime. The equals looks like this:

public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof IdmsScheduleTable))
return false;
IdmsScheduleTable that = (IdmsScheduleTable) rhs;
if (this.getScheduleId() == null || that.getScheduleId() == null)
return false;
return (this.getScheduleId().equals(that.getScheduleId()));
}

It was written by hibernate.
10468 and 43307 are the two schedule ids.
So That should be putting both in Set. But its not.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:07 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
dnewman3131 wrote:
Yeah it seems that it is pulling them but the Set is consolidating them to 1 everytime. The equals looks like this:

public boolean equals(Object rhs)
{
if (rhs == null)
return false;
if (! (rhs instanceof IdmsScheduleTable))
return false;
IdmsScheduleTable that = (IdmsScheduleTable) rhs;
if (this.getScheduleId() == null || that.getScheduleId() == null)
return false;
return (this.getScheduleId().equals(that.getScheduleId()));
}

It was written by hibernate.
10468 and 43307 are the two schedule ids.
So That should be putting both in Set. But its not.


How about trying this, just to prove the set is losing them somehow.

Code:
List results = s.createCriteria(IdmsScheduleTable.class).add(Restrictions.eq("sdrl_id",sdrl_id)).list();
System.out.println("List.size() = " + results.size();
Set schedules = new HashSet(results);
System.out.println("Set.size() = " + schedules.size();

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:37 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
I ran it and got these results:
List.size() = 2
Set.size() = 1

It appears to be removing the extra one in a set.
Any idea what could cause this Set to do this?

Thanks,
Dave


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:39 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
dnewman3131 wrote:
I ran it and got these results:
List.size() = 2
Set.size() = 1

It appears to be removing the extra one in a set.
Any idea what could cause this Set to do this?

Thanks,
Dave


I'd step through the equals() method in a debugger.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:49 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
Interesting.
The code does not seem to use equals at all when doing a Set.
It is entering hashCode method. Both have a variable called hashCode.
This variable is the same for both. 11097
For some reason its not using different hashCodes.

public int hashCode()
{
if (this.hashValue == 0)
{
int result = 17;
int scheduleIdValue = this.getScheduleId() == null ? 0 : this.getScheduleId().hashCode();
result = result * 37 + scheduleIdValue;
this.hashValue = result;
}
return this.hashValue;
}

This is the generated hashCode method.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:49 am 
Newbie

Joined: Wed Sep 07, 2005 5:15 pm
Posts: 12
Correct that the variable is called hashValue.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 08, 2005 11:53 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
dnewman3131 wrote:
Correct that the variable is called hashValue.


The variable name doesn't matter but the implementation should definitely return different values for different objects.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 18 posts ]  Go to page 1, 2  Next

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.