Question

one hibernate select generating lot of sql statement when using property-ref

Asked by: aturetsky

(This is in a sense a follow up to my question http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_21396914.html#13879622
which was successfully answered. The difference in this case is that the association that's resulting in multiple SQL statements being generated is mapped using a property-ref attribute, and the solution to the question mentioned above does not seem to work in this case.)

Anyhow, here the problem:
I have a class called Po, a member of which has an associated RfqReqs. The latter has a field that keeps track of this relationship. So the Po class mapping has the following line: <one-to-one name="rfqReqs" class="RfqReqs" property-ref="reqPo"/>

while the RfqReqs class mapping has the following line:
<one-to-one name="rfqReqs" class="RfqReqs" property-ref="reqPo"/>

It is mapped that way based on the recommendation on page 221-222 of Hibernate in Action.

I have the RfqReqs class declared as lazy, so that it's not pulled up every time I do a find on a Po class. When I do need it, however, I retrieve it using the "left join fetch po.rfqReqs" as recommended in the answer to my question mentioned above, where, like here, I was looking for a way to prevent multiple SQL statements from being generated.

In this case, however, that solution does not seem to work: I still get multiple SQL statements generated as a result of the RfqReqs association and I suspect it has to do with the fact that I am using the property-ref attribute here.


Please help.


Hibernate version:
2.7

Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.talisen.lbwebpo.model">
<class name="RfqReqs" table="RFQ_REQS_TABLE" lazy="true">
<id name="rfqReqsId" column="RFQ_REQS_ID" type="integer">
<generator class="native"/>
</id>
<many-to-one name="reqPo" column="REQ_ID" class="Po" unique="true"/>
<many-to-one name="rfq" column="RFQ_ID" class="Rfq"/>
</class>
</hibernate-mapping>


<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.talisen.lbwebpo.model">
<class name="Po" table="PO_LIST" where="SOURCE != 'WEBPO - IDS' AND active = 1" lazy="true">
<id name="poId" column="PO_ID" type="integer">
<generator class="native"/>
</id>

<property name="parentPoId" column="PARENT_PO_ID" type="integer" />
<property name="poNumber" column="PO_NUMBER" type="string" not-null="true" />
<property name="changeOrderSeq" column="CHANGE_ORDER_SEQ" type="string" />
<property name="source" column="SOURCE" type="string" not-null="true" />
<property name="poDate" column="PO_DATE" type="date" not-null="true" />
<property name="loadDate" column="LOAD_DATE" type="date" not-null="true" />
<property name="filename" column="FILENAME" type="string" not-null="true" />
<property name="loadInfo" column="LOAD_INFO" type="string" />
<property name="creator" column="CREATOR" type="string" />
<property name="supplierName" column="SUPPLIER_NAME" type="string" />
<property name="supplierNumber" column="SUPPLIER_NUMBER" type="integer" />
<property name="buyerCode" column="BUYER_CODE" type="string" />
<property name="buyerDept" column="BUYER_DEPT" type="string" />
<property name="contract" column="CONTRACT" type="string" />
<property name="active" column="ACTIVE" type="byte" not-null="true" />
<property name="poTypeId" column="PO_TYPE_ID" type="integer" not-null="true"/>
<one-to-one name="rfqReqs" class="RfqReqs" property-ref="reqPo"/>
</class>
</hibernate-mapping>

Data Retrieval Code:
reqListForOneBuyerCode = session.find("from Po po left join fetch po.rfqReqs order by po.poDate asc where po.poTypeId=11 and po.buyerCode='" + buyerCode[i] + "'");
Full stack trace of any exception that occurs:
N/A

Database I'm using:
Oracle 9i

The generated SQL :
Hibernate: select po0_.PO_ID as PO_ID0_, rfqreqs1_.RFQ_REQS_ID as RFQ_REQS1_1_, po0_.PARENT_PO_ID as PARENT_P2_0_, po0_.PO_NUMBER as PO_NUMBER0_, po0_.CHANGE_ORDER_SEQ as CHANGE_O4_0_, po0_.SOURCE as SOURCE0_, po0_.PO_DATE as PO_DATE0_, po0_.LOAD_DATE as LOAD_DATE0_, po0_.FILENAME as FILENAME0_, po0_.LOAD_INFO as LOAD_INFO0_, po0_.CREATOR as CREATOR0_, po0_.SUPPLIER_NAME as SUPPLIE11_0_, po0_.SUPPLIER_NUMBER as SUPPLIE12_0_, po0_.BUYER_CODE as BUYER_CODE0_, po0_.BUYER_DEPT as BUYER_DEPT0_, po0_.CONTRACT as CONTRACT0_, po0_.ACTIVE as ACTIVE0_, po0_.PO_TYPE_ID as PO_TYPE_ID0_, rfqreqs1_.REQ_ID as REQ_ID1_, rfqreqs1_.RFQ_ID as RFQ_ID1_ from PO_LIST po0_ left outer join RFQ_REQS_TABLE rfqreqs1_ on po0_.PO_ID=rfqreqs1_.REQ_ID where po0_.SOURCE != 'WEBPO - IDS' AND po0_.active = 1 and ((po0_.PO_TYPE_ID=11 )and(po0_.BUYER_CODE='N1R' )) order by po0_.PO_DATE asc

And then this statement about twenty times:

Hibernate: select rfqreqs0_.RFQ_REQS_ID as RFQ_REQS1_2_, rfqreqs0_.REQ_ID as REQ_ID2_, rfqreqs0_.RFQ_ID as RFQ_ID2_, rfq1_.RFQ_ID as RFQ_ID0_, rfq1_.ISSUED_BY as ISSUED_BY0_, rfq1_.RELEASE_DATE as RELEASE_3_0_, rfq1_.BID_CLOSE_DATE as BID_CLOS4_0_, rfq1_.TAX_INFORMATION as TAX_INFO5_0_, rfq1_.FOB as FOB0_, rfq1_.SPECIAL_INSTRUCTIONS as SPECIAL_7_0_, rfq1_.RFQ_CONSTANT as RFQ_CONS8_0_, rfq1_.RFQ_BUYER_CODE as RFQ_BUYE9_0_, rfq1_.RFQ_BLUES_CODE as RFQ_BLU10_0_, rfq1_.RFQ_SEQ_REVISION_NUMBER as RFQ_SEQ11_0_, rfq1_.RFQ_STATUS as RFQ_STATUS0_, rfq1_.PO_ID as PO_ID0_, rfq1_.STATUS as STATUS0_, rfq1_.INCREMENTAL_PRICE_QUOTE as INCREME15_0_, rfq1_.PROCUREMENT_NOTES as PROCURE16_0_, po2_.PO_ID as PO_ID1_, po2_.PARENT_PO_ID as PARENT_P2_1_, po2_.PO_NUMBER as PO_NUMBER1_, po2_.CHANGE_ORDER_SEQ as CHANGE_O4_1_, po2_.SOURCE as SOURCE1_, po2_.PO_DATE as PO_DATE1_, po2_.LOAD_DATE as LOAD_DATE1_, po2_.FILENAME as FILENAME1_, po2_.LOAD_INFO as LOAD_INFO1_, po2_.CREATOR as CREATOR1_, po2_.SUPPLIER_NAME as SUPPLIE11_1_, po2_.SUPPLIER_NUMBER as SUPPLIE12_1_, po2_.BUYER_CODE as BUYER_CODE1_, po2_.BUYER_DEPT as BUYER_DEPT1_, po2_.CONTRACT as CONTRACT1_, po2_.ACTIVE as ACTIVE1_, po2_.PO_TYPE_ID as PO_TYPE_ID1_ from RFQ_REQS_TABLE rfqreqs0_ left outer join RFQ_TABLE rfq1_ on rfqreqs0_.RFQ_ID=rfq1_.RFQ_ID left outer join PO_LIST po2_ on rfq1_.PO_ID=po2_.PO_ID where rfqreqs0_.REQ_ID=?

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-05-03 at 15:50:33ID21411639
Tags

hibernate

Topic

Java Programming Language

Participating Experts
1
Points
500
Comments
9

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Tomcat and Hibernate
    Hi, I am sooo close to getting this working :) Everything with Hibernate seems fine but the problem is setting up the database pool itself. I have what I believe to be the right config but I don't know where in server.xml it should go ( if indeed that is where it need...
  2. hibernate exception: invalid identifier
    when doing a session.find(from RfqResponse) query I get the following error: java.sql.SQLException: ORA-00904: "ORGANIZATI1_"."LBSS": invalid identifier here's the full error output: testEverything(com.talisen.lbwebpo.dao.test)net.sf.hibernate.except...
  3. Hibernate
    Hi Present we are using Oracle stored procedures and Callable statements to invoke those procedures to deal with data in our applicaiton JDBC part. Now we are planning to re-develop that part using Hibernate. I have some doubts 1. Can we write complex queries using H...
  4. Hibernate and stored procedures examples
    My development team is currently trying to integrate hibernate into a few new jsf (java-based) applications we are working on. Alot of our apps depend on stored procedures and we have not been able to find any clear cut examples of using stored procedures with hibernate. Co...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: aozarovPosted on 2005-05-04 at 20:51:03ID: 13932897

