-->
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: backref problem
PostPosted: Sun Dec 10, 2006 7:54 pm 
Newbie

Joined: Sun Dec 10, 2006 7:10 pm
Posts: 2
Hello,

I am using Hibernate 3.2.0 (same result with 3.2.1) on postgres 8.1. I have three classes A,B,C like this: B has some C children and A has some B children. I need the position, so I use lists:

Code:
public class A {
   public int id;
   public List<B> b = new ArrayList<B>();
   public A() { }
   public void add(B b) { this.b.add(b); b.a = this; }
}
public class B {
   public int id;
   public List<C> c = new ArrayList<C>();
   public A a;
   B() { }
   public B(A a) { this.a = a; }
   public void add(C c) { this.c.add(c); c.b = this; }
}
public class C {
   public int id;
   public B b;
   public C(B b) { this.b = b; }
}


The mapping file is:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="fr">
   <class name="A" table="A">
      <id access="field" name="id" type="int" column="A_ID" length="4">
         <generator class="sequence"><param name="sequence">a_id_seq</param></generator>
      </id>
      <list access="field" name="b" lazy="true" inverse="false">
         <key column="A_ID" not-null="true" unique="true" />
         <index column="B_IDX" />
         <one-to-many class="B" />
      </list>
   </class>
   <class name="B" table="B">
      <id access="field" name="id" type="int" column="B_ID" length="4">
         <generator class="sequence"><param name="sequence">b_id_seq</param></generator>
      </id>
      <list access="field" name="c" lazy="true" inverse="false">
         <key column="B_ID" not-null="true" unique="true" />
         <index column="C_IDX" />
         <one-to-many class="C" />
      </list>
      <many-to-one access="field" name="a" class="A"
         foreign-key="FK_B_A" column="A_ID" insert="false" update="false"
         not-null="true" />
   </class>
   <class name="C" table="C">
      <id access="field" name="id" type="int" column="C_ID" length="4">
         <generator class="sequence"><param name="sequence">c_id_seq</param></generator>
      </id>
      <many-to-one access="field" name="b" class="B"
         foreign-key="FK_C_B" column="B_ID" insert="false" update="false"
         not-null="true" />
   </class>
</hibernate-mapping>


I create a very small hierarchy:
Code:
a-b1-c
|-b2

public static void test() {

      A a = new A();
      B b1 = new B(a);
      B b2 = new B(a);
      C c = new C(b1);

      Transaction tx = null;
      try {
         Session session = getSession();
         tx = session.beginTransaction();
         session.save(a);
         tx.commit();
         session.evict(a);
         // [...]
         tx = session.beginTransaction();
         A a1 = b1.a;
         session.refresh(a1);
         a.add(b1);
         session.save(b1);
         tx.commit();
         session.evict(a1);
         session.evict(b1);
         // [...]
         tx = session.beginTransaction();
         A a2 = b2.a;
         session.refresh(a2);
         a.add(b2);
         session.save(b2);
         tx.commit();
         session.evict(a2);
         session.evict(b2);
         // [...]
         tx = session.beginTransaction();
         B b = c.b;
         session.refresh(b);
         b.add(c);
         session.save(c);
         tx.commit();
         session.evict(b);
         session.evict(c);
      } catch (HibernateException e) {
         e.printStackTrace();
      }
   }


The database access code has 4 sections separated with [...]. They are actually 4 DAO operations. Notice the explicit eviction. I get the following exception at the line session.save(c):

Code:
org.hibernate.PropertyValueException: not-null property references a null or transient value: fr.C._cBackref
   at org.hibernate.engine.Nullability.checkNullability(Nullability.java:72)
   at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:284)
   at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
   at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
   at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
   at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
   at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
   at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
   at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
   at fr.cs.interdistance.persistence.dao.ADAO.test(ADAO.java:56)
   at fr.cs.interdistance.persistence.helpers.SchemaExport.test(SchemaExport.java:71)
   at fr.cs.interdistance.persistence.helpers.SchemaExport.main(SchemaExport.java:36)


However, if I change
Code:
C c = new C(b1);
into
Code:
C c = new C(b2);
it works. Also, removing the explicit evictions solves the problem. Anyone knows why?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 4:10 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
I guess this is because there are more loaded objects than those you evict from the cache. Try calling session.clear() instead, I think your code will work.

Another problem might come from the fact that you're using the same session for more than one tx. Read the ref doc :
http://www.hibernate.org/hib_docs/v3/re ... tices.html
Quote:
Never share a single session across more than one application transaction, or you will be working with stale data

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 12:31 pm 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
I got some insight when I ran into this problem from here: http://www.jroller.com/page/RickHigh?en ... l_property


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 11, 2006 2:35 pm 
Newbie

Joined: Sun Dec 10, 2006 7:10 pm
Posts: 2
I am now using a session-per-transaction mechanism (current session is managed by the SessionFactory and Transaction logic). It also seemed that I was not using the right way to "reconnect" a detached object.

Thank you both and the point goes to batmat.


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.