-->
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.  [ 4 posts ] 
Author Message
 Post subject: Fetch Join returns all child POs in collection as parent PO
PostPosted: Mon Aug 02, 2004 2:48 pm 
Regular
Regular

Joined: Tue Nov 11, 2003 7:35 pm
Posts: 63
I have a PO table called CspCmsRgnCd which represents a geographical region (such as "Eastern US" or "Southern US") and has a collection of all associated states in the region, represented by POs called CspAllStateCdVw, one for each state. I am trying through an HQL query to load all regions and associated states in one sql query. I tried the following HQL:

Code:
select r from CspCmsRgnCd r
left outer join fetch r.cspAllStateCdVws
order by r.cdId


I have 10 regions and 50 states, and I expected 10 CspCmsRgnCd POs as a result of this query. However, what I got back was 60 POs instead, most of them identical. For example, if the Eastern US region has 12 states in it, I get back 12 identical POs, each fully populated, instead of 1 PO representing the region along with the populated cspAllStateCdVws collection. Why is this happening?

I am using Hibernate 2.1.4 and Oracle. Following are the hbm and java files for these objects.

Thanks in advance,
Daniel Rosenbaum

Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <!--
    Created by the Middlegen Hibernate plugin 2.1

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->
  <class name="org.ifmc.qies.qw.data.po.CspAllStateCdVw" table="CSP_ALL_STATE_CD_VW" schema="qies_dbowner">
    <id name="stateCd" column="STATE_CD" type="java.lang.String" length="2">
      <generator class="assigned"/>
    </id>
    <property name="cmsRgnCd" column="CMS_RGN_CD" type="java.lang.String" length="2"/>
    <!-- bi-directional one-to-many association to CspStateRgnCd -->
    <set name="cspStateRgnCds" lazy="true" inverse="true">
      <key>
        <column name="STATE_CD"/>
      </key>
      <one-to-many class="org.ifmc.qies.qw.data.po.CspStateRgnCd"/>
    </set>
    <!-- uni-directional one-to-many association to CspCntyCd -->
    <set name="cspCntyCds" lazy="true">
      <key>
        <column name="STATE_CD"/>
      </key>
      <one-to-many class="org.ifmc.qies.qw.data.po.CspCntyCd"/>
    </set>
    <!-- bi-directional one-to-many association to CspPrvdr -->
    <set name="cspPrvdrs" lazy="true" inverse="true">
      <key>
        <column name="STATE_CD"/>
      </key>
      <one-to-many class="org.ifmc.qies.qw.data.po.CspPrvdr"/>
    </set>
    <property name="stateName" column="STATE_NAME" type="java.lang.String" length="20"/>
    <property name="stateAbrvtnCd" column="STATE_ABRVTN_CD" type="java.lang.String" length="2"/>
    <property name="ssaStateCd" column="SSA_STATE_CD" type="java.lang.String" length="2"/>
  </class>
</hibernate-mapping>


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
  <!--
    Created by the Middlegen Hibernate plugin 2.1

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->
  <class name="org.ifmc.qies.qw.data.po.CspCmsRgnCd" table="CSP_CMS_RGN_CD" schema="qies_dbowner">
    <meta attribute="implements" inherit="false">org.ifmc.qies.qw.hibernate.Auditable</meta>
    <id name="cdId" type="java.lang.String" column="CD_ID">
      <generator class="assigned"/>
    </id>
    <property name="cdDesc" type="java.lang.String" column="CD_DESC" length="400"/>
    <property name="cnsrtCd" type="java.lang.String" column="CNSRT_CD" length="2"/>
    <property name="efctvFromDt" type="java.sql.Timestamp" column="EFCTV_FROM_DT" length="7"/>
    <property name="efctvThruDt" type="java.sql.Timestamp" column="EFCTV_THRU_DT" length="7"/>
    <property name="creatTs" type="java.sql.Timestamp" column="CREAT_TS" length="7"/>
    <property name="creatUserId" type="java.lang.String" column="CREAT_USER_ID" length="20"/>
    <property name="updtTs" type="java.sql.Timestamp" column="UPDT_TS" length="7"/>
    <property name="updtUserId" type="java.lang.String" column="UPDT_USER_ID" length="20"/>
    <!-- Associations -->
    <!-- uni-directional one-to-many association to CspAllStateCdVw -->
    <set name="cspAllStateCdVws" lazy="true" order-by="ssa_state_cd" outer-join="auto">
      <key>
        <column name="CMS_RGN_CD"/>
      </key>
      <one-to-many class="org.ifmc.qies.qw.data.po.CspAllStateCdVw"/>
    </set>
  </class>
