| 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
 
 
 |