-->
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: saveOrUpdate() association table
PostPosted: Tue May 04, 2004 12:20 pm 
Newbie

Joined: Fri Apr 02, 2004 2:22 pm
Posts: 11
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!


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 06, 2004 2:27 pm 
Newbie

Joined: Fri Apr 02, 2004 2:22 pm
Posts: 11
For those of you who are interested, looks like my mappings were correct for what I needed to do. The actual problem seemed to be in setting the relationships correctly in the Java code (good to see it was my screw up).

Just for the record, I can't be the only person confused on how to implement many-to-many relationships using an association table that contains other columns under MySQL. The examples in the hibernate document are either:

1. simple Parent/Child or one-to-one (need some more real-world examples),
2. leave out an intermediate mapping that would be helpful, or
3. omit important java code

so using the documentation alone to figure out my problem didn't help as much as I would have liked. With that being said if anybody has some good examples posted elsewhere on the web, I'd sure like see them.


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.