-->
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.  [ 7 posts ] 
Author Message
 Post subject: many-to-many AND many-to-one for same association?
PostPosted: Fri Oct 21, 2005 6:07 am 
Newbie

Joined: Fri Oct 21, 2005 4:49 am
Posts: 13
Hibernate version:
3.0.5
Name and version of the database you are using:
mySQL 4.1.14

I've got my application working fine apart from one issue.

I have a many to many association between A and B. In the mapping files these are explicitly called many to many.

However, I need to store some extra information about the association. There is an extra column in the link table and the A and B thus have an extra many to one association to the link class AB.
The link table has combo of both foreign keys of A and B as the PK.
The mapping file for AB generated by middlegen includes a composite key but instead declares the only non-key property (extraInfo) in the key.
Code:
MIDDLEGEN GENERATED VERSION
<class name="AB" table="AB">
   <composite-id>
        <key-property name="extraInfo" type="String">
   </composite-id>
<many-to-one name="A" class="A" not-null="true">
    <column name = "AID">
</many-to-one>
<many-to-one name="B" class="B" not-null="true">
    <column name = "BID">
</many-to-one>
</class>

<class name="A" table="A">
  <id name="A" column="A" type="integer">
<set name="Bs" lazy="true" cascade="save-update" table="AB" >
    <key><column name="AID"></key>
    <many-to-many class="B"><column name="BID"></many-to-many>
</set>
<set name="AB" lazy="true" cascade="save-update" inverse="true">
   <key><column name="AID"></key> <!-- i guess this should be composite too-->
   <one-to-many class="AB"/>
</set>
</class>

similarly for B but the many-tomany to A is mapped inverse=true

If I try to correct this, (using <key-many-to-one> or putting AID and BID in <composite-id>) the application won't even start.
Code:
______              ______
  A   |*----------*| B    |
PK:AID|            |PK:BID|
______|            |______|
    1               1
       *___________*
       | AB        |
       |PK: AID,BID|
       |extraInfo  |
       -------------

The problem is that when I assign an A to a B (or vice versa) a line is correctly put into the AB table but always the default value of extraInfo is saved. I have tried all sorts of different ways of changing this value, none of which work. What is the correct way to do this? Is it just a mapping issue or is there some java code that I haven't tried yet?

Thanks for your help[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 8:52 am 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
yeah but how do you want hibernate to guess what your extraInfo is? There is nothing that will provide it with the extraInfo, so I guess the simplest way to do it is to introduce an extra object AB. Then your associations will look like this:

A one-to-many AB
B one-to-many AB

and

AB many-to-one A
AB many-to-one B
plus the extraInfo field to map

Note that all of your associations are now much simpler than many-to-many :-) Of course now you have to deal with an extra object :-(

HTH,
Alex.


Top
 Profile  
 
 Post subject: there is an AB object
PostPosted: Fri Oct 21, 2005 9:33 am 
Newbie

Joined: Fri Oct 21, 2005 4:49 am
Posts: 13
But there already is an AB object with the extraInfo field. The problem is the mapping I think. That can't be right as middlegen generated it. But Hibernate just gets stuck "processing foreign key constraints" on startup if I tell it what AB's actual PKs are.

Using middlegen's hbm file, causes a warning on startup along lines of cannot validate since can't find class AB. Even though it previously said it already mapped it...

I guess there is an issue because A and B have 2 associations to each other. One many to many which works beautifully AND a one-to-many with link table...

Thanks for the help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 11:26 am 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
yes, sorry what was I looking at :-)

I am not sure what middlegen is doing but here how my mapping for a situation like this usually looks:

Code:
<class name="AB" table="AB">
         <id
            name="id"
            column="id"
            type="long">
            <generator class="native" />
        </id>
        <many-to-one
            name="a"
            class="A"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="aid"
        />
        <many-to-one
            name="b"
            class="B"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="bid"
        />

       <property name="extraInfo" />

</class>


Top
 Profile  
 
 Post subject: Re: many-to-many AND many-to-one for same association?
PostPosted: Fri Oct 21, 2005 9:50 pm 
Beginner
Beginner

Joined: Tue Oct 18, 2005 3:57 pm
Posts: 48
Location: Los Angeles, CA
torpid wrote:
I have a many to many association between A and B. In the mapping files these are explicitly called many to many.

However, I need to store some extra information about the association. There is an extra column in the link table and the A and B thus have an extra many to one association to the link class AB.

Try using this in you mapping file:
Code:
<set name="ab" table="AB">
  <key column="AID"/>
  <composite-element class="AB">
    <parent name="a"/>
    <many-to-one name="b"
                 class="B"
                 column="BID"
                 not-null="true"/>
    <property name="extraInfo" column="EXTRA_INFO" type="string" not-null="true"/>
  </composite-element>
</set>


And your classes will look something like this:
Code:
class A {
  private Set ab;
  ...
}

class AB {
  private A a;
  private B b;
  private String extraInfo;
  ...
}


Hope this helps (and don't forget to rate :D).

jd


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 11, 2005 4:53 am 
Newbie

Joined: Fri Oct 21, 2005 4:49 am
Posts: 13
In the end, I left the mapping files as they were. The problem I had was that in the Java, I was calling
Code:
B b = new B();
a.getBs().add(b);
b.getAs().add(a);
AonB aob = new AonB(a, b);
AonB aob2 = session.get(aob, AonB.class);
aob2.setExtraInfo("extra");
session.save(aob2);


the code which works is just
Code:
session.save(new AonB(a,b,"extra"));


Hope this helps others


Top
 Profile  
 
 Post subject: Mapping needs to change
PostPosted: Fri Nov 11, 2005 7:22 am 
Newbie

Joined: Fri Oct 21, 2005 4:49 am
Posts: 13
A separate problem I had here
http://forum.hibernate.org/viewtopic.php?p=2270574#2270574

is down to this mapping issue. Any more suggestions?
Really stumped, any help would be greatly appreciated


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