-->
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.  [ 2 posts ] 
Author Message
 Post subject: DetachedCriteria Help
PostPosted: Thu Jun 29, 2006 2:43 pm 
Beginner
Beginner

Joined: Sat Mar 13, 2004 4:00 pm
Posts: 32
I have two objects A and B. A looks like

@Entity
@Table(name="TABLE_A")
public class A {
@Id
@Column(name="ID")
private Long id;

@OneToMany(mappedBy="owner", cascade=CascadeType.ALL,
fetch=FetchType.EAGER)
private List<B> listOfBs;
}
B looks like

@Entity
@Table(name="TABLE_B")
public class B{
@Id
@Column(name="ID")
private Long id;

@Column
private double value;

@Column
private Date theDate;
}

Basically I'm trying to compare the B.value of the most recent entry of B (i.e. max(B.theDate)) using a DetachedCriteria object. My attempt is as follows, but its not working. Any help would be appreciated.

DetachedCriteria dc = DetachedCriteria.forClass(A.class);
DetachedCriteria cdc = dc.createCriteria(listOfBs);
cdc.add(Restrictions.eq("value", 5.0));
DetachedCriteria maxDate = DetachedCriteria.forClass(B.class);
cdc.add(Property.forName("theDate").eq(maxDate.setProjection(Property.forName("theDate").max()));


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 29, 2006 4:41 pm 
Expert
Expert

Joined: Tue Apr 25, 2006 12:04 pm
Posts: 260
Try this code

Code:
DetachedCriteria bDC = DetachedCriteria.forClass( B.class );
bDC.setProjection( Projections.max( "theDate" ) );

Criteria aCriteria = session.createCriteria( A.class );
aCriteria.add( Restrictions.eq( "id", new Long( 5 ) ) );
aCriteria.createAlias( "listOfBs", "bObjects" );
aCriteria.add( Property.forName( "bObjects.theDate" ).eq( bDC ) );


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