Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate makes a save instead os an update

 
Rui Felix
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings,

I already set some posts because I was not able to save in two table with hibernate with the a schema of Parent/Child type. It is already solved. I've used a set on the Parent side and a composite-id with unsaved-value="any" on child side.
But now I'm having another problem, I can't update the values previously saved. What I want is to get the values from the database, which goes very fine and change this values and set them again in the database. What is happening right now is hibernate tries to insert again instead of update which return a unique constraint (RMS12.RBI_PK) violated.
What I've search tells me that if I use unsaved-value="any" in the composite-id element I need to implicit Update() but I don't know what this means.

Any one had this problem before? How did you solved it?

Many thanks in advance
Rui Felix
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, based on your few threads, what I think would help out to explain what a Hibernate Session, JPA EntityManager, Persistence Context (Which I will just call a session from now on because it is less to type) and how it operates with your managed objects.

So when you open a Session and Transaction, you are creating a bunch of empty maps(Which is in the PersistenceContext object in the Session object) to hold your managed objects that you create/load/query from the database and Hibernate will put into those maps.

These objects in the session are being managed by Hibernate. And also so that Hibernate can hold all the statements it needs to run on the database at flush time. So if the object that is being managed does not have its id attribute set, so it is null, then Hibernate will assume it needs to run an insert statement. If it is not null, and Hibernate knows that you have made any changes to any other attribute then Hibernate will know to do an update.

So what it sounds like, and without seeing any code, that yo have this object without the id set, it is then "saved" in a Hibernate Session, then you close the session, but still have a reference to that object without the id set, you open a new session make a change to the object then call save on that second session and that second session looks at the object and doesn't see an id set, and says, hey I need to do an insert.

Just my thoughts.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic