-->
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.  [ 10 posts ] 
Author Message
 Post subject: Is it possible to query by Object?
PostPosted: Mon Oct 27, 2003 6:45 pm 
Newbie

Joined: Mon Oct 27, 2003 6:41 pm
Posts: 7
Instead of building a query, can I just query with an object that has the properties set that I'm looking for? Similiar to how OJB does it?

Thanks for any ideas you have!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 6:50 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Hibernate 2.1 has Example queries.

And done incredibly cool, if I don't say so myself ;-)


Top
 Profile  
 
 Post subject: Thanks
PostPosted: Mon Oct 27, 2003 7:03 pm 
Newbie

Joined: Mon Oct 27, 2003 6:41 pm
Posts: 7
Could you point me to a url with that documentation/examples? I seem to be running into older documentation.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 8:42 pm 
Newbie

Joined: Mon Oct 27, 2003 6:41 pm
Posts: 7
I'm not sure if I found the documentation on what you mentioned, what I did find was Criteria queries. I still did not find any examples on the following:

I want to pass just the object(who's properties are set to match my criteria) and have the criteria built for me. This would be better than having to build the criteria by calling add(Expression.like()), when all I want is to fetch the object that has matching properties.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 27, 2003 8:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The hibernate 2.1 documentation is in the hibernate 2.1 download, of course.


Code:
session.createCriteria(Foo.class)
    .add( Example.create(foo) )
    .list();


It is much more powerful than that, however:


Code:
session.createCriteria(Foo.class)
    .add( Example.create(foo).enableLike(MatchMode.ANYWHERE) )
    .createCriteria("friends")
        .add( Example.create(bar).enableLike(MatchMode.ANYWHERE) )
    .list();



etc...


Thus, the Hibernate example query API is unified with the criteria query API, making it MUCH more powerful than other implementations I've seen. I am very proud of this. :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:38 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
Is there any reason why properties in a class that are mapped as database IDs are not included when you use Criteria with Examples?

For example, I have a simple bean with an "id" property (mapped as a db ID) and a "name" property - if I set both of the properties and then create an Example object from this bean, when the select occurs it quietly ignores the id property.

If you then continue this, and then map the "name" property as a db ID as well, then the select reverts to a "where 1=1".


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
sure. it makes absolutely no sense to include the PK in an example query. If the PK is included, then the Example identifies exactly one row by definition. So you don't need an Example at all.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Could be different however if you use like ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:42 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
uuuhh I *suppose*...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:48 am 
Regular
Regular

Joined: Tue Oct 07, 2003 10:20 am
Posts: 77
I can understand that point :)

I'm trying to refactor some code which used to build up the SQL manually depending upon the properties set (it doesn't necessarily have the PK set in the bean) - using an Example seemed like the perfect replacement.

However, if the PK is always left out, then it's not exactly hard to code around that!

Cheers again for the fast response.


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