-->
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: Collections with Super class
PostPosted: Wed Jan 21, 2004 10:13 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
Hi:

I have an interesting problem. I have a class called Member which is a subclass of Individual. Now, this superclass has setContacts() and getContacts() methods. These are the setter and getter of a list of objects of the type Contact.

I have two tables one for Member and one for Contact.

The guts of my XML file looks like this:
<class name="com.amerisys.Member" table="Member">
<id name="ID" type="string">
<column name="ID" length="32"/>
<generator class="uuid.hex"/>
</id>
<list name="identifiers" lazy="true" cascade="all">
<key column="parentID"/>
<index column="sequence"/>
<one-to-many class="com.amerisys.Contact"/>
</list>
.
.
.
</class>

<class name="com.amerisys.Contact" table="Contact">
<property name="parentID" column="parentID" type="string"/>
<property name="sequence" column="sequence" type="int"/>
.
.
.
</class>


Now, the problem that I have is when I save the Member class, the Contact information of the Member gets saved in the Contact table but the foreign key is not inserted.

I can overcome this problem by re-implementing (copying from the super class Individual) the setContacts() and getContacts() methods in the Member class. This inserts the forign key without any problems. But, I do not want to do that for obvious reasons.

I am using version 2.1

Any help will be greatly appreciated. Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2004 11:59 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
Your mapping is off.

Look in the Hibernate documentation about the subclass and joined-subclass tags.


Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 12:47 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
Thanks for your prompt response, sgwood.

But I do not want to have a table for the Individual class. Is it possible to have just a table for Member and for Contact and let Member have the list of Contacts in such a way that a row in CONTACT table will have a foreign key relating to a row in MEMBER table?

Thank you,


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 1:03 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
If you use subclass to map Member, both fields of Individual and Member will be saved in a single table.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 1:16 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
You are right. But I do not want a table for Individual at all and I do not want to use the subclass or joined-subclass tags. I just want a row in the CONTACT table to have a foreign key relating to an entry in the MEMBER class. The problem I have is the entry in the CONTACT table has no foreign key while the rest of the column values are inserted.

If I implement the getter and setter for the list of Contacts in the Member class instead of its (Member's) super class, Individual, then I get the foreign key inserted. But I do not want to retype the code for the getter and setter in the subclass, Member.

Is there any way around it?

Thank you,
Om


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2004 1:37 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
Sorry, I did not read your posting carefully enough.

I have a few classes that have exactly the same configuration as you have with Individual and Member, and it is working fine.

You don't refer to Individual at all in your mapping, do you? You should not be.

Rather than having the identifiers collection on Member be a list, for now try having it as a set, ie.


Code:
        <set
            name="identifiers"
            lazy="true"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="parentID"
              />

              <one-to-many
                  class="com.amerisys.Contact"
              />
        </set>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 12:43 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
Okay this was the problem...The super class's setContacts(List contacts) method was iterating the given contacts and this somehow was not liked by Hibernate. So when I copied the contacts to another local temporary variable and used it for iteration and everything worked fine. Was this expected? Can someone explain when you find time.

Also, thanks to sgwood and mota for your help. Appreciate it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 1:16 pm 
Senior
Senior

Joined: Tue Nov 25, 2003 9:35 am
Posts: 194
Location: San Francisco
Can you show both the no-working and working code?


Sherman


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 2:07 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
Working code:
Code:
public void setContacts(List contacts)
{
   if (contacts != null)
     for (Iterator iterator = new ArrayList(contacts).iterator();iterator.hasNext(); this.addContact((ContactInformation) iterator.next()));
}


Non-working code:
Code:
public void setContacts(List contacts)
{
   if (contacts != null)
     for (Iterator iterator = contacts.iterator();iterator.hasNext();this.addContact((ContactInformation) iterator.next()));
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 3:18 pm 
Regular
Regular

Joined: Wed Nov 05, 2003 10:57 pm
Posts: 96
The error is caused by your code not by Hibernate.
Once you create an iterator on a list, this list can be modified only through the iterator's own remove or add methods.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 23, 2004 4:17 pm 
Newbie

Joined: Mon Jan 12, 2004 11:59 am
Posts: 13
Location: USA
I am sorry I didn't know iterator has an add method.


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.