-->
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: QBC Problem
PostPosted: Wed Feb 16, 2005 8:41 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
Hi
Hi

I am writing a QBC query with a couple of joins in it and have a question...

I have a FleetDefect object, which contains a FleetVehicle, which contains a FleetVehicleSite...

So I start off with this criteria...

Criteria criteria = session.createCriteria(FleetDefect.class);

The FleetVehicle entity has a creationDate and I want to compare that
with the startDate of the fleetVehicleSite.

I can create my sub criteria for the joining tables, but how do I refer to the parent object FleetVehicle? from down in the subCriteria?

I need to access fleetVehicle.creationDate field...

So to clarify: FleetVehicleDefect -> FleetVehicle -> FleetVehiclesSite

startDate and endDate are in the FleetVehiclesSite entity and
creationDate is in FleetDefect:

Criteria criteria = session.createCriteria(FleetDefect.class);

criteria.createCriteria("fleetVehicle")
.createCriteria("fleetVehiclesSite")
.add(Expression.and(
Expression.le("startDate",
"this.creationDate"),
Expression.ge("endDate",
"this.creationDate")
));

I know I can this working using QBE, but like the riteria stuff so would like to use that if possible...

Any help would be appreciated on this (and I have read my Hibernate in Action chapter on QBC/E! :))

Hibernate version:2.1.7

Mapping documents:

Name and version of the database you are using:Oracle 9i

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 16, 2005 8:45 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
use createAlias()


Top
 Profile  
 
 Post subject: QBC Problem
PostPosted: Wed Feb 16, 2005 9:00 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
I tried createAlias, but could not work out what I am supposed to put in there...

I cannot find an example (in the book) where there is an alias being created to reference the parent entity... The only examples I see are where the alias is for the associated entity that is being joined...

I tried: createAlias("this", "fleetDefect") and it did not work...

What should the syntax be?

As an aside note... the createAlias stuff I thought might tidy up my QBC more... so I do not need to do the createCriteria for all the joins... and instead just create aliases for the joined tables/entites..

but I tried:

criteria.createAlias("fleetVehicle", "fv");

and I got an error saying association had already been made...

and so I took it out again and tried referencing fleetVehicle.aproperty instead and got:

could not resolve property: fleetVehicle.registrationNumber of: com.ocado.tracker.defects.pojo.FleetDefect; nested exception is net.sf.hibernate.QueryException: could not resolve property: fleetVehicle.registrationNumber of: com.ocado.tracker.defects.pojo.FleetDefect
net.sf.hibernate.QueryException: could not resolve property: fleetVehicle.registrationNumber


Top
 Profile  
 
 Post subject: QBC Problem
PostPosted: Wed Feb 16, 2005 10:10 am 
Beginner
Beginner

Joined: Thu Oct 14, 2004 5:07 am
Posts: 37
Location: Cambridge, UK
FleetDefect -> FleetVehicle -> FleetVehiclesSite

I tried the aliases - they seemed to work fine for fleetVehicle, but each FleetVehicle has a many to one association with FleetVehiclesSite and that did not work (it could not resolve the propery whichever way I tried to set up the alias..)

Criteria criteria = session.createCriteria(FleetDefect.class);
criteria.createAlias("fleetVehicle", "fv");

So I resort back to creating (sub) criteria's:

Criteria criteria = session.createCriteria(FleetDefect.class);
Criteria fleetVehicleCriteria = criteria.createCriteria("fleetVehicle");
Criteria fleetVehiclesSiteCriteria = fleetVehicleCriteria.createCriteria("fleetVehiclesSite");
Criteria fleetVehiclesVorCriteria = fleetVehicleCriteria.createCriteria("fleetVehiclesVor");

But it is still not resolving *this*... when you said use createAlias, are you suggesting I create an alias for the parent entity FleetDefect?

fleetVehiclesSiteCriteria.add(Expression.eq("site",
fleetDefect.getFilterSite()))
.add(Expression.and(
Expression.le("startDate", "this.creationDate"),
Expression.ge("endDate", "this.creationDate")
));

?


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.