-->
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.  [ 1 post ] 
Author Message
 Post subject: How to persist One-Many bi directional recursive relation
PostPosted: Fri Sep 23, 2011 11:44 am 
Newbie

Joined: Mon Sep 19, 2011 4:34 pm
Posts: 1
Hi All,

I'm trying to understand the recursive mapping given in "Hibernate in Action" and not able to persist the Category object. Please help me in understanding that the mistake I'm making here.

Category.hbm.xml
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">
<!-- Generated Sep 22, 2011 3:30:35 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="org.learning.hibernate.Category" table="CATEGORY">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <many-to-one name="parent" class="org.learning.hibernate.Category" fetch="join">
            <column name="PARENT" />
        </many-to-one>
        <set name="children" table="CATEGORY" inverse="false" lazy="true">
            <key>
                <column name="ID" />
            </key>
            <one-to-many class="org.learning.hibernate.Category" />
        </set>
    </class>
</hibernate-mapping>


Category.java
Code:
import java.util.HashSet;
import java.util.Set;

public class Category {
   private int id;
   private Category parent;
   private Set<Category> children = new HashSet<Category>();
   
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public Category getParent() {
      return parent;
   }
   public void setParent(Category parent) {
      this.parent = parent;
   }
   public Set<Category> getChildren() {
      return children;
   }
   public void setChildren(Set<Category> children) {
      this.children = children;
   }
   
   public void addChildren(Category child){
      if(child.getParent()!=null){
         child.getParent().getChildren().remove(child);
      }
      child.setParent(parent);
      children.add(child);
   }
}



So, if are already aware of this example given in the book (Hibernate in action), then this setup is almost the same as given.

Now, my persisting logic is as below and this is where, I think the flaw is

Code:
Session session = HibernateUtil.currentSession();
session.beginTransaction();
Category electronics = new Category();
Category tv = new Category();
Category discPlayer = new Category();
      
tv.setId(2);
tv.setParent(electronics);
      
discPlayer.setId(3);
discPlayer.setParent(electronics);
      
electronics.setId(1);
electronics.addChildren(tv);
electronics.addChildren(discPlayer);
      
session.save(electronics);
session.getTransaction().commit();


I'm getting the below exception
Code:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
   at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:85)
   at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
   at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:47)
   at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1206)
   at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
   at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:265)
   at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:188)
   at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
   at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
   at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
   at org.learning.hibernate.tests.TestHibernateExamples.testCategory(TestHibernateExamples.java:130)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
   at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
   at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)


Upong debugging, I see its failing at session.getTransaction().commit();
and the Hibernate logs shows

Hibernate:
/* insert org.learning.hibernate.Category
*/ insert
into
LEARN.CATEGORY
(PARENT, ID)
values
(?, ?)
Hibernate:
/* create one-to-many row org.learning.hibernate.Category.children */ update
LEARN.CATEGORY
set
ID=?
where
ID=?


Now, I know that Hibernate does not do automatic association management as EJB does, but what is the proper way of persisting the Category.

Please advise.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.