I'm new to Hibernate and am using Hibernate 2.1.3. I've been struggling for a week trying to figure out many-to-many mappings as they relate to association tables that contain additional columns. Reading the forum entries and documentation (and there is a lot of good stuff) have helped but I'm afraid I'm still missing something.
To keep things simple, I have two tables:
Code:
client
====
(PK) account_id
discount
notes
identity
=====
(PK) identity_id
lastname
firstname
and they are linked via an association table (the primary keys of the other two tables combine to make a unique key):
Code:
account_identity
==========
(PK) account_id
(PK) identity_id
type
relationship
I have created the following mappings for the three tables:
Code:
<class name="Client" table="client">
<id
name="accountId"
type="java.lang.String"
column="account_id"
>
<generator class="assigned"/>
</id>
<property
name="discount"
column="discount"
type="java.lang.Float"
/>
<property
name="notes"
column="notes"
type="java.lang.String"
/>
<!-- link to account_identity to get the identity of client -->
<set name="clientToIdentity" table="account_identity" lazy="false" cascade="save-update" inverse="false">
<key column="account_id"/>
<composite-element class="AccountIdentity">
<property name="type" column="type"/>
<property name="relationship" column="relationship"/>
<many-to-one name="identity" class="Identity" column="identity_id" outer-join="auto"/>
</composite-element>
</set>
</class>
<class name="Identity" table="identity">
<id
name="identityId"
type="java.lang.Integer"
column="identity_id"
>
<generator class="identity"/>
</id>
<property
name="lastname"
column="lastname"
type="java.lang.String"
not-null="true"
/>
<property
name="firstname"
column="firstname"
type="java.lang.String"
not-null="true"
/>
<set name="identityToClient" table="account_identity" lazy="false" inverse="true">
<key column="identity_id"/>
<composite-element class="AccountIdentity">
<property name="type" column="type"/>
<property name="relationship" column="relationship"/>
<many-to-one name="client" column="account_id" class="Client"/>
</composite-element>
</set>
</class>
<class name="AccountIdentity" table="account_identity">
<composite-id name="id" class="AccountIdentityPK">
<key-many-to-one name="identityId" class="Identity" column="identity_id"/>
<key-many-to-one name="accountId" class="Client" column="account_id"/>
</composite-id>
<property
name="type"
column="type"
type="java.lang.String"
/>
<property
name="relationship"
column="relationship"
type="java.lang.String"
/>
<many-to-one name="client" class="Client" column="account_id" insert="false" update="false" />
<many-to-one name="identity" class="Identity" column="identity_id" insert="false" update="false" />
</class>
Now keep in mind that a client can have
many identities associated with it such as a secondary contact, emergency contact, etc. Querying the tables seems to work correctly so I believe the mappings are correct for that.
My problem comes when trying to perform a
session.saveOrUpdate(client). I need to be able to save (create or update) an identity, set the two fields in the association table for that identity and have the record created in the association table. All of my attempts so far have been unsuccessful.
Can hibernate do this without having to perform a save on the association table class directly? For example:
Code:
Identity newContact = new Identity();
newContact.setFirstname("John");
newContact.setLastname("Doe"):
AccountIdentityPK aiPK = new AccountIdentityPK();
aiPK.setIdentity(newContact);
aiPK.setAccountId(client);
AccountIdentity ai = new AccountIdentity(aiPK);
ai.setType("secondary");
ai.setRelationship("spouse");
Set contacts = new HashSet();
contacts.add(ai);
client.addToClientToIdentity(contacts);
session.saveOrUpdate(client);
session.flush();
session.close();
I did not include the code for the classes due to the size. If it pertinent then I will but I'm more interested in knowing if hibernate can perform all of the saves through the client object only without separate saves on the identity and account_identity objects and if so whether or not my mappings are correct as is in order for that to happen.
If I haven't been clear or need to re-read a particular section in the documentation, please let me know. Thanks in advance!