>> I suspect it has to do with the fact that I am using the property-ref attribute here.
I don't think so.

>> And then this statement about twenty times:
This is probably because you have another many-to-one relation in RfqReqs (to Rfq class).

The left join fetch po.rfqReqs was doing it correctly and loaded rfqReqs columns when you applied the finder as
there was no request to join/load the related rfq.

I am not sure if you can apply multiple join fetch in HQL but you can try somethig like that:
reqListForOneBuyerCode = session.find("from Po po left join fetch po.rfqReqs rfqReqs left join fetch rfqReqs.rfq order by po.poDate asc where po.poTypeId=11 and po.buyerCode='" + buyerCode[i] + "'");

 

by: aturetskyPosted on 2005-05-05 at 07:26:33ID: 13936166

It's interesting that you mention this.  I thought it was that at first, but then I tried it out without the rfq element altogether and it was still generating multiple sqls.

Also, if you look at the sqls, they are not going against the rfq_table, but against the rfq_reqs_table.

 

by: aozarovPosted on 2005-05-05 at 10:11:07ID: 13937908

>> Also, if you look at the sqls, they are not going against the rfq_table, but against the rfq_reqs_table.
The first query joins against RFQ_REQS_TABLE, "po0_ left outer join RFQ_REQS_TABLE", as one expect after having "po left join fetch po.rfqReqs" in your finder.
 
