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

how hibernate works?

 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was going through very good tutorial on "how hibernate works" on webpage
i want to understand one thing. when does the actual trip to DB takes place



In the above code, there would be two SQL queries fired at the DB for lines marked as 1(insert) and 2 (update). Does that mean from the code there wil be two trips to the DB server ?

 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
trips to DB take place only when the session is flushed or the Transaction is committed.
In your case, it only takes place with the line marked with "//2"

In this case, Hibernate will keep the changes to the user object in its session, and only when a commit() is called, it makes the DB call..

You can check this debugging the code, and verifying that both the queries are executed at the line marked with "//2"
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul Babbar wrote:trips to DB take place only when the session is flushed or the Transaction is committed.

Well, they also take place when stuff is loaded into the session, but Ravi's code doesn't include this step so I'll let you off
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed!!...
 
Ravi Bansal
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rahul

but as you said

Both the queries are executed at the line marked with "//2"


then why should we have two queries ? As in this user object field password is overritten ..dont you think framework should update the data in the DB with just one query
 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ravi Bansal wrote:Thanks Rahul
then why should we have two queries ? As in this user object field password is overritten ..dont you think framework should update the data in the DB with just one query


Well...if you do the following two queries,
1) Insert into A values(1, 'A');
2) Update A set value = 'B'

Hibernate should definitely execute two different queries, because two different queries could have different side effects...

As a matter of fact, the same question can be asked from you...why didn't you set all the values at once...and then did a save.....(instead of doing save and then update)...

Hibernate just uses the queries that you want it to fire...and fires them in the same way and order...as you want it to...

It doesn't have any(much) of AI....thankfully...
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for checking out this little How Hibernate Works tutorial of mine. It certainly generates alot of discussion, and that was really the intention.

Certainly looking at the code, you'd think the database is invoked on the save, after all, that is when we are asking for a save. But this demonstrates that in face Hiberante keeps the object locally, accepts changes, and then commits all the changes when the transaction is committed. So, the all important run to the database really happens when the transaction is committed, at //2, and all of the data that is set in the object is sent to the database.

Hopefully the example generated some good questions and helped further some of your learning about Hibernate.

-Cameron McKenzie
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic