• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why extra select on one to many mapping in hibernate ?

 
Ranch Hand
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

Thanks

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
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 558
2
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any thoughts on this question
 
You don't like waffles? Well, do you like this tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic