Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Why update occurs right after insert?
PostPosted: Wed Jun 29, 2011 6:04 am 
Newbie

Joined: Wed Jun 29, 2011 5:55 am
Posts: 3
Hello!

I'm curious why when using the code:
Code:
      em.getTransaction().begin();
      Agent a = new Agent();
      a.setName("AAA!");
      em.persist(a);
      a.setName("BBB!");
      em.getTransaction().commit();


I got two sql statements:
Code:
Hibernate: insert into AGENT (NAME, id) values (?, ?)
Hibernate: update AGENT set NAME=? where id=?


This is just a simple testcase. The real problem is that at the moment I call persist I dont have all the required fields filled and that causes an relation check exception.
Is it a matter of my configuration error or this suppose to works this way? I thought that the real synchronization with database should occur on commit?

Thanks
Aleksander Filuba


Top
 Profile  
 
 Post subject: Re: Why update occurs right after insert?
PostPosted: Wed Jun 29, 2011 8:27 am 
Newbie

Joined: Thu Jun 23, 2011 9:34 am
Posts: 4
The Answer for your first question is Persist () confirms that it will execute the INSERT statement when it is called inside of transaction boundaries. Objects inside a transaction boundary share a common transaction identifier. Your control over a transaction boundary varies depending on the transaction model you select for your application. The basic validation of columns are checked first , then values are inserted . these values are considered as old values when compared to the values used when UPDATE statement is executed . so Update occurs after Insert .

The Answer for your second question is when u declare a column in a table as NON NULL and provide null values while setting it a transaction in Hibernate class . It will surely throw Exception ..


Top
 Profile  
 
 Post subject: Re: Why update occurs right after insert?
PostPosted: Thu Jun 30, 2011 2:49 am 
Newbie

Joined: Wed Jun 29, 2011 5:55 am
Posts: 3
Hello!

Thanks for Your answer.

To be honest I understand what is happening just dont understand why.
If the synchronization with DB takes place at the commit transaction moment can't those two statements be single insert?

According to doc (at least this is how I understand them) calling persist() only makes the object managed. Is there a way to skip hibernate validation phase (null check) at that moment? Validation phase occurs again on commit time.


Top
 Profile  
 
 Post subject: Re: Why update occurs right after insert?
PostPosted: Thu Jun 30, 2011 4:10 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2296
Location: Third rock from the Sun
As soon as the entity becomes persisted it must have a valid id. From your logs it looks like you're using a database generated identifier, so the insert must be performed before the persist() invocation returns to retrieve this value. Try using a user-assigned id, or a UUID generator as primary key.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Why update occurs right after insert?
PostPosted: Thu Jun 30, 2011 4:28 am 
Newbie

Joined: Wed Jun 29, 2011 5:55 am
Posts: 3
Hello,

Right after persist() call hibernate does sequence retrieval from database - totally understood by me.
I just didn't included that sql in logs. Later on commit() I have insert and immediate update.

I've also tried without sequences manually setting id - on commit() occurs insert and update.
If any more information are required please let me know.

I'm attaching my Agent entity code:
Code:
@Entity
@Table(name = "AGENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"NAME"})})
public class Agent {
   @Id
   @Column(name = "ID", nullable = false)
   private long id;

   @Column(name = "NAME", length = 32, nullable = false)
   private String name;

   public long getId() {
      return id;
   }

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

   public String getName() {
      return name;
   }

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 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.