-->
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.  [ 7 posts ] 
Author Message
 Post subject: Criteria Query Example.
PostPosted: Tue Aug 16, 2005 10:55 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hi,


Hibernate version:3.0.5



[b]Name and version of the database you are using:

Oracle 10g

Can anyone give me an example of a Criteria Query with an Integer value set on an instance of the Object we are selecting?

To make it more clear,

If i have a Class Example.

// Create an instance
Example testExample = new Example();

// suppose Example has an attribute Id and i set the value
testExample.setID(1);

// Now i write the Session Criteria.
List lstsrchExampls = searchSession.createCriteria(Example).add(testExample).list();


Will This work? I tried setting String values instead of numerical values and that worked and gave me a result set based on that string value. I want to know if the above works. As well, will setting values to more than one attribute also generate the result set. Meaning to the above code, i add
testExample.setName("Example");

Will i get a result set satisfying both the coditions or any one of the conditions? Wat will the result set contain?

Thanks,
Surya


Top
 Profile  
 
 Post subject: Re: Criteria Query Example.
PostPosted: Tue Aug 16, 2005 11:07 am 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
See reference 16.6. Example queries:

Quote:
16.6. Example queries
The class org.hibernate.criterion.Example allows you to construct a query criterion from a given instance.

Code:
Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .list();


Version properties, identifiers and associations are ignored.


Top
 Profile  
 
 Post subject: Re: Criteria Query Example.
PostPosted: Tue Aug 16, 2005 11:27 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
Hi ,

I have seen this references and read through it as well. Even in the example, they show setting class type Color instance and a Single Character value on the Cat Object. They don't talk about an integer int. I don't want to assume it works with integers because i tried setting integer values and the record wasn't picked up.
The other thing i was looking to know is if this Querying technique will work on searching with primary key field. Because, that if it doesn't then that might be my problem. My integer column was a primary key field.

Thanks,
Surya

sergeya wrote:
See reference 16.6. Example queries:

Quote:
16.6. Example queries
The class org.hibernate.criterion.Example allows you to construct a query criterion from a given instance.

Code:
Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
    .add( Example.create(cat) )
    .list();


Version properties, identifiers and associations are ignored.


Top
 Profile  
 
 Post subject: Re: Criteria Query Example.
PostPosted: Tue Aug 16, 2005 11:40 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
suryavijayk wrote:
Hi ,

I have seen this references and read through it as well. Even in the example, they show setting class type Color instance and a Single Character value on the Cat Object. They don't talk about an integer int. I don't want to assume it works with integers because i tried setting integer values and the record wasn't picked up.
The other thing i was looking to know is if this Querying technique will work on searching with primary key field. Because, that if it doesn't then that might be my problem. My integer column was a primary key field.

Thanks,
Surya


Read it again.

Quote:
Version properties, identifiers and associations are ignored.


If you have the ID, you don't need any other information. That's the definition of an ID.

Code:
SomeEntity someEntity = (SomeEntity) session.get(SomeEntity.class, new Integer(id));


Top
 Profile  
 
 Post subject: Re: Criteria Query Example.
PostPosted: Tue Aug 16, 2005 12:25 pm 
Regular
Regular

Joined: Thu Dec 02, 2004 7:11 am
Posts: 85
suryavijayk wrote:
The other thing i was looking to know is if this Querying technique will work on searching with primary key field. Because, that if it doesn't then that might be my problem. My integer column was a primary key field.


Yes, that is the reason. Integer properties should work if they are not version or identifier field.


Top
 Profile  
 
 Post subject: Re: Criteria Query Example.
PostPosted: Wed Aug 17, 2005 11:03 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
sergeya wrote:
suryavijayk wrote:
The other thing i was looking to know is if this Querying technique will work on searching with primary key field. Because, that if it doesn't then that might be my problem. My integer column was a primary key field.


Yes, that is the reason. Integer properties should work if they are not version or identifier field.


What about if the identifier is a Foreign key? Is it still ignored?
I am thinking it probably will be ignored as part of the Associations that was mentioned rather than part of the identifiers? Am i Right on this?

wat other way can i use to retrieve objects linked in an unidirectional many-to-one associations ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 11:24 am 
Regular
Regular

Joined: Thu May 26, 2005 2:08 pm
Posts: 99
Are you sure you really want to do Query By Example? Query By Criteria is probably what you're really after.

The documentation has examples of this:
http://www.hibernate.org/hib_docs/v3/re ... sociations

If you wanted to change up their example and use a foreign key value, it would look like this instead.

Code:
List cats = sess.createCriteria(Cat.class)
    .add( Restrictions.like("name", "F%")
    .createCriteria("kittens")
        .add( Restrictions.like("kitten_id_property", someIdValue)
    .list();


The thing to notice here is that you add the association alias or sub-criteria to your main criteria, and then try to match the associated classes primary key property (or whatever your foreign key is linked to). You don't try to match the foreign key value directly on your main class.


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