-->
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.  [ 3 posts ] 
Author Message
 Post subject: Help with mapping recursive association
PostPosted: Wed Aug 15, 2007 9:36 am 
Newbie

Joined: Fri Jul 06, 2007 10:01 am
Posts: 10
I have a table that has a rescursive relationship like this.
Table name: User has these columns
user_id (pk)
depend_user_id (fk)


where depend_user_id has recursive assocation with user_id.
The Hibernate mapping looks like the following
Code:
@Entity
@Table (name = "USER")
public class User {

private int userId;
private List<User> children;
private User parentUser;


@Id
@Column(name = "USER_ID", length = 4)
public int getUserId()
{
   return userId;
}

@OneToMany(mappedBy = "parentuser", targetEntity = User.class)
  public List<User> getChildren()
  {
    return this.getChildren();
  }

@ManyToOne( fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE}, targetEntity = User.class )
@JoinColumn(name="DEPEND_USER_ID", insertable = true, updatable = true)
public User getParentUser()
{
return parentUser;
}
   
}



Is this the correct way to map rcursive relationship? On executing the testcase, hibernate throws this error (stack trace):
Code:
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.domain.User; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.domain.User
Caused by: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.domain.User
   at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
   at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
   at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
   at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:563)
   at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3071)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:472)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:197)
   at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
   at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
   at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
   at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:615)
   at org.springframework.orm.hibernate3.HibernateTemplate$CloseSuppressingInvocationHandler.invoke(HibernateTemplate.java:1197)
   at $Proxy28.flush(Unknown Source)
   at com.fsg.fedad.test.dbunit.HibernateDbUnitTemplate$2.doInHibernate(HibernateDbUnitTemplate.java:65)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:367)
   at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:333)
   at com.fsg.fedad.test.dbunit.HibernateDbUnitTemplate.flush(HibernateDbUnitTemplate.java:70)
   at com.unittest.TestUserTasks.testUpdateUser(TestUserTasks.java:248)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:615)
   at junit.framework.TestCase.runTest(TestCase.java:154)
   at junit.framework.TestCase.runBare(TestCase.java:127)
   at com.fsg.fedad.test.DataAccessTierTestCase.runBare(DataAccessTierTestCase.java:109)
   at junit.framework.TestResult$1.protect(TestResult.java:106)
   at junit.framework.TestResult.runProtected(TestResult.java:124)
   at junit.framework.TestResult.run(TestResult.java:109)
   at junit.framework.TestCase.run(TestCase.java:118)
   at junit.framework.TestSuite.runTest(TestSuite.java:208)
   at junit.framework.TestSuite.run(TestSuite.java:203)
   at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)



Please help me with this issue.
Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 1:00 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
This seems to work for me. Can you show your test case.

I presume you removed the setters to make the post shorter? And the getChildren method is clearly very wrong!
Code:
public List<User> getChildren()
  {
    return this.getChildren();
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 15, 2007 1:24 pm 
Newbie

Joined: Fri Jul 06, 2007 10:01 am
Posts: 10
I tried changing the getChildren() method like u suggested. Didn't work either.
on the joincolumn of getParentUser()

@JoinColumn(name="DEPEND_USER_ID", insertable = false, updatable = false)


if I change the insertable, updatable to false, it works perfectly fine. But have to write values to this column, hence the insertable & updatable has to be true in my case.

Yes, I have left out the setters to keep the post short.
Any suggestions is appreciated.
Thanks,


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