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