-->
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: TransientObjectException on Session.merge()
PostPosted: Mon Apr 13, 2009 10:02 pm 
Newbie

Joined: Mon Apr 06, 2009 10:48 pm
Posts: 4
Location: Wellington, New Zealand
I've discovered what I consider to be an unexpected inconsistency in behavior:

Say you have an association which has no cascading options set, such as 'parent' in the example below:

Code:
@Entity
public class MyEntity {
    private int id;

    private MyEntity parent;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public int getId() {
        return id;
    }

    @ManyToOne
    @JoinColumn
    public MyEntity getParent() {
        return this.parent;
    }

    public void setParent(MyEntity parent) {
        this.parent = parent;
    }
}


Say you have object 'child' which refers to a transient 'parent':

Code:
MyEntity parent = new MyEntity();
MyEntity child = new MyEntity();
child.setParent(parent);


Now you persist the child using saveOrUpdate:

Code:
session.saveOrUpdate(child);


This succeeds. The parent is (correctly) not persisted, and the parent_id column on the child instance is set to null.

If, instead of using saveOrUpdate, you instead use merge:

Code:
session.merge(child);


...you get a TransientObjectException: "object references an unsaved transient instance - save the transient instance before merging"

(if you don't set the 'parent' association, 'merge' will successfully persist the child object.)

Question:

Is there a rationale for this difference in behavior? I.e., is there a reason 'merge' could not behave like 'saveOrUpdate', and simply leave leave the parent_id column 'null' for the child instance, not persisting the parent?

Hibernate version:

3.3.1

Thanks!
Aron


Top
 Profile  
 
 Post subject: Re: TransientObjectException on Session.merge()
PostPosted: Wed May 06, 2009 7:39 am 
Newbie

Joined: Wed May 06, 2009 5:13 am
Posts: 2
I also miss the hibernate saveOrUpdate functionality in JPA.
See this related thread: viewtopic.php?f=9&t=987792


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.