</hibernate-mapping>

Code:
package org.ifmc.qies.qw.data.po;

import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class CspAllStateCdVw implements Serializable {

    /** identifier field */
    private String stateCd;

    /** nullable persistent field */
    private String cmsRgnCd;

    /** nullable persistent field */
    private String stateName;

    /** nullable persistent field */
    private String stateAbrvtnCd;

    /** nullable persistent field */
    private String ssaStateCd;

    /** persistent field */
    private Set cspStateRgnCds;

    /** persistent field */
    private Set cspCntyCds;

    /** persistent field */
    private Set cspPrvdrs;

    /** full constructor */
    public CspAllStateCdVw(String stateCd, String cmsRgnCd, String stateName, String stateAbrvtnCd, String ssaStateCd, Set cspStateRgnCds, Set cspCntyCds, Set cspPrvdrs) {
        this.stateCd = stateCd;
        this.cmsRgnCd = cmsRgnCd;
        this.stateName = stateName;
        this.stateAbrvtnCd = stateAbrvtnCd;
        this.ssaStateCd = ssaStateCd;
        this.cspStateRgnCds = cspStateRgnCds;
        this.cspCntyCds = cspCntyCds;
        this.cspPrvdrs = cspPrvdrs;
    }

    /** default constructor */
    public CspAllStateCdVw() {
    }

    /** minimal constructor */
    public CspAllStateCdVw(String stateCd, Set cspStateRgnCds, Set cspCntyCds, Set cspPrvdrs) {
        this.stateCd = stateCd;
        this.cspStateRgnCds = cspStateRgnCds;
        this.cspCntyCds = cspCntyCds;
        this.cspPrvdrs = cspPrvdrs;
    }

    public String getStateCd() {
        return this.stateCd;
    }

    public void setStateCd(String stateCd) {
        this.stateCd = stateCd;
    }

    public String getCmsRgnCd() {
        return this.cmsRgnCd;
    }

    public void setCmsRgnCd(String cmsRgnCd) {
        this.cmsRgnCd = cmsRgnCd;
    }

    public String getStateName() {
        return this.stateName;
    }

    public void setStateName(String stateName) {
        this.stateName = stateName;
    }

    public String getStateAbrvtnCd() {
        return this.stateAbrvtnCd;
    }

    public void setStateAbrvtnCd(String stateAbrvtnCd) {
        this.stateAbrvtnCd = stateAbrvtnCd;
    }

    public String getSsaStateCd() {
        return this.ssaStateCd;
    }

    public void setSsaStateCd(String ssaStateCd) {
        this.ssaStateCd = ssaStateCd;
    }

    public Set getCspStateRgnCds() {
        return this.cspStateRgnCds;
    }

    public void setCspStateRgnCds(Set cspStateRgnCds) {
        this.cspStateRgnCds = cspStateRgnCds;
    }

    public Set getCspCntyCds() {
        return this.cspCntyCds;
    }

    public void setCspCntyCds(Set cspCntyCds) {
        this.cspCntyCds = cspCntyCds;
    }

    public Set getCspPrvdrs() {
        return this.cspPrvdrs;
    }

    public void setCspPrvdrs(Set cspPrvdrs) {
        this.cspPrvdrs = cspPrvdrs;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("stateCd", getStateCd())
            .toString();
    }

    public boolean equals(Object other) {
        if ( (this == other ) ) return true;
        if ( !(other instanceof CspAllStateCdVw) ) return false;
        CspAllStateCdVw castOther = (CspAllStateCdVw) other;
        return new EqualsBuilder()
            .append(this.getStateCd(), castOther.getStateCd())
            .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getStateCd())
            .toHashCode();
    }

}


Code:
package org.ifmc.qies.qw.data.po;

import java.io.Serializable;
import java.util.Date;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;


/** @author Hibernate CodeGenerator */
public class CspCmsRgnCd implements org.ifmc.qies.qw.hibernate.Auditable,Serializable {

    /** identifier field */
    private String cdId;

    /** nullable persistent field */
    private String cdDesc;

