-->
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: Navigating within an object in a Restriction
PostPosted: Wed Jan 21, 2009 12:27 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2008 4:24 pm
Posts: 29
Hy, If I am using the next code:
Suppose I have two tables:
person id(PK), name, cocheId(FK to cars table)
car: id(PK), name, colour, model(FK to carModel table)
carModel: id(PK), fabricationYear, etc...


Suppose the names of the columns of a database and the names of the properties in the three classes of hibernate are the same so:

If I want to know a person who has a car which fabricationYear is 1975 , I would to apply the next restriction to an object Person

-
- Restrictions.eq("cocheId.model.fabricationYear",1975 ));

Right?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2009 2:16 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
No, that will not work. Criteria queries has no "implicit" joins as in HQL. You'll need to use either Criteria.createAlias() or Criteria.createCritera() to navigate associations. There are some example in the API doc (http://www.hibernate.org/hib_docs/v3/ap ... teria.html) and the reference documentation (http://www.hibernate.org/hib_docs/v3/re ... tions.html)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 21, 2009 4:55 pm 
Beginner
Beginner

Joined: Tue Aug 26, 2008 4:24 pm
Posts: 29
Ok so just giving an alias to the object , I can navigate inside it but I dont understand what makes to give an alias to an object to be able to navigate inside it.

In my example should I do something like:

createAlias("cocheId.model", "model");
Restrictions.eq("model.fabricationYear",1975 ));

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 22, 2009 4:56 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Is your association really called "cocheId" and not "coche"? Remember that in order to use criteria you must have associations at class level, like:
Code:
public class Person {
  private Car coche;
  ...
}

your criteria could then look like:
Code:
session.createCriteria(Person.class).createCriteria("coche", "c").createAlias("c.model", "model").add(Restrictions.eq("model.fabricationYear",1975 ))

_________________
-----------------
Need advanced help? http://www.viada.eu


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.