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>
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.