-->
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.  [ 4 posts ] 
Author Message
 Post subject: Example queries respecting (selected) null values?
PostPosted: Fri Jun 24, 2005 12:18 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
Hibernate version:
3.0.3
Mapping documents:
n/a
Code between sessionFactory.openSession() and session.close():
n/a
Full stack trace of any exception that occurs:
n/a
Name and version of the database you are using:
MySql
The generated SQL (show_sql=true):
n/a
Debug level Hibernate log excerpt:
n/a

Question:
Ciao,

I have a persistent class Group that has lots of properties.
I would like to quiery my db for all Groups that match a certain Example instance of Group:

Group example = new Group();
example.setA("red");
example.setB("large");
example.setC(null);

List result = session.createCriteria(Group.class).add(Example.create(example).list();

As Null Values are excluded by default from the Criterias my Group.C Criteria is not regarded. In fact I wanted to explicitely search for Null Values in Property C.
The excludeNone() function I found not helpful here because that would mean that ALL null-valued properties of Group were regarded, not only C.

Is there any solution for respecting selected null values explicitely in an Example Query?


PS:
I have found a solution by using a CriteriaQuery:
if (null == group.getC()) {
crit.add(Expression.isNull("c"));
} else {
crit.add(Expression.eq("c", group.getC()));
}
..but I would prefer Example Queries..


Thanks
Kai


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 10:58 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
So, I guess one cannot use an Example Query here?!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 28, 2005 11:25 am 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Kuwe wrote:
So, I guess one cannot use an Example Query here?!


Code:
Group example = new Group();
example.setA("red");
example.setB("large");
example.setC(null);

List result = session.createCriteria(Group.class)
                             .add(Example.create(example)
                             .excludeNone() // Don't exclude zero or null properties
                             .exclude("d")   // Exclude the property named d
                             .list();


It would be nice if there were a way to include() a single null property but it doesn't appear that there is. So you need to .excludeNone() and to turn off the null-exclusion and then exclude any additional properties you want to exclude from the example.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 29, 2005 12:04 pm 
Beginner
Beginner

Joined: Wed Feb 23, 2005 2:23 pm
Posts: 21
Location: Pescara, italy
pksiv wrote:
It would be nice if there were a way to include() a single null property but it doesn't appear that there is. So you need to .excludeNone() and to turn off the null-exclusion and then exclude any additional properties you want to exclude from the example.


Thanks. I agree that it would be nice to include a single null value because in my case there are quite some properties to exclude....


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