-->
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: Persisting Transient Objects Representing Existing Entities
PostPosted: Mon Nov 30, 2009 2:15 pm 
Regular
Regular

Joined: Fri Oct 05, 2007 1:17 pm
Posts: 78
Using Hibernate 3 and JPA, I am attempting to merge a transient Java object with an existing entity, but I am getting a StaleObjectStateException.

Imagine for example an Employee entity:

Code:
@Entity
@Table(name = "EMPLOYEE")
public class Employee {
.
.
.
    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "NAME")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name= name;
    }
}


Let's say I have an Employee entity in my database with id=100 and name="John." Then let's say I do this:

Code:
Employee e = new Employee();
e.setId(100);
e.setName("Mike"):
entityManager.merge(e);


It is basically this sort of situation that is leading to the StaleObjectStateException I mentioned earlier. I assumed that Hibernate would realize there is an existing entity with id=100 and do a SQL UPDATE call on the entity to change "John" to "Mike." Clearly, I am mistaken.

Can you please tell me how I can persist the transient object? I appreciate any insight.

Thanks.


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