-->
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.  [ 2 posts ] 
Author Message
 Post subject: many-to-many only populating one side of relationship
PostPosted: Thu Oct 02, 2003 5:48 pm 
Newbie

Joined: Thu Oct 02, 2003 4:53 pm
Posts: 2
Location: Australia
Hi folks,

I have been driving myself crazy trying to get a many-to-many mapping to work properly and I am hoping that it is something simple!

I have three tables:
1) accomodation
2) accomodation_electronic
3) contact_electronic

accomodation_electronic is the table that joins accomodation and contact_electronic - it simply holds foreign keys to both tables.

In the code snippet below I load an existing accomodation, create a new electronic and add it to the Set of existing electronic addresses. The code runs with no exceptions but unfortunately the only table that is touched is contact_electronic.

By watching the SQL calls I can see that a new row is added to contact_electronic but no row is added in accomodation_electronic - there is nothing to link the newly added contact_electronic with the accomodation.

I could remove the link table and create a one-to-many but I intend to use contact_electronic for other electronic addresses as well e.g., employee emails, company websites etc so it would be great if I can get this many-to-many to work.

My mappings are as follows:

--------------------------------------------------------------------------------
| accomodation.hbm.xml
--------------------------------------------------------------------------------
<class name="Accomodation" table="accomodation">

<jcs-cache usage="nonstrict-read-write"/>

<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="accomodation_ser" sql-type="integer" not-null="true"/>
<generator class="sequence">
<param name="sequence">accomodation_accomodation_ser_seq</param>
</generator>
</id>

<set name="electronicAddresses" lazy="false" table="accomodation_electronic" sort="natural" inverse="true">
<jcs-cache usage="nonstrict-read-write"/>
<key column="accomodation_ser"/>
<many-to-many class="Electronic" column="contact_electronic_ser"/>
</set>
</class>
--------------------------------------------------------------------------------



--------------------------------------------------------------------------------
| electronic.hbm.xml
--------------------------------------------------------------------------------
<class name="Electronic" table="contact_electronic">

<jcs-cache usage="nonstrict-read-write"/>

<id name="id" type="java.lang.Long" unsaved-value="null">
<column name="contact_electronic_ser" sql-type="integer" not-null="true"/>
<generator class="sequence">
<param name="sequence">contact_electronic_contact_electronic_ser_seq</param>
</generator>
</id>

<property name="electronicType" type="java.lang.Character">
<column name="contact_type_code" sql-type="char(1)" not-null="true"/>
</property>

<property name="lastModifiedByUserId" type="java.lang.Long">
<column name="last_modified_by" sql-type="integer" not-null="true"/>
</property>

</class>
--------------------------------------------------------------------------------

Note that there is no mapping back to accomodation from electronic... I am not sure if this is required; still getting my feet wet :)



--------------------------------------------------------------------------------
| Finally, the code:
--------------------------------------------------------------------------------
...
Transaction tx = null;

try
{
Session session = getSessionFactory().openSession();
Accomodation a = (Accomodation) session.load (Accomodation.class, new Long (1));

tx = session.beginTransaction();

Electronic e = new Electronic ();
e.setLastModifiedByUserId(new Long(1));
e.setElectronicType(new Character ('W'));

session.save(getElectronic());

a.getElectronicAddresses().add(e);

session.saveOrUpdate(a);

session.flush();

tx.commit();
}
...
--------------------------------------------------------------------------------


Thanks in advance to anyone who can offer any assistance - I am really enjoying Hibernate and want to keep learning!

Kind regards,

Damian


Top
 Profile  
 
 Post subject: A ha!
PostPosted: Fri Oct 03, 2003 2:58 am 
Newbie

Joined: Thu Oct 02, 2003 4:53 pm
Posts: 2
Location: Australia
OK... I found the problem and am kicking myself (quite hard). The troublemaker was a compareTo method in the Electronic class - once I removed this method everything fell into place quite nicely.

I read in another post that I can reinstate the compareTo method and feel safe that it won't throw NullPointerExceptions as long as I set lazy="true" for the <set>. Will probably end up adding a column to the join table instead (more learning fun!)


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