-->
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.  [ 4 posts ] 
Author Message
 Post subject: Problem with foreign keys and bidirectional associations
PostPosted: Fri Jan 19, 2007 4:22 am 
Newbie

Joined: Thu Dec 21, 2006 7:30 am
Posts: 11
Hi -

I have some issues with inverse and bidirectional many-to-one association.

The issues arises under the simplified circumstances:

A is many-to-one B
B has a set of A.

Java classes are

Code:
public class A {
   int id;
   B b;

   private void setId(int id) {
      this.id = id;
   }

   public int getId() {
      return id;
   }

   public void setB(B b) {
      this.b = b;
   }

   public B getB() {
      return b;
   }
}


Code:
public class B {
   int id;
   Set<A> as = new HashSet<A>(0);

   private void setId(int id) {
      this.id = id;
   }

   public int getId() {
      return id;
   }

   public void setAs(Set<A> as) {
      this.as = as;
   }

   public Set<A> getAs() {
      return as;
   }
}


Mapping files are

Code:
<hibernate-mapping>
   <class name="A" table='"A"' schema='"public"' lazy="true">
      <id name="id" type="int">
         <column name='"Id"' />
    <generator class="sequence">
       <param name="sequence">"A_Id_seq"</param>
    </generator>
      </id>
      <many-to-one name="b" class="B" fetch="select">
         <column name='"bId"' not-null="true" />
      </many-to-one>
   </class>
</hibernate-mapping>

Code:
<hibernate-mapping>
   <class name="B" table='"B"' schema='"public"' lazy="true">
      <id name="id" type="int">
         <column name='"Id"' />
    <generator class="sequence">
       <param name="sequence">"B_Id_seq"</param>
    </generator>
      </id>
      <set name="as" inverse="false" lazy="true">
         <key>
            <column name='"bId"' not-null="true"/>
    </key>
         <one-to-many class="A" />
      </set>
   </class>
</hibernate-mapping>


The issues I am having, is when I create a new A and B, binding them via B.getAs().add(a)

Code:
Session session = HibernateSessionFactory.getSession();

session.beginTransaction();

B b = new B();
A a = new A();

Set<A> as = b.getAs();
as.add(a);
b.setAs(as);

b.save();
a.save();      
      
session.getTransaction().commit();


I am getting
Quote:
org.hibernate.PropertyValueException: not-null property references a null or transient value: A.b


What does work is:

code]
Session session = HibernateSessionFactory.getSession();

session.beginTransaction();

B b = new B();
A a = new A();
a.setB(b);

b.save();
a.save();

session.getTransaction().commit();
[/code]

I thought inverse="false", should have taken care of this problem?

Is there any solution to this issue or am I forced to change my app server so that everything is done in the second manner?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 6:43 am 
Newbie

Joined: Thu Nov 30, 2006 12:21 pm
Posts: 14
Hi,
your probleme is a typic cascade probleme ,
try to add the : cascade="save" option for the association with A in your class B mapping file , I think it would work with this .
What happen , is when you try to save B , Hibernate found that B reference a Colection "As" which refernce a transient Object A because A is not yet persist , And this use case crach immediately
the deal is before evry persistant action with hibernate be sure that the object you persist don't referencea transient one .


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 7:54 am 
Newbie

Joined: Thu Dec 21, 2006 7:30 am
Posts: 11
Hi aldwin -

Thanks for replying -

I have added cascade="save-update" so now hibernate uses its cascade engine when i save "b".

However an other issue occurs. When it is persisting "a" it uses "NULL" as id for "b" ! (strange)

Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 19, 2007 8:20 am 
Newbie

Joined: Thu Dec 21, 2006 7:30 am
Posts: 11
Jesus I am stupid ...

Well it should be inverse="true" and cascade="save-update".

Well here is how it should be done

Code:
<hibernate-mapping>
   <class name="A" table='"A"' schema='"public"' lazy="true">
      <id name="id" type="int">
         <column name='"Id"' />
         <generator class="sequence">
            <param name="sequence">"A_Id_seq"</param>
         </generator>
      </id>
      <many-to-one name="b" class="B" fetch="select" cascade="save-update">
         <column name='"bId"' not-null="true" />
      </many-to-one>
   </class>
</hibernate-mapping>


Code:
hibernate-mapping>
   <class name="B" table='"B"' schema='"public"' lazy="true">
      <id name="id" type="int">
         <column name='"Id"' />
         <generator class="sequence">
            <param name="sequence">"B_Id_seq"</param>
         </generator>
      </id>
      <set name="as" inverse="true" lazy="true">
         <key>
            <column name='"bId"' not-null="true"/>
         </key>
         <one-to-many class="A" />
      </set>
   </class>
</hibernate-mapping>


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