-->
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.  [ 11 posts ] 
Author Message
 Post subject: 2.11 issue using composite-id within a set - works in 2.03
PostPosted: Sat Jan 03, 2004 3:30 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
I've been troubleshooting a problem with hibernate for the last few days.

I have a parent object that has a composite key. This object has a collection (bag) of children, all of which also have a composite key.

All objects seem to get loaded, but when i call parent.getChildren, it's size is 0, even though i can see the children objects getting loaded.

After trying mapping many diff ways, i changed out my jars to 2.03. Works fine. I suspect some cglib issues, but i don't know enough about the hibernate source yet.

Model:

Visit has 1-m relation to visit-role

xml for visit (excerpt)
Code:
<composite-id name="id" class="x.tas.entities.patientmodel.VisitId">
   <key-property name="cdrId" column="PATIENT_CDR_ID" type="long"/>
   <key-property name="visitNumber" column="VISIT_NBR" type="string"/>
</composite-id>



<bag name="visitRoles" lazy="true" cascade="all" outer-join="auto"  >
   <key >
      <column name="PATIENT_CDR_ID" />
      <column name="VISIT_NBR" />
   </key>
   <one-to-many class="x.tas.entities.patientmodel.CaregiverVisitRole" />
</bag>

=============for the visit-role table
<composite-id name="id" class="x.tas.entities.patientmodel.VisitRoleId" >
<key-many-to-one name="visit">

<column name="PATIENT_CDR_ID"  />
<column name="VISIT_NBR" />
</key-many-to-one>
   <key-property column = "VISIT_ROLE" name="role"/>
</composite-id>


Thanks!
James


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 1:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Be sure to remove *every* old libs from hibernate 2.0 and replace them by Hibernate 2.1 ones (including cglib).
If the children are actually loaded, check your visitRoles getter and setter, it might be somehow broken (usual symptoms).

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 10:23 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Thanks for the tips. I rechecked my classpath, and I am certain i'm not using a mix of jars. The setter/getter pair for the collection is fine.

Still having the same issue. There is no problem when using 2.03, but there is with 2.11. Any other suggestions out there?

---James


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 12:37 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
some extra info, as i spent some time tracing the source code... lots to learn! At the end of this method, the List does have my expected results in it. It just never gets assigned to the field in my object with the collection on it.

Thanks

Code:
/**
    * Execute an SQL query and attempt to instantiate instances of the class mapped by the given
    * persister from each row of the <tt>ResultSet</tt>. If an object is supplied, will attempt to
    * initialize that object. If a collection is supplied, attempt to initialize that collection.
    */
   private List doQueryAndInitializeNonLazyCollections(
      final SessionImplementor session,
      final QueryParameters queryParameters,
      final Object optionalObject,
      final Serializable optionalId,
      final Serializable[] optionalCollectionKeys,
      final boolean returnProxies
   ) throws SQLException, HibernateException {

      session.beforeLoad();
      List result;
      try {
         result = doQuery(session, queryParameters, optionalObject, optionalId, optionalCollectionKeys, returnProxies);
      }
      finally {
         session.afterLoad();
      }
      session.initializeNonLazyCollections();
      return result;
   }


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 05, 2004 1:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sounds like there might be a problem with your equals() / hashCode() implementation. (Remember that the composite-id is also the key of the collection.)

Try

(1) checking your equals()/hashCode()
(2) changing to a non-composite-id

If neither of these fixes works (ie. if the problem still occurs without a composite-id), submit a very simple test main() method to JIRA, so that someone can take a look.

(If it works for a non-composite-id, the problem is certainly with your equals/hashCode)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 9:29 am 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Thanks Gavin, but we unfortuneately can't change the key. Can i still submit to JIRA? I've worked with Hibernate on about 4 projects now, and unfortuneately, this is one project where i can't make the datamodel more hibernate friendly, by losing the composite keys...

The code DOES seem to work in 2.03. I can get around the problem by manually loading this one dependent collection using session.find, but of course i'd rather have the mapping take care of it for me, as it makes the code so much simpler to read. We have checked/re-checked our equals/hashcode methods, and they are Josh Block friendly.

Thanks for HIbernate! It has made it possible to be object oriented in my domain model!

James


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 06, 2004 1:11 pm 
Pro
Pro

Joined: Tue Aug 26, 2003 1:24 pm
Posts: 213
Location: Richardson, TX
Just FYI, a fellow programmer here is having a similar problem, yet I'm not. (collection not loading.) We've recently upgraded to 2.1.1. (not sure what version she was on before)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 3:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Quote:
Thanks Gavin, but we unfortuneately can't change the key.


???!

Not even for a test???

Quote:
Can i still submit to JIRA?


No, not until you have followed my instructions.

Quote:
The code DOES seem to work in 2.03.


It is not at all impossible that a broken hashCode() problem might not have manifested itself previously.

Please do as I told you: try the same code out with a noncomposite-id, and, if it still doesn't work, then submit to JIRA. Otherwise, if it works, the bug is in your code.


