Techspace

IT happens only in IT

org.hibernate.TransientObjectException Revisited

This is continuation of my earlier post on org.hibernate.TransientObjectException. Let me write the scenario again

<class name=”com.xxx.A” table=”A” schema=”TESTSCHEMA”>
<id name=”aId” type=”java.lang.Long”>
<column name=”A_ID” precision=”29″ scale=”0″ />
</id>

…………………..
some more mapping elements
…………………..
…………………..
<many-to-one name=”bId” class=”com.xxx.B” fetch=”select”>
<column name=”B_ID” precision=”29″ scale=”0″ />
</many-to-one>

…………………..

A is referring to B using a primary key column of bId of B.

In that post I have mentioned that if B is a transient object and you don’t want to persist the value of B to A then just tell the hibernate to ignore that value by saying
update=”false” insert=”false” in the many to one mapping.

But what if you want to persist the value of foreign key in A.

Then the approach is different. You have to make sure that instance B is persistent not transient.

That is if your code says something like

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

session.save(a);

you are in trouble. Because B is in transient state. You have to attach b to the session.
There may be other ways of attaching this transient object to session. The approach I am following is simple. I am reading the value of B from the database using Hibernate. That way, hibernate attaches B to session and it is then a persistent object.

That is I do something like

A a = new A();
B b = session.get(B.class, new Long(1));
a.setB(b);
…..
…..

session.save(a);

I won’t get any exception because this time there is no transient object to save. All the objects are persistent.

Powered by ScribeFire.

Advertisement

January 28, 2008 - Posted by | Hibernate

13 Comments »

  1. I’m doing just what you say, but I still get the transientobjectexception… It is very strange.

    Comment by Dave Bates | March 26, 2008 | Reply

  2. Sorry should have finished my previous post. the update=”false” insert=”false” works just fine, but why is it required when I have just loaded the value of the many to one column from the database? Should be the same sessions right? Right in the same method… And there are other very similar properties (i am initializing the same way) that don’t have the problem. That is what is strange.

    Comment by Dave Bates | March 26, 2008 | Reply

  3. what happens if A and B are new objects

    that is if B is not existing in the DB.

    Is there a strategy there

    Comment by Niraj | May 7, 2008 | Reply

  4. Hi paras,
    could you help me
    how to do this with JPA
    A a = new A();
    B b = session.get(B.class, new Long(1));
    a.setB(b);
    …..
    …..

    session.save(a);
    TIA
    kirti

    Comment by Anand | January 7, 2009 | Reply

  5. @Niraj
    If A and B are new objects, solution given here is not gonna work. You may need to apply cascade for merge in mapping.

    Comment by Yadu | October 26, 2009 | Reply

  6. Hello there,

    For new objects, as Niraj one, I found a solution, but maybe its not the best one.

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

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

    session.commit();

    Well, the problem comes when I try to attach a Set to session; for each instance stored in the Set, I have to make a call to session.save(currentObject); these iteration is a really nasty way to resolve the problem, but works fine for me.

    Maybe something better?

    Comment by Jeff | May 16, 2010 | Reply

  7. I mean, a objects Set pointed by an Object.

    Comment by Jeff | May 16, 2010 | Reply

  8. Thanks Jeff for contributing. I must admit that my post is a little obsolete. I will have to update it based on current hibernate version and my knowledge on that.

    Comment by Paras | May 18, 2010 | Reply

  9. A bit more about this issue:

    Be sure your id generator option is not set to “assigned”. If your have something like that:

    Turn class to “increment”. If you dont, it throws this issue because the parent object has not an identifier assigned yet.

    See you.

    Comment by Jeff | May 19, 2010 | Reply

  10. And if you want to keep generator as “assigned” then make sure you assign “unique” id to the object before persisting

    Comment by Paras | May 19, 2010 | Reply

  11. [...] org.hibernate.TransientObjectException Revisited January 200810 comments 3 [...]

    Pingback by 2010 in review « Techspace | January 4, 2011 | Reply

  12. Hi Niraj..

    Please use the below statements..if you are using annotations..and want to save both parent and child as new objects..
    then inside the Bean of parent class use

    @OneToOne(cascade = CascadeType.ALL)
    above the child member..

    it works. :)

    Comment by Vikas | August 6, 2011 | Reply

  13. HI All,

    How can update value of b in table A with “Null”

    Can any one help me out

    thanks in advance.

    Comment by ram | August 19, 2011 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

Gravatar
WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.