-->
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: HQL returning duplicate objects
PostPosted: Thu Apr 29, 2004 6:32 am 
Newbie

Joined: Fri Apr 16, 2004 11:48 am
Posts: 18
Version: 2.1.3

I have ReferenceEntity and ReferenceObligation. A ReferenceEntity has many ReferenceObligations

Code:
<hibernate-mapping>
    <class
        name="ReferenceEntity"
        table="REFERENCE_ENTITY"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <set
            name="referenceObligations"
            lazy="false"
            inverse="true"
            cascade="all-delete-orphan"
            sort="unsorted"
        >

              <key
                  column="referenceEntityId"
              />

              <one-to-many
                  class="ReferenceObligation"
              />
        </set>

    </class>

</hibernate-mapping>



Code:

<hibernate-mapping>
    <class
        name="ReferenceObligation"
        table="REFERENCE_OBLIGATION"
        dynamic-update="false"
        dynamic-insert="false"
    >

        <id
            name="id"
            column="id"
            type="java.lang.Long"
        >
            <generator class="native">
            </generator>
        </id>

        <property
            name="name"
            type="java.lang.String"
            update="true"
            insert="true"
            column="name"
        />

        <many-to-one
            name="referenceEntity"
            class="ReferenceEntity"
            cascade="none"
            outer-join="auto"
            update="true"
            insert="true"
            column="referenceEntityId"
            not-null="true"
        />

    </class>

</hibernate-mapping>


I have a simple HQL query like this:
"SELECT referenceEntity from ReferenceEntity AS referenceEntity join referenceEntity.referenceObligations as referenceObligation "
Executed like this:
Code:
Query query = session.createQuery(queryString);
List resultList = query.list();

This query now returns multiple times a ReferenceEntity object (exactely each time for the number of ReferenceObligations). Am I missing something?

Many thanks!!!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 6:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
SELECT referenceEntity from ReferenceEntity AS referenceEntity left fetch join referenceEntity.referenceObligations as referenceObligation


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 10:20 am 
Newbie

Joined: Fri Apr 16, 2004 11:48 am
Posts: 18
Many thanks but actually that does not work either.

Code:
      List resultList = query.list();
      Set  resultSet = new HashSet();
      resultSet.addAll(resultList);
      System.out.println("------------------");
      System.out.println("Result count: " + resultList.size());
      System.out.println("Result count: " + resultSet.size());


Prints:

Result count: 985
Result count: 878

Any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 11:16 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
This works exactly the same as in SQL, meaning joining tables gives you duplicates. Use Distinct or do just like you did.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 30, 2004 5:52 am 
Newbie

Joined: Fri Apr 16, 2004 11:48 am
Posts: 18
Many thanks. Distinct does the trick.


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.