-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Joins not working as expected
PostPosted: Fri Mar 24, 2006 6:02 pm 
Newbie

Joined: Mon Apr 25, 2005 4:21 pm
Posts: 7
I'm using 3.1.3 (though 3.1.1 had the same problem) and am having a strange problem. This is a legacy DB and it would not be possible to change the structure. My mapping looks something like:

Code:
<class name="DBDrug" table="DRUG">
    <composite-id>
        <key-property name="facilityId" type="string">
            <column name="FACILITY_ID" length="2" />
        </key-property>

        <key-property name="drugId" type="string">
            <column name="DRUG_ID" length="12" />
        </key-property>
    </composite-id>

    <property name="primaryName" type="string">
        <column name="PRIMARY_NAME" length="50" />
    </property>

...      
    <join table="DRUG_BARCODE">
        <key>
            <column name="FACILITY_ID" length="2" />
            <column name="DRUG_ID" length="12" />
        </key>

        <property name="drugBarcode" type="string">
            <column name="DRUG_BARCODE" length="40" />
        </property>
...
    </join>
</class>


and I select from the DRUG table with:

Code:
items = session.createQuery( "from DBDrug where drugId = ? and facilityId = ?" )
    .setString(0, drugID)
    .setString(1, facilityID).iterate();


When I look at the SQL that is generated there are two queries. The first returns just the primary key information and the second returns everything. The problem is that I can't get the data from the "everything" query - only the "key" query. To illustrate better.

The "key" query (cleaned up for formatting)

Code:
select dbdrug0_.FACILITY_ID as col_0_0_,
          dbdrug0_.DRUG_ID as col_0_1_
from DRUG dbdrug0_
inner join DRUG_BARCODE dbdrug0_1_ on
               dbdrug0_.FACILITY_ID=dbdrug0_1_.FACILITY_ID and
               dbdrug0_.DRUG_ID=dbdrug0_1_.DRUG_ID
where dbdrug0_.DRUG_ID=? and dbdrug0_.FACILITY_ID=?


and the "everything" query (also cleaned up for formatting):

Code:
select dbdrug0_.FACILITY_ID as FACILITY1_18_0_,
         dbdrug0_.DRUG_ID as DRUG2_18_0_,
         dbdrug0_.PRIMARY_NAME as PRIMARY3_18_0_,
...
         dbdrug0_1_.DRUG_BARCODE as DRUG3_19_0_,
from DRUG dbdrug0_
inner join DRUG_BARCODE dbdrug0_1_ on
         dbdrug0_.FACILITY_ID=dbdrug0_1_.FACILITY_ID
         and dbdrug0_.DRUG_ID=dbdrug0_1_.DRUG_ID
where dbdrug0_.FACILITY_ID=? and dbdrug0_.DRUG_ID=?


So Hibernate is doing the right thing eventually (running the second query by hand results in the expected data) but the result of the createQuery only has my facilityID and drugID set - nothing else.

Any thoughts as to where I'm going wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 24, 2006 6:59 pm 
Newbie

Joined: Mon Apr 25, 2005 4:21 pm
Posts: 7
Update: if I change from iterate() to uniqueResult() I get the expected results. When I iterate there is only one result - why is it not the correct one?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 27, 2006 12:29 pm 
Newbie

Joined: Mon Apr 25, 2005 4:21 pm
Posts: 7
Update 2: I found what the issue is. One of the other columns I had was returning a null but the reverse engineering tool made it a long, not a java.lang.Long. For some reason this forced the result set to use the key version, not the complete version. Now that it works the two queries are still there but the data is populated correctly.

Another day, another two Hibernate bugs. And don't even get me started with columns named "id".


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.