>>  I thought it was that at first, but then I tried it out without the rfq element altogether and it was still generating multiple sqls.
Did you remove from RfqReqs class and RfqReqs hibernate definition any reference to rfq and still got the extra queries?
I am pretty sure that should not be the case as those extra queries fetch the RFQ parts of RFQ_REQS (after that was loaded in the first query), see "from RFQ_REQS_TABLE rfqreqs0_ left outer join RFQ_TABLE ".

 

by: aturetskyPosted on 2005-05-05 at 10:18:54ID: 13937980

ok, thanks so much - I'm putting out another fire right now, but will try this ASAP

 

by: aturetskyPosted on 2005-06-01 at 09:58:28ID: 14123528

>>Did you remove from RfqReqs class and RfqReqs hibernate definition any reference to rfq and still got the extra queries?
Yes, I have - and the problem still persists.

>>I am pretty sure that should not be the case as those extra queries fetch the RFQ parts of RFQ_REQS (after that was loaded in the first query), see "from RFQ_REQS_TABLE rfqreqs0_ left outer join RFQ_TABLE ".
Well, yeah, but even when I remove any reference to rfq, it still executes the extra selects, except those selects no longer say left outer join RFQ_TABLE.

Any thoughts?

Alex

 

by: aturetskyPosted on 2005-06-07 at 15:12:07ID: 14166463

any suggestions?

 

by: aozarovPosted on 2005-06-08 at 08:47:34ID: 14171821

This: PO_LIST po2_ on rfq1_.PO_ID=po2_.PO_ID
suggest that there is a direct relation between rfq to Po.
Can you provide the hibernate-mapping for the RFQ class ? What happens if you take out this (probably one side) relation between RFQ to PO ?

 

by: aturetskyPosted on 2005-07-20 at 12:16:12ID: 14488264

sorry, haven't had a chance to respond - I've modified the rfq mapping since so I have to put in some time to recreate the problem, but I still want to get to the bottom of this.
I will respond later.  Once again, sorry for the delay.

Please do not remove this question.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...