wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Why a select statement, not an insert ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why a select statement, not an insert ?" Watch "Why a select statement, not an insert ?" New topic
Author

Why a select statement, not an insert ?

Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Hussein Baghdadi
clojure forum advocate
Bartender

Joined: Nov 08, 2003
Posts: 3476

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Nov 08, 2003
Posts: 3476

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

Joined: Feb 05, 2001
Posts: 17249
    
    6

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

Joined: Nov 08, 2003
Posts: 3476

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why a select statement, not an insert ?
 
Similar Threads
Same old song, same old dance, Lazy Hibernate
HFE - Error in mock exam answer in page 434?
well can you tell how transfer a List eg.
EJB-QL question
xls:sort question