This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Hi. Two of my domain objects are User and Order, the cascade attribute is all-delete-orphan. User object has orders property. User's session contains a detached User object after a successfull login. When the client decided to order his cart, a new Order instance created and added to User's orders field :
managerOrder( ) method manages the both sides of the relationship. The code works fine and a new recored created in the database. But actually what makes me surprised is the generated SQL. The first SQL is an update statement on the USER_TABLE, the second SQL is a select statement on the ORDER_TABLE. Why a select statement, not an insert (after all, a new recored is created) ?
Hibernate: update USER_TABLE set VERSION=?, FIRST_NAME=?, LAST_NAME=?, PASSWORD=?, LAST_TIME_LOGGED=?, STREET=?, CITY=?, COUNTRY=?, creditCard=? where ID=? and VERSION=?
Hibernate: select orders0_.USER_ID as USER_ID__, orders0_.ID as ID__, orders0_.ID as ID0_, orders0_.VERSION as VERSION0_, orders0_.CREATION_DATE as CREATION3_0_, orders0_.TOTAL_PRICE as TOTAL_PR4_0_, orders0_.USER_ID as USER_ID0_ from ORDER_TABLE orders0_ where orders0_.USER_ID=?
Is the detached object re-attached to the Session?
Please post your mapping files for the two classes, and the code that reattaches the User to the session, and the session being commited.
I think we are missing something there.
Because here is what I know, when you reattach a detached object to a session, Hibernate will automatically assume it has changed and put an Update statement into the "queue", so since it is updated, it also wants to refresh the Orders collection, hence the second SQL, but it is never cascading to the orders collection.