-->
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.  [ 4 posts ] 
Author Message
 Post subject: one-to-many behavior
PostPosted: Wed Feb 16, 2005 11:48 am 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
Hibernate version:2.1.7

I am seeing some behavior with a one-to-many relationship that may or may not be strange. This may just be the way it is, but I thought I would ask for some advice.

I have a table "member" and a table "eligible". There is a one-to-many relationship between these tables. (one member to many eligible). I have eligible defined as a "Set" in my member object. There is a possibility that there are no eligible rows for a member. For that reason I do a "left join fetch" in my HQL. Now here is what I am getting. I get multipile member objects (one for each eligible object). Each member object has the correct number of eligible objects in the "Set". Is this just the way it is? Is there a better way of doing this? What I would have liked to see is one member object with the eligible Set loaded correctly.

Here is my code

hbm for member
Code:

<class name="com.sys.dao.bean.memberBean" table="member" mutable="false">
    <composite-id name="memberKey" class="com.sys.dao.bean.memberComposite">
       <key-property name="subscriberNo" column="SUBNO"/>
       <key-property name="personNo" column="PERNO"/>
    </composite-id>   
   
    <set name="eligSet" lazy="true" inverse="true">
      <key>
          <column name="SUBNO"/>
          <column name="PERNO"/>
     </key>
      <one-to-many class="com.sys.dao.bean.eligibleBean"/>
  </set>

... rest of mappings


hbm for eligible
Code:
  <class name="com.sys.dao.bean.eligibleBean" table="eligible" mutable="false">
     <composite-id name="eligibleKey" class="com.sys.dao.bean.eligibleComposite">
        <key-property name="subscriberNo" column="SUBNO"/>
        <key-property name="personNo" column="PERNO"/>
        <key-property name="eligStartDate" column="ESTDT" type="java.lang.Integer"/>       
        <key-property name="eligEndDate" column="EENDT" type="java.lang.Integer"/>     
    </composite-id>

... rest of mappings



HQL code
Code:
FROM memberBean memBean  left join fetch memBean.eligSet eligSet


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 12:32 pm 
Newbie

Joined: Wed Sep 29, 2004 10:10 am
Posts: 13
Location: Washington D.C. USA
1. try removing 'inverse=true', default is flase.
2. It seems like from your message that there is many to many relationship between member and eligible. If yes, you might want to add a cross-reference table.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 12:58 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
I took out 'inverse=true' and tried it again. Same result. This is truly a one-to-many releationship. That is, a member could have many eligibles, an eligible has exactly one member.

Thanks Anyway!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 1:08 pm 
Beginner
Beginner

Joined: Wed Nov 24, 2004 10:54 am
Posts: 48
I had a thought, perhaps my hashcode and/or equals override was not correct, but it looks good to me.

Here they are.

for memberComposite
Code:
   public String toString() {
       return new ToStringBuilder(this)
           .append("subscriberNo", getSubscriberNo())
           .append("personNo", getPersonNo())
           .toString();
   }
   public boolean equals(Object other) {
       if ( (this == other ) ) return true;
       if ( !(other instanceof memberComposite) ) return false;
       memberComposite castOther = (memberComposite) other;
       return new EqualsBuilder()
           .append(this.getSubscriberNo(), castOther.getSubscriberNo())
           .append(this.getPersonNo(), castOther.getPersonNo())
           .isEquals();
   }
   public int hashCode() {
       return new HashCodeBuilder()
           .append(getSubscriberNo())
           .append(getPersonNo())
           .toHashCode();
   }


for eligibleComposite

Code:
   public String toString() {
       return new ToStringBuilder(this)
         .append("subscriberNo", getSubscriberNo())
           .append("personNo", getPersonNo())
           .append("eligStartDate", getEligStartDate())
           .append("eligEndDate", getEligEndDate())
           .toString();
    }
    public boolean equals(Object other) {
       if ( (this == other ) ) return true;
           if ( !(other instanceof eligibleComposite) ) return false;
           eligibleComposite castOther = (eligibleComposite) other;
           return new EqualsBuilder()
               .append(this.getSubscriberNo(), castOther.getSubscriberNo())
               .append(this.getPersonNo(), castOther.getPersonNo())
               .append(this.getEligStartDate(), castOther.getEligStartDate())
               .append(this.getEligEndDate(), castOther.getEligEndDate())
               .isEquals();
    }
    public int hashCode() {
       return new HashCodeBuilder()
           .append(getSubscriberNo())
           .append(getPersonNo())
           .append(getEligStartDate())
           .append(getEligEndDate())
           .toHashCode();
    }


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