Top
 Profile  
 
 Post subject: composite-id not loading children
PostPosted: Thu Jul 08, 2004 12:38 pm 
Newbie

Joined: Thu Jul 08, 2004 11:17 am
Posts: 1
I have the same problem while loading collections of an object with a composite id. I'm using hashcode - equals utils from apache commons, so i guess there shouldn't be trouble with that:

/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object o) {
if ( !(o instanceof PathId) ) {
return false;
}
PathId rhs = (PathId) o;
return new EqualsBuilder()
.appendSuper(super.equals(o))
.append(context, rhs.context)
.append(token, rhs.token)
.isEquals();
}

/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return new HashCodeBuilder(43, 5).
append(context).
append(token).
toHashCode();
}

furthermore, I performed a test with changing the composite-id to a regular one and the collection was correctly loaded? I wonder what happened with this thread, jlawmi, did you found a solution? was a jira issue created?

here is my mapping:

<hibernate-mapping>
<class
name="mx.connecto.store.Path"
table="Paths"
dynamic-update="false"
dynamic-insert="false"
>
<composite-id name="identifier" class="mx.connecto.store.PathId">
<key-property name="context" column="context"/>
<key-property name="token" column="token"/>
</composite-id>
<set
name="entries"
lazy="false"
inverse="false"
cascade="none"
sort="unsorted"
order-by="identifier"
>

<key>
<column name="context"/>
<column name="token"/>
</key>

<one-to-many
class="mx.connecto.store.ElementEntry"/>
</set>

</class>

</hibernate-mapping>

i would appreciate any help about this issue,


Top
 Profile  
 
 Post subject: Same problem for me
PostPosted: Mon Aug 02, 2004 1:29 pm 
Newbie

Joined: Mon Aug 02, 2004 1:21 pm
Posts: 9
Hi,

I really would like to know what happend to this issue, because I'm experiencing the same problem. I am using hibernate 2.1.4 and jakarta commons for building hashCode and equals methods. I have already tryed everything emmanuel suggested and haven't been successfull.

Here's my mapping:

Code:
<class name="InvoiceItem" table="AMFLIBC.MBDDREP" lazy="true">
   <composite-id>
      
      <key-property name="internalHeaderType" type="string" column="DDDCCD"/>      
      <key-property name="orderNumber" type="string" column="DDCVNB"/>
      
      <key-property name="shipmentHeaderNumber" type="int" column="DDK4NB"/>
      
      <key-property name="shipReleaseSequence" type="int" column="DDLCNB"/>
      <key-property name="kitReleaseSequence" type="int" column="DDAASZ"/>
      
   </composite-id>
   
   <property
      name="shippedQuantity"
      column="DDARQT"
   />
   <property
      name="netSale"
      column="DDDUVA"
   />
   
   <property
      name="productCode"
      column="DDAITX"
   />

    <set name="comments" cascade="none" lazy="true">
      <key>
         <column name="GADCCD"/>
         <column name="GACVNB"/>
         <column name="GAK4NB"/>
         <column name="GALCNB"/>
         <column name="GAAASZ"/>
      </key>
      <one-to-many class="InvoiceItemComment" />
   </set>
</class>

<class name="InvoiceItemComment" table="AMFLIBC.MBGACPP" lazy="true">
      <composite-id>
         <key-property name="invoiceNumber" type="int" column="GAGGNB"/>
         <key-property name="invoiceSequence" type="int" column="GAHYNB"/>
         <key-property name="commentSequence" type="int" column="GAKBNB"/>
         <key-property name="language" type="string" column="GAAKCD"/>
      </composite-id>
      
      <property
         name="reference"
         column="GAHXCD"
         type="string"
      />
      <property
         name="text"
         column="GAHDTX"
         type="string"
      />

</class>


I would really appreciate your help.

_________________
Camilo A.


Top
 Profile  
 
 Post subject: I finally solved the problem.
PostPosted: Tue Aug 03, 2004 5:31 pm 
Newbie

Joined: Mon Aug 02, 2004 1:21 pm
Posts: 9
I finally solved the problem. Thanks a lot!

The cause was indeed the implementation of the "equals" method. I was relying on a plugin called commonclipse that can generate toString(), equals(), hashCode() and compareTo() methods using the Jakarta Commons Lang package.

What I haven't notice, is that the code generated by this plugin for the equals method first calls super.equals(). When a class inherits directly from object, this call must be deleted, for the implementation of equals in Object is a pointer comparison (o1 == o2).

Code:
public boolean equals(Object object) {
      if (!(object instanceof InvoiceHeader)) {
         return false;
      }
      InvoiceHeader rhs = (InvoiceHeader) object;
      return new EqualsBuilder()
         .appendSuper(super.equals(object)) //<-- This was the problem!
         .append(this.creationDate, rhs.creationDate)
         .append(this.invoiceSequence, rhs.invoiceSequence)
         .append(this.invoiceNumber, rhs.invoiceNumber)
         .isEquals();
   }


I hope this can help the other guys having similar problems.

_________________
Camilo A.


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