This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes Why extra select on one to many mapping in hibernate ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Why extra select on one to many mapping in hibernate ?" Watch "Why extra select on one to many mapping in hibernate ?" New topic

Why extra select on one to many mapping in hibernate ?

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518

Hi All,

First of all, I apologize for a vague subject line as I could not think of anything better.

Coming to my problem, I'm trying to understand the inverse = "true/false" attribute being used in bidirectional 1-M and M-M associations as given in the link with a slight modifications.

I have Stock.hbm.xml as below

StockRecordDaily.hbm.xml as below

as you noticed, I did not use not-null = true in the definition, as I want to create an association at a later point, not while inserting the records, which otherwise would fail due to null constraint.

Now, I populated both STOCK and STOCKDAILYRECORD table with one record each and now trying to associate them as below

This ran fine, but I noticed the following SQL being generated by Hibernate

I did load the Stock object first and then the StockDailyRecord, but the first SQL is SELECT FROM STOCKDAILYRECORD. Not sure, if the order load has any impact on SQL being executed. if Yes, not sure in this case, it is reversed. Any explanation is appreciated and why STOCK_ID is selected twice as stockrecor0_.STOCK_ID as STOCK7_3_1_, and stockrecor0_.STOCK_ID as STOCK7_4_0_ . What is the purpose of SELECTING same column twice.

Third, I understood the last update, as that is my basic requirement, updating STOCKDAILYRECORD with STOCK_ID which is the result of stock.getStockRecords().add(record); and I do not expect an update of STOCK in response to record.setStock(stock); (As STOCK table will not have any COLUMN to hold Stock Record anyway).
But what is the purpose of Fourth update, where it is updating open price, close price, pricechange, date etc. Those values were already available for StockDailyRecord object. Why the update again and what is it going to achieve. This statement seem to be unnecessary to me. But why Hibernate choose to have this executed.

Could some one explain me these issues and why it is happening.


Secondly, once the StockDailyRecord is loaded through first SQL, I did not understand why another SQL SELECT is issued again on STOCKDAILYRECORD through third SQL


Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518

after some more exercise, and changing the "fetch" attribute to "join" instead of "select", I was able to avoid an extra SELECT ON STOCKDAILYRECORD and made the StockDailyRecord to manage the association, by putting "inverse = true". Now the SQL is looking as below

So compared to my earlier post, I was able to remove an extra SELECT, which I thought was unnecessary and and update as now, I believe the association is managed by StockDailyRecord instead of Stock.

So, now I'm thinking "fetch=join" seem to be more faster compared to "fetch=select". If yes, why do we need "Select" as an option at the first place.

secondly, though I see only one update now, I still see other columns are also being updated along with STOCK_ID. but why? Instead

Kumar Raja
Ranch Hand

Joined: Mar 18, 2010
Posts: 518

Any thoughts on this question
It is sorta covered in the JavaRanch Style Guide.
subject: Why extra select on one to many mapping in hibernate ?
Similar Threads
SQL-Query not returning specified Object
Help needed on the following example?
Having trouble in understanding bi directional relationships
how to save collection object into database
Repeated updates on EVENT table, in Hibernate one to many mapping