Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why a select statement, not an insert ?

 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi John. I am trying to follow, but got a little confused. Is there ever an Insert statement run on the Order table?

Can you post your console output that you see?

Thanks

Mark
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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=?
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Mark
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
User.hbm.xml:

And Order.hbm.xml :

(Note, I removed some simple properties in order not make the post big).
I use Spring in the middleware, the following is in OrderAction Struts action :

Thanks Mark.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, I just caught this but

"all-delete-orphan"

Shouldn't that be "all, delete-orphan" I don't know of a cascade option called "all-delete-orphan" Which would be a good reason why it wasn't working.

Mark
 
Hussein Baghdadi
clojure forum advocate
Bartender
Posts: 3479
Clojure Mac Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry Mark, but what is wrong with all-delete-orphan ?
I'm using Hibernate2 and all-delete-orphan is a valid value.
Am I missing something ?
Thanks for your time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic