-->
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.  [ 5 posts ] 
Author Message
 Post subject: Strange Query Result
PostPosted: Thu Sep 11, 2003 2:42 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:32 pm
Posts: 28
Hi everyone,

I am experiencing something really strange with my query result and I would like to find out if I am doing something wrong. I am trying to run a HQL query under the following two mapping files:

Here is the mapping of my customer class:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>


<class
    name="com.accucast4.testcase5.Customer"
    table="CUSTOMER"
>
    <id
        name="custId"
        type="long"
        column="CUST_ID"
    >
        <generator class="assigned" />
    </id>
    <property
        name="firstName"
        type="java.lang.String"
        column="FIRST_NAME"
        length="20"
    />

    <!-- bi-directional one-to-many association to Recipient -->
    <map
        name="recipients"
        lazy="true"
        inverse="true"
   cascade="all"   
    >
        <key>
            <column name="RC_C_ID" />
        </key>
        <composite-index class="com.accucast4.testcase5.RecipientPK">
            <key-property name="rcDId" type="long" column="RC_D_ID" />
        </composite-index>
        <one-to-many
            class="com.accucast4.testcase5.Recipient"
        />
    </map>

</class>
</hibernate-mapping>


Here is the mapping of the recipient class:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>

<class
    name="com.accucast4.testcase5.Recipient"
    table="RECIPIENT"
>
    <composite-id name="recip_comp_id" class="com.accucast4.testcase5.RecipientPK">
        <key-property name="rcDId" type="long" column="RC_D_ID" />
        <key-property name="rcCId" type="long" column="RC_C_ID" />
    </composite-id>
   
    <property
        name="rcCreatedDttm"
        type="java.sql.Timestamp"
        column="RC_CREATED_DTTM"
        length="7"
    />

    <!-- associations -->
    <!-- bi-directional many-to-one association to Customer -->
    <many-to-one
        name="customer"
        class="com.accucast4.testcase5.Customer"
        not-null="true"
   update="false"
   insert="false"
    >
        <column name="RC_C_ID" />
    </many-to-one>

</class>
</hibernate-mapping>


Here is the HQL query:
Code:
select customer from Customer as customer
left outer join customer.custAccucastData as custaccucastdata
inner join customer.recipients as recipients
where recipients.recip_comp_id.rcDId = ?


Here is the method to traverse all customer objects plus child recipient objects:

Code:
    public void getCustAccucastDataWithRecipFields(){
        Session session = null;
        Iterator custIter = null;
        try {
            session = getSession();
            custIter=session.iterate("select customer from Customer as customer " +
                                     "inner join customer.recipients as recipients "+
                                     "where recipients.recip_comp_id.rcDId = ?",
                                     new Long(1234),
                                     Hibernate.LONG);
            while (custIter.hasNext()){
             Customer cust = (Customer)custIter.next();
             System.out.println("CustId: "+cust.getCustId());
             System.out.println("FirstName:"+cust.getFirstName());
             
             if (cust.getRecipients() == null){
                System.out.println("No recipient record");                 
             } else {
                 Map recipients = cust.getRecipients();
                 if (recipients.size() == 0) {
                    System.out.println("No recipient record");                 
                 } else{
                     Iterator keySetIter = recipients.keySet().iterator();
                     while (keySetIter.hasNext()){
                         RecipientPK recipPk = (RecipientPK) keySetIter.next();
                         Recipient recip = (Recipient)recipients.get(recipPk);
                         System.out.println("RecipDId:" + recip.getRecip_comp_id().getRcDId());
                     }
                 }
             }
            }
            session.close();
        } catch (HibernateException he){
            he.printStackTrace();
        }
    }


Here is the strange part is when I used this method to transverse the result, I can still see recipient records that do not belong to rcDId=1234. My question is: am I doing something wrong??? If yes, what should I do in order to avoid recipient records besides rcDId=1234 to show up??

I would really appreciate any suggestions on this issue.

Thanks

Vivian


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 11, 2003 11:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Use this query:


select customer, recipient
from Customer as customer
left join fetch customer.custAccucastData as custaccucastdata
inner join customer.recipients as recipients
where recipients.recip_comp_id.rcDId = ?


Which will return ordered pairs.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 3:22 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:32 pm
Posts: 28
Hi Gavin,

I can see where your suggestion comes from:
Quote:
select customer, recipient
from Customer as customer
left join fetch customer.custAccucastData as custaccucastdata
inner join customer.recipients as recipients
where recipients.recip_comp_id.rcDId = ?


But I want to know if the following situation is possible:

I have one customer (id = 1)record relates two recipient records ([rcDId = 1234, c_id =1], [rcDId = 2345, c_id = 1]) and another customer (id = 2) record relates to only one recipient record ([rcDId = 1234, c_id = 1]). When I issue the HQL:
Code:
select customer from Customer as customer
left outer join customer.custAccucastData as custaccucastdata
inner join customer.recipients as recipients
where recipients.recip_comp_id.rcDId = ?

set rcDId = 1234. I am expecting to see both customer objects (id = 1 and id = 2) to carry only one recipient object through customer object iteration. When I iterate the customer objects, I realize that customer object with id value = 1 still carries two recipient objects (rcDId = 1234 and rcDId = 2345). I thought my query should elinimate the chance of rcDId = 2345 to show up. Am I correct or I am wrong with my expectation?? If I am wrong, could you give me some explanation.

Thanks for any suggestion

Vivian


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 12, 2003 3:29 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You are wrong.

In Hibernate, objects have *identity*. You can't change the state of an object, ie. of its collection, by simply querying it!

That would be Evil.


Top
 Profile  
 
 Post subject: Query results explained in light of perception.
PostPosted: Fri Sep 12, 2003 6:01 pm 
Senior
Senior

Joined: Sun Aug 31, 2003 3:14 pm
Posts: 151
Location: Earth (at the moment)
Hi Vivian,

I had the same "problem" (perception) as you so it might benefit you to read Gavin's reply to me at http://forum.hibernate.org/viewtopic.php?t=356 rather than repeat the topic here.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.