-->
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.  [ 9 posts ] 
Author Message
 Post subject: one-to-many and generated IDs
PostPosted: Sat Nov 05, 2005 6:53 am 
Newbie

Joined: Sat Nov 05, 2005 5:16 am
Posts: 4
I'm stuck on a one-to-many association and how the ID gets assigned on the many end.

With existing data I can load an A and all its Bs get loaded for me in the map just fine. I can even modify elements of B, save A and have those changes to B written correctly.

The problem is when I create a new A with a new set of Bs. Hiberrnate generates a new ID and writes the A correctly. It then tries to write every column of the new Bs except for their a_id column. How does the generated ID for A get propagated to its Bs?


Hibernate version:
3.0.5

Schema:
Code:
create table a
(
  a_id integer not null,

  primary key (a_id)
);

create table b
(
  a_id integer not null,
  code integer not null,
  desc varchar(255),

  primary key (a_id, code),
  foreign key (a_id) references a (a_id)
);



Mapping documents:
Mapping for A:
Code:
<class name="A" table="a">
  <id name="id" column="a_id" type="string" unsaved-value="null">
    <generator class="my.uuid.Generator"/>
  </id>

  <map name="foo" table="b" lazy="false" cascade="all">
    <key column="a_id"/>
    <map-key column="code" type="string"/>
    <one-to-many class="B"/>
  </map>
</class>


Mapping for B:
Code:
<class name="B" table="b">
  <composite-id name="aId" column="a_id" type="string"/>
  <composite-id name="code" column="code" type="string"/>

  <property name="desc" column="desc"/>
</class>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 8:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Well, given the current setup you have, you'd need to explicitly set the a-id onto B yourself. That would mean disabling cascading, explicitly saving A and then using the generated id to populate B.aId.

The easier approach, however, would be to simply map the B->A association which you have not yet done (i.e. make it bi-directional):
Code:
<class name="B" table="b">
  <composite-id>
    <key-many-to-one name="aId" column="a_id" class="A"/>
    <key-property name="code" column="code" type="string"/>
  </composite-id>
  <property name="desc" column="desc"/>
</class>

Your B class would then have a reference to an A, as opposed to just the aId.

BTW, how did you get mapping the composite-id as two seperate elements to work? The DTD does not even allow that.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 2:51 pm 
Newbie

Joined: Sat Nov 05, 2005 5:16 am
Posts: 4
Unfortunately, that's one of the things I've tried and gotten the same results. The ID on B never gets set, nor does a full ref to its A, which causes a not-null constraint violation on the B insert.

Quote:
BTW, how did you get mapping the composite-id as two seperate elements to work? The DTD does not even allow that.


That was a typo in copying it into this form. The element names just got shuffled a little, it should have read:
Code:
<composite-id>
  <key-property .../>
  <key-property .../>
</composite-id>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 3:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
nor does a full ref to its A, which causes a not-null constraint violation on the B insert

I am certain that was just issues in your code. Given then mapping I suggested, you'd just do:

Code:
A a = new A();

B b1 = new B();
b1.setA( a );
a.getFoo().put( ... );

B b2 = new B();
b2.setA( a );
a.getFoo().put( ... );

session.save( a );


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 3:40 pm 
Newbie

Joined: Sat Nov 05, 2005 5:16 am
Posts: 4
steve wrote:
Code:
A a = new A();
B b1 = new B();
b1.setA( a );
...


I see; I was thinking hibernate would set the A for me. With that change both sides get persisted correctly. However, I can't use it as that circular reference will cause problems in some unrelated code, which is why I was originally only holding the key to A, not A itself. Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 05, 2005 10:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
The only way is what i suggested in my original reply


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 07, 2005 5:13 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
bhl wrote:
steve wrote:
Code:
A a = new A();
B b1 = new B();
b1.setA( a );
...


I see; I was thinking hibernate would set the A for me. With that change both sides get persisted correctly. However, I can't use it as that circular reference will cause problems in some unrelated code, which is why I was originally only holding the key to A, not A itself. Any suggestions?


Just one question: What do you mean by "circular reference"?

I normally do it this way:

Code:
class A {
   Set bs;

   public void addB( B b ) {
       assert b != null;
       if ( ! bs.contains( b ) ) {
          bs.add( b );
          b.setA( this );
       }
   }

   public void removeB( B b ) {
      if ( bs.contains( b ) ) {
         bs.remove( b );
         b.setA( null );
      }
   }
}

class B {
   A a;

   public void setA( A _a ) {
      if ( a != _a ) {
         if ( a != null ) {
            a.removeB( this );
         }
         a = _a;
         if ( a != null ) {
            a.addB( this );
         }
      }
   }
}



This way, you only have to attach one side of the association in your code and the object themselves ensure that the association's integrity is kept.



hth,
Heinz
Don't forget to rate if this helped.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 08, 2005 2:03 am 
Newbie

Joined: Sat Nov 05, 2005 5:16 am
Posts: 4
hhuber wrote:
Just one question: What do you mean by "circular reference"?


Some custom bean-based serialization is the problem I run into. If a B has a reference to the A that owns it, that A will get serialized, which will cause each of its Bs to get serialized, which hold references to their As which get serialized, and we're into an infinite recursion.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 4:02 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
bhl wrote:
hhuber wrote:
Just one question: What do you mean by "circular reference"?


Some custom bean-based serialization is the problem I run into. If a B has a reference to the A that owns it, that A will get serialized, which will cause each of its Bs to get serialized, which hold references to their As which get serialized, and we're into an infinite recursion.


If your design has no flaw (and I'm understanding it correctly), one B can only have one A.
Then, your done as soon as each of the Bs is serialized. The As of all the other Bs are the original A.
Serialization has to deal with this issue anyway! You have to remember, which objects you already serialized.

Regards,
Heinz


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