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.  [ 2 posts ] 
Author Message
 Post subject: the famous ERROR "object references an unsaved transien
PostPosted: Thu Jan 10, 2008 11:41 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi,
I have the famous ERROR "object references an unsaved transient instance - save the transient instance before flushing" in a bidirectional ManyToOne-association, where I just call EntityManager.merge() on the One-side.
Yes, and I know, I have a) to have to mark both sides with cascade=CascadeType.MERGE and b) I have to declare on the MANY-side "mappedBy=...". So, I really did this.

What is going on, I just partly understand while debugging into Hibernate code. I noticed that Hibernate does the following:
- it calls newInstance on the class which is my root class which I gave EntityManager to merge.
- it copies this new instance into some cache, and the, before copying the values of the original object into it, it does "cascadeBeforeSave".

In this check process the error comes up: Hibernate does a check on the instance it created with newInstance. And of course detects, that this is transient. Then it throws.

So my problem is: I do not know what is the real reason for this behaviour. I can not image that this is a Hibernate bug (this would have been discovered a long time ago..), so it must result from my mappings.

I give you the main code points:

Code:
@Entity
public class CentralInstitution {
...
@ManyToOne(cascade=CascadeType.MERGE) 
   //@JoinColumn(name="USER_ID")
   @JoinTable(name = "CENTRALINST_USER",
             joinColumns = { @JoinColumn(name = "INSTITUT_ID") },
             inverseJoinColumns = { @JoinColumn(name = "USER_ID") })
   @org.hibernate.annotations.ForeignKey(name="FK_USER_CENTRALINST_ID")
private User responsible;
...
}

@Entity
public class User {
...
@OneToMany(mappedBy = "responsible", cascade = { CascadeType.MERGE } )
private Set<CentralInstitution> institutions = new HashSet<CentralInstitution>();
...
}

The association goes into a join table, because it is optional for a parallel super class of CentralInstitution.


If anyone has a idea what's going on: I need it URGENTLY!

Ciao, Carlo


Top
 Profile  
 
 Post subject: Re: the famous ERROR "object references an unsaved tran
PostPosted: Fri Jan 11, 2008 6:39 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi,

I made an experiment and just removed the bidirection, that is I removed the following code.
Code:

@OneToMany(mappedBy = "responsible", cascade = { CascadeType.MERGE } )
private Set<CentralInstitution> institutions = new HashSet<CentralInstitution>();
...
}


So when I did this, the error went away. I have to mention one thing that I didn't describe in my first post. The User object which is shown above is already stored in the database, and only the CentralInstitution object is new and wants to get saved.

So, my question is: how can I have a true bidirectional association get working? With the example shown, I could live without bidirection. But I have other associations where bidirectionality is essential.

Any ideas????

Ciao, Carlo


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