-->
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.  [ 5 posts ] 
Author Message
 Post subject: Criteria search needs help
PostPosted: Thu Nov 30, 2006 5:35 am 
Newbie

Joined: Thu Nov 16, 2006 12:42 pm
Posts: 7
Hi all,

I need help on a criteria i am trying to add to make a query.

I have these two objects, Policy and Product:

Code:
public class Policy {

   private int id;

   private Product product;

   private int conversionAttempts;

       ...
}

public class Product{

   private int id;

   private Date creationDate;

       ...
}


This is my mapping for the relation between the two objects:

Code:
  <class name="Policy" table="policy_status">
     <id name="id" type="int" column="policy_status_id">
    <property name="conversionAttempts" column="conversion_attempts" />
    <many-to-one name="product" column="product_scheme_id" class="Product" lazy="false" cascade="none" not-null="true" />


And i also have the mapping of the Product which contains a creation date.

I am then trying to add the following expression:
criteria.add(Expression.eq("product.creationDate", date));

But it fails complaining that it does not know the property product.creationDate.

And i am 100% sure that it is not a spelling error.

Apparently the creation date is not known for the Policy, only the whole product object is.

How can i add a criteria where i want all the Policies, that have a certain product creation date ?

Hope someone has the answer.

Best Regards
Cemil[/b]


Last edited by Cemil on Thu Nov 30, 2006 5:41 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 5:40 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
Here you can find an example of what you want:

http://www.hibernate.org/hib_docs/v3/re ... sociations

" You may easily specify constraints upon related entities by navigating associations using createCriteria()."

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 5:54 am 
Newbie

Joined: Thu Nov 16, 2006 12:42 pm
Posts: 7
Hi andresgr

That was brilliant.

Thanks for the swift answer, i made it work now.

The solution was:
criteria.createCriteria("product").add(Restrictions.eq("creationDate", date));

I have only small question left.

When should i use Expression and when should i use Restrictions ?

What is the difference

Best Regards
Cemil


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 6:03 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
From the javadoc:

http://www.hibernate.org/hib_docs/v3/ap ... ssion.html

This class is semi-deprecated. Use Restrictions.

Please, if my answers did help, rate them :)

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 30, 2006 6:20 am 
Newbie

Joined: Thu Nov 16, 2006 12:42 pm
Posts: 7
Hi andresgr,

Sorry about the rating.

I rated it just now.

And thanks for the answer about the deprecation as well...

Have nice day.

Best Regards Cemil


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