-->
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: Example criteria on alias
PostPosted: Fri Feb 22, 2008 12:59 pm 
Newbie

Joined: Fri Feb 22, 2008 12:45 pm
Posts: 5
Hi there,

According to the Hibernate documentation, Example queries ignore associations, so if I want to apply an example criteria on an association I should do something like this:

Code:
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .createCriteria("mate")
        .add( Example.create( cat.getMate() ) )
    .list();


The problem is that the associated entity ("mate") is retrieved from the database. How can this be avoided?
I just want to filter the root entities with restrictions on their "mate"s but I don't want to retrieve the "mate"s.

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 22, 2008 7:48 pm 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
MDiaz,

You are mixing a couple of issues in your question. To clarify:
-the example query's purpose is to contain parameters already set, a function that, in your case, we assume the "cat" object has already been created for elsewhere.
-you cannot, on your cat object, place restrictions on the "mate" property simply using cat.mate.propertyA=xxx, assuming mate is a property of cat, of course. You can use this "dot path" notation without need of subcriterias.
-you can ask for specific properties (fields) of Cat instead of the whole object, but if you select the whole object, asking if there is a way to "avoid" the mate to be retrieved is the wrong kind of question.
Hibernate by default connects objects loosely, i.e., using "lazy" associations by default. This means that the mate will not be retrieved by your selection of cats unless you specifically order Hibernate to do so using the "fetch" keyword on your HQL or specify the property retrieval as not lazy.
In other words, "mate" won't be retrieved from your database unless you call "getMate()" case by case, even if "mate" forms part of the "where" clause.

Hablo castellano si te sirve mejor.

_________________
Gonzalo Díaz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 27, 2008 4:43 am 
Newbie

Joined: Fri Feb 22, 2008 12:45 pm
Posts: 5
It looks like the Criteria API ignores the lazyness of the associations when restrictions are set for associated entities (as discussed here and here).

The scenario is a Parent entity with a many-to-one (or one-to-one) relationship with a Child entity:

Code:
class Parent {
  private String parentName;
  private Child child;
  // ... getters and setters ...
}

class Child {
  private String childName;
  // ... getters and setters ...
}


If you want to make a Criteria query to retrieve some Parent objects placing restrictions on the Child object(s) you would do it this way:

Code:
List results = session.createCriteria(Parent.class)
    .add( Restrictions.eq("parentName", "John") )
    .createCriteria("child")
        .add( Restrictions.eq("childName", "Paul") )
    .list();


Hibernate eagerly retrieves the Child entities regardless of the laziness of the association in the mapping document.

The workarounds proposed in the threads above have some issues and they don't allow (I think) for Example based queries.

I haven't found any comment about this behavior in the Hibernate docs nor any opened issue regarding this in Hibernate Jira. I think an Issue should be opened for this since the docs are misleading and the behavior seems wrong.

@gonzao_diaz: gracias por el ofrecimiento, pero creo que me apaño con el inglés


Top
 Profile  
 
 Post subject: Criteria - Parent/Child - Result is not correct
PostPosted: Thu Feb 28, 2008 2:12 am 
Newbie

Joined: Thu Feb 28, 2008 2:06 am
Posts: 1
I execute the following code

[/img]
Code:
session.createCriteria(Parent.class)
    .add( Restrictions.eq("parentName", "John") )
    .createCriteria("child")
        .add( Restrictions.eq("childName", "Paul") )
    .list();


I get the parent with child as its set in my result.
But the problem is that the childset returned has first element has null and only the second has the details of "Paul".

Any help?


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.