-->
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.  [ 10 posts ] 
Author Message
 Post subject: Criteria returning multiple copies of same object
PostPosted: Fri Jan 26, 2007 4:44 pm 
Newbie

Joined: Fri Jan 26, 2007 3:44 pm
Posts: 4
Location: Kansas, US
The following 2 sections of code are returning a different number of results, which appears to be a Hibernate bug. Is there any reason (odd config, etc) that this might not be a bug?

Option 1
Code:
Criteria crit = session.createCriteria(treeDefClass);
List<?> results = crit.list();


Option 2
Code:
Query q = session.createQuery("from " + treeDefClass.getSimpleName() + " as def");
List<?> results = q.list();


The first option returns 400 copies of the same object (same in the sense of the Java = operator, meaning same identity, not just object equality). The second option returns the correct result, 1 object.

Any ideas?

In case it matters...
I'm using Hibernate 3.2.1 GA and Annotations 3.2.1 GA on a Windows XP SP 2 host running a MySQL 5.0.27 DB.


Top
 Profile  
 
 Post subject: Criteria returning too mayn results with eager fetching
PostPosted: Wed Jan 31, 2007 2:49 pm 
Newbie

Joined: Fri Jan 26, 2007 3:44 pm
Posts: 4
Location: Kansas, US
This appears to be related to EAGER fetching. I setup my DB to have 1 'Parent' record and 4 'Child' records, based on the following two classes...
Code:
package datamodel;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
*
* @code_status Alpha
* @author jayhawk
*/
@Entity(name="parent")
public class Parent
{
    protected Long id;
    protected String name;
    protected Set<Child> children;
   
    @Id
    public Long getId()
    {
        return id;
    }
    public void setId(Long id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
   
    @OneToMany(mappedBy="parent", fetch=FetchType.EAGER)
    public Set<Child> getChildren()
    {
        return children;
    }
    public void setChildren(Set<Child> children)
    {
        this.children = children;
    }
}

package datamodel;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

/**
*
* @code_status Alpha
* @author jayhawk
*/
@Entity(name="child")
public class Child
{
    protected Long id;
    protected String name;
    protected Parent parent;
   
    @Id
    public Long getId()
    {
        return id;
    }
    public void setId(Long id)
    {
        this.id = id;
    }
   
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
   
    @ManyToOne
    public Parent getParent()
    {
        return parent;
    }
    public void setParent(Parent parent)
    {
        this.parent = parent;
    }
}


When I leave fetch=FetchType.EAGER on the getChildren() method, a Criteria query returns 4 results from my DB (one for each child). Using show_sql, I see that Hibernate is doing a join.

When I remove fetch=FetchType.EAGER and let the system default to LAZY, I get only 1 result, the proper number.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 31, 2007 3:12 pm 
Newbie

Joined: Fri Jan 26, 2007 3:44 pm
Posts: 4
Location: Kansas, US
I found another posting about this problem. It suggested putting...
Code:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);

after creating each Criteria. This seems to work, but I have no idea what it's actually doing. I'll have to spend some time figuring that out.

Is this safe to do on ALL Criteria queries?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 3:09 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi jayhawk,

Have you extend Parent Or Child and mapped these with other HBM file

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 01, 2007 11:16 am 
Newbie

Joined: Fri Jan 26, 2007 3:44 pm
Posts: 4
Location: Kansas, US
dharmendra.pandey wrote:
Have you extend Parent Or Child and mapped these with other HBM file


No I haven't. Parent and Child are both as given above, and no subclasses exist for either of them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:12 am 
Newbie

Joined: Mon Aug 28, 2006 12:01 pm
Posts: 11
Putting criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) on each query will cancel the pagination feature, since it filters the results on the hibernate side and not on the DB, while the pagination works on the DB side.

Is it safe to use it, if you do not need pagination.


I have the same problem with the same results returned by the Criteria API queries, and I have to use eager fetching due the nature of my application. Still found no proper solution.

There is a feature request opened for a few years to add "distinct" option to the Criteria API, but no one seems to care.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:24 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
http://www.hibernate.org/117.html#A12

You can't do SQL row-based pagination when you do outer join fetching. No "distinct" keyword is going to help you with that. Just look at the SQL resultset.

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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 02, 2007 5:11 pm 
Newbie

Joined: Mon Aug 28, 2006 12:01 pm
Posts: 11
Is there a way not to use "OUTER" join when working with Criteria API ?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 3:52 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sure, you can use an inner join (inner join collection fetching rarely makes sense). But its the same problem, because the resultset also contains duplicates that conflict with row-based limits...

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


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 03, 2007 4:51 am 
Newbie

Joined: Mon Aug 28, 2006 12:01 pm
Posts: 11
Just to make it clear, THE ONLY way to use pagination with eager fetching, is by setting @Fetch(value = FetchMode.SELECT) ?


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