    /** nullable persistent field */
    private String cnsrtCd;

    /** nullable persistent field */
    private Date efctvFromDt;

    /** nullable persistent field */
    private Date efctvThruDt;

    /** nullable persistent field */
    private Date creatTs;

    /** nullable persistent field */
    private String creatUserId;

    /** nullable persistent field */
    private Date updtTs;

    /** nullable persistent field */
    private String updtUserId;

    /** persistent field */
    private Set cspAllStateCdVws;

    /** full constructor */
    public CspCmsRgnCd(String cdId, String cdDesc, String cnsrtCd, Date efctvFromDt, Date efctvThruDt, Date creatTs, String creatUserId, Date updtTs, String updtUserId, Set cspAllStateCdVws) {
        this.cdId = cdId;
        this.cdDesc = cdDesc;
        this.cnsrtCd = cnsrtCd;
        this.efctvFromDt = efctvFromDt;
        this.efctvThruDt = efctvThruDt;
        this.creatTs = creatTs;
        this.creatUserId = creatUserId;
        this.updtTs = updtTs;
        this.updtUserId = updtUserId;
        this.cspAllStateCdVws = cspAllStateCdVws;
    }

    /** default constructor */
    public CspCmsRgnCd() {
    }

    /** minimal constructor */
    public CspCmsRgnCd(String cdId, Set cspAllStateCdVws) {
        this.cdId = cdId;
        this.cspAllStateCdVws = cspAllStateCdVws;
    }

    public String getCdId() {
        return this.cdId;
    }

    public void setCdId(String cdId) {
        this.cdId = cdId;
    }

    public String getCdDesc() {
        return this.cdDesc;
    }

    public void setCdDesc(String cdDesc) {
        this.cdDesc = cdDesc;
    }

    public String getCnsrtCd() {
        return this.cnsrtCd;
    }

    public void setCnsrtCd(String cnsrtCd) {
        this.cnsrtCd = cnsrtCd;
    }

    public Date getEfctvFromDt() {
        return this.efctvFromDt;
    }

    public void setEfctvFromDt(Date efctvFromDt) {
        this.efctvFromDt = efctvFromDt;
    }

    public Date getEfctvThruDt() {
        return this.efctvThruDt;
    }

    public void setEfctvThruDt(Date efctvThruDt) {
        this.efctvThruDt = efctvThruDt;
    }

    public Date getCreatTs() {
        return this.creatTs;
    }

    public void setCreatTs(Date creatTs) {
        this.creatTs = creatTs;
    }

    public String getCreatUserId() {
        return this.creatUserId;
    }

    public void setCreatUserId(String creatUserId) {
        this.creatUserId = creatUserId;
    }

    public Date getUpdtTs() {
        return this.updtTs;
    }

    public void setUpdtTs(Date updtTs) {
        this.updtTs = updtTs;
    }

    public String getUpdtUserId() {
        return this.updtUserId;
    }

    public void setUpdtUserId(String updtUserId) {
        this.updtUserId = updtUserId;
    }

    public Set getCspAllStateCdVws() {
        return this.cspAllStateCdVws;
    }

    public void setCspAllStateCdVws(Set cspAllStateCdVws) {
        this.cspAllStateCdVws = cspAllStateCdVws;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("cdId", getCdId())
            .toString();
    }

    public boolean equals(Object other) {
        if ( (this == other ) ) return true;
        if ( !(other instanceof CspCmsRgnCd) ) return false;
        CspCmsRgnCd castOther = (CspCmsRgnCd) other;
        return new EqualsBuilder()
            .append(this.getCdId(), castOther.getCdId())
            .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getCdId())
            .toHashCode();
    }

}


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 02, 2004 2:50 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
FAQ, an outer join doesn't return distinct results. Make them distinct yourself (new HashSet(result)) or wait for Hibernate3.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: Results incorrect when paging
PostPosted: Mon Aug 09, 2004 11:26 am 
Newbie

Joined: Mon Aug 09, 2004 11:24 am
Posts: 3
Making them distinct is fine. But if you want to page through the results then the results are incorrect when you have duplicates .


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 09, 2004 11:33 am 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
christian wrote:
FAQ, an outer join doesn't return distinct results. Make them distinct yourself (new HashSet(result)) or wait for Hibernate3.


I hope I am not getting prematurely giddy in thinking that distinct results will be a feature in Hibernate 3?


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