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

When does the hibernate query commit?

 
dina raj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will session.save(customer); do any checking on the database? If there is a primary key violation will an exception be thrown while saving the object?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. It will just throw any SQLExceptions generated by violating and constraints you have imposed on your data.
 
dina raj
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use session.save(customer);
My problem is that ,
Now my custonmer table has a child CUST_IDEN. The statement
session.save(customer);
does not throw any exception if there is a primary key violation in CUST_IDEN table. Instead the exception is thrown only later when i commit the transaction.
Is this how it is supposed to happen?
The CUSTOMER hbm file contains this entry which relates it to the CUST_IDEN table
<set name="identifiers" cascade="all-delete-orphan" inverse="true" lazy="true" table="CUST_IDEN">
<key column="CUST_ID" update="true" not-null="true" />
<one-to-many class="CustomerIdentifier"/>
</set>
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, since session.save() does not go to the database immediately. Only when you commit, or if you were to run a query on that table that makes Hibernate do a flush on the session and calls the database.

This is to increase performance. It is also why you want to open and commit a session as quickly as possible, so the "transaction/session" demarcation is very short, if you do a lot of work, you could face issues like what you are having an issue with.

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