-->
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: Need help on Criteria Query Restrictions.
PostPosted: Tue Dec 16, 2008 9:12 am 
Newbie

Joined: Tue Dec 16, 2008 8:34 am
Posts: 1
Hi All,
i am facing a problem while using a Critera query.
iam using Criteria query to build a search screen.
iam able to add restrictions on child name and child description but not able to add the restriction on parent_id



Hibernate version:3.1
Mapping documents:

Parent_Table
--------------
parent_Id -->PK
Parent_Name
Prant_Description

Child_Table
------------
Child_id
parent_Id -->FK to parent_Id of parent table
Child_Name
Child_Description

Parent Entity
-------------

Entity

@Table(name="Parent_Table")

@Versioned

public class Parent {

private long parent_Id;

@Versioned
private String Parent_Name;
private String Prant_Description;


child entity
------------


@Entity

@Table(name="Child_Table")

public class Child {

private long Child_id;
private Parent parent;
private String Child_Name;
private String Child_Description;

Code between sessionFactory.openSession() and session.close():
Java Class
----------
Criteria lcCriteria = session.createCriteria(Child.class);
lcCriteria.add(Restrictions.ilike("Child_Name", "somevalue", MatchMode.START));
lcCriteria.add(Restrictions.ilike("Child_Description", "somevalue", MatchMode.START));




Name and version of the database you are using:Oracle 10 g
Can some one tell me how to add a restriction to the same criteria for parent_Id

Thanks & Regards,
Sukumar


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 16, 2008 12:54 pm 
Newbie

Joined: Mon Dec 15, 2008 2:28 pm
Posts: 8
You have to use subcriteria objects.

So your "root" entity is the Child class and you have created the lcCriteria object for it.

You create a subcriteria on it by doing:

Code:
Criteria parentCriteria = IcCriteria.createCriteria("parent")


Now you have a Criteria object for the Parent class which is a subcriteria object on the Child class.

Add the criteria conditions that you have for the Parent class on this parentCriteria object.

Now when you call IcCriteria.list(), the SQL generated will contain the conditions that you might have added to the parentCriteria object.

_________________
Naresh
Please rate replies if they've helped you out.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 17, 2008 10:20 pm 
Newbie

Joined: Wed Dec 17, 2008 6:35 pm
Posts: 11
I would create an alias to the parent object (which does an inner join) and then add restrictions to aliasName.property.

lcCriteria.createAlias("parent", "PARENT_ALIAS_NAME");
lcCriteria.add(Restrictions.eq("PARENT_ALIAS_NAME.parent_Id", 1L));


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.