Hibernate Criteria API results not matching HQL results for left join fetch

I have 2 simple models:

class Parent { Long id; //auto generated sequence and primary key String name; Set<Child> children; } class Child { String name; Long parent_id; //foreign key }

I have a hql query like this:

FROM Parent p left join fetch p.children as children WHERE p.name = 'John'

'children' is a collection (set) of 'Child' model in Parent model.

If 'John' has 2 children, result of above query gives me list of 2 Parents (same reference) each with 2 children by executing a single query.

I am trying to achieve the same via Criteria API like below:

Criteria c = session.createCriteria(Parent.class); c.setFetchMode("children", FetchMode.JOIN); c.createCriteria("children", Criteria.LEFT_JOIN); c.add(Restrictions.eq("name", "John")); c.scroll();

With above code, I get the list of 2 parent instances (same reference) with only 1 child element (instead of expected 2) bu executing a single sql query.

What am I doing wrong in api? When I see the generated sql, it is same.

--------------Solutions-------------

I have a similar situation, and here is the code that is equivalent (at least in my situation):

Criteria c = session.createCriteria(Parent.class);
c.setFetchMode("children", FetchMode.JOIN);
c.add(Restrictions.eq("name", "John"));

List<Parent> list = c.list();
Iterator<Parent> iter = list.iterator();

I have the impression that the line c.scroll(); forces a similar behavior to c.setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY );

I had the same issue, and after debugging through the Hibernate code I think it's a Hibernate bug rather than anything you're doing wrong.

It was fixed in this Hibernate issue, but our Criteria was still returning incomplete child collections in scrollable results. I found that in the Loader class there's some logic that decides whether to use the FetchingScrollableResultsImpl to handle the rows properly, and on CriteriaLoader it never does because needsFetchingScroll() always returns false. However QueryLoader does use it when a join fetch is used, which is why converting our Criteria into HQL fixed the issue for us.

I intend to file a bug with Hibernate to implement CriteriaLoader.needsFetchingScroll().

Category:hibernate Time:2011-06-23 Views:0

Related post

  • Hibernate Criteria API - HAVING clause work arounds 2009-01-21

    I've written a query using Hibernate Criteria API to grab a summation of a particular value, now I need to be able to restrict the result to rows where that sum is greater or equal to a particular value. Normally I would use a HAVING clause in my SQL

  • Hibernate Criteria API 2010-12-17

    I was asked in the interview today about Hibernate eager loading. The question was: If there is an Employee class and an Address Class and relationship:1 employee --> many addresses. How with a single query one can query Employee class and get all

  • Reading with Hibernate Criteria API 2012-04-20

    I'm using Hibernate Criteria API in my project. In this API, list() method is used to get a number of records from db. My question is : How to get only one record from db (without using list() method as there is no need)? --------------Solutions-----

  • Hibernate Criteria API Equivalent to Oracle's Decode 2011-05-02

    What would the equivalent of Oracle's DECODE() function be in the Hibernate Criteria API? An SQL example of what I need to do: SELECT DECODE(FIRST_NAME, NULL, LAST_NAME, FIRST_NAME) as NAME ORDER BY NAME; Which returns LAST_NAME to NAME in the event

  • Querying many to many association where the entity doesn't have a certain value using Hibernate Criteria API 2011-08-29

    I got two classes User and Role which are mapped to each other using a many to many associations. Now I'm trying to query all users which doesn't have a certain role using the Hibernate Criteria API. But I'm a little bit stuck. To query users with a

  • Is Hibernate Criteria API stable for future usage? 2011-10-22

    I mostly use Hibernate criteria API in my previous project. I found Hibernate criteria Expression is already deprecated! Is Hibernate Criteria API stable for future usage? http://docs.jboss.org/hibernate/core/3.3/api/org/hibernate/criterion/Expressio

  • Hibernate Criteria API - how to order by collection size? 2009-08-10

    Assuming I have classes User and UserGroup. There is an optional 1-many group to user association and the association is mapped from both sides (the UserGroup side via a property called "members", which is a HashSet, the User side via property "group

  • How can we order a column as int using hibernate criteria API? 2010-05-20

    Hi I want to fetch the data form data base using hibernate Criteria API. That data should be ordered by some column as number. This column is defined as varchar in DB. But I have to fetch as numberic. I am facing problem using criteria API as it is o

  • Hibernate Criteria API multiple joins 2010-08-02

    My hibernate entities are as follows: @Entity @Table(name = "EditLocks") public class EditLock extends AuditableEntity { /** The document that is checked out. */ @OneToOne @JoinColumn(name = "documentId", nullable = false) private Document document;

  • hibernate criteria api join table problem 2010-09-02

    i cannot use sorting on join tables. Let me explain; i have three tables. users, roles and user_roles. my JPA entities are User, UserRole, UserRolePK, Role. |User | | UserRole | | UserRolePK | | Role | |--------| |----------| -------------- --------

  • question on Hibernate Criteria API 2011-08-24

    I have a two table A and B: A columns (ID,NameA,BiD) B columns (ID,NameB) ID is referenced to Bid as foreign RelationMapping. Here is the problem .My Hibenate B Entity has not got List but My hibernate A entity has got B entity instance. select a.* f

  • Hibernate Criteria API for filtering a table rows by min value of its column 2011-12-09

    How can we implement the following query using Hibernate criteria api select * from User where date = select min(date) from User where User have a column named date --------------Solutions------------- You have the Projections class for that which he

  • Hibernate criteria API equals method 2012-02-22

    I have used hibernate reverse engineering to generate my model and Dao classes i am using hibernate criteria API to retrive values from database criteria.add(Restrictions.eq( "propert1.propert2.StateId", 1)); i am getting the following exception java

  • Hibernate Criteria API: count and group by with result in Map 2010-06-03

    The Car entity is mapped to a database table with 2 columns: ID and Color. CarDao has the following method: Map<Color, Integer> countByColor(); If we have 3 red cars and 2 blue cars in the database table, the method returns a map with 2 keys (r

  • Write HQL clause using Hibernate Criteria API 2012-03-06

    I want to write a method that returns a list of last added objects grouped by field 'serviceId'. The following HQL works, but I want to write this using Criteria API: FROM Notification WHERE date IN (SELECT MAX(date) FROM Notification GROUP BY servic

  • using hibernate Criteria.uniqueResult() when there are several results 2011-10-26

    I have a table USERS with names and creation dates, and an api function T read(Criterion... criteria) that searches by criterions that i cant change. My problem is that the function returns crit.uniqueResult() but sometimes the criteria gives many na

  • Hibernate Criteria API equivalent to HQL select clause? 2010-05-10

    I'd like to have a combined query for two persistent classes. In HQL this could be achieved by the select clause, select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr In the ab

  • Querying Many-To-Many relationship with Hibernate Criteria API 2009-04-06

    I've the following many to many relation: File 1 --- * File_Insurer * --- 1 Insurer. I'm trying to query this relation using the Criteria API (Active Record) to get Files that meet ALL specified Insurers (Get all Files where Insurer.Id == 2 AND Insur

  • Hibernate Criteria API - Filtering collection property 2011-05-23

    I have such entity: @Entity public class Album { private Integer id; private Integer ownerId; private String name; private String description; private Date created; @OneToMany @JoinColumn(name = "albumId") private Set<AlbumUser> users = new Has

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.580 (s). 13 q(s)