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.  [ 3 posts ] 
Author Message
 Post subject: How to make this query ?
PostPosted: Thu Feb 21, 2008 8:17 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 4:29 am
Posts: 33
hello,

i'm wondering how i could make this query with createcriteria ?

I have this tables:
- Work
- Coeff (id, idwork, idmonth, idyear, Version, coeff)
- Year
- Month

The query:

Code:
SELECT * FROM Coeff cc WHERE cc.IdWork = " + id + " and cc.Version = (
SELECT Min(c.Version) FROM Coeff c WHERE c.IdMonth = cc.IdMonth and c.IdWork = cc.IdWork and c.IdYear = cc.IdYear and c.Version >= " + Version);


I don't know if the part " c.IdMonth = cc.IdMonth and c.IdWork = cc.IdWork and c.IdYear = cc.IdYear" is possible...

Thanks


Top
 Profile  
 
 Post subject: How to make this query ?
PostPosted: Thu Feb 21, 2008 8:51 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
Should be something like this:

Code:
DetachedCriteria subquery = DetachedCriteria.For(typeof(Coeff), "c");

subquery.Add( Expression.EqProperty( "c.IdMonth", "cc.IdMonth" ) )
             .Add( Expression.EqProperty( "c.IdWork", "cc.IdWork" ) )
             .Add( Expression.EqProperty( "c.IdYear", "cc.IdYear" ) )
             .Add( Expression.Gt( "Version", Version)
             .SetProjection( Projections.Min( "Version" ) );

ICriteria crit = session.CreateCriteria(typeof(Coeff), "cc");

crit.Add( Expression.Eq( "IdWork", id ))
    .Add( Subqueries.PropertyGe( "Version", subquery )


I haven't tried it, but I use a similar query in our app.

Regards,
Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 21, 2008 9:14 am 
Beginner
Beginner

Joined: Wed Mar 14, 2007 4:29 am
Posts: 33
Expression.EqProperty, i see...

Thanks a lot !


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