[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5795) Allow Criteria#add(null)

Lorber Sebastien (JIRA) noreply at atlassian.com
Mon Aug 8 09:52:02 EDT 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5795?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43187#comment-43187 ] 

Lorber Sebastien commented on HHH-5795:
---------------------------------------

Actually i would have liked a kind of similar function...

The aim is perhaps not to be able to add null as a criterion, but to be able to return an "empty restriction" (i mean, NOT an isEmpty restriction).


It would have been great to have a "Restrictions.noRestriction()" or something like that, that will eventually be translated into something like sqlRestriction("1=1");


Note that you can also return a Restrictions.conjunction() without anything inside and it'll be considered as an empty restriction... 




> Allow Criteria#add(null)
> ------------------------
>
>                 Key: HHH-5795
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5795
>             Project: Hibernate Core
>          Issue Type: Improvement
>          Components: core
>    Affects Versions: 3.6.0
>         Environment: Irrelevant
>            Reporter: Joachim Durchholz
>
> Please allow using null as a Criterion in Criteria#add.
> I'd like to write Criterion createFooBarFilter(x,y). It inspects both x and y, and returns a Criterion that compares property foo with x and property bar with y, provided X resp. Y are non-null (null values mean: do not filter).
> If x is not null, the function returns Restrictions.eq("foo",x).
> Likewise for Y.
> If both are not null, return Restrictions.and(Restrictions.eq("foo",x),Restrictions.eq("bar",y)).
> I'd like to return null if both x and y are null. This would pass a null to Criteria#add(null), but that crashes and burns miserably as soon as Hibernate wants to get the SQL string from such a Criterion.
> So... how about modifying CriteriaImpl#add like this:
> 	public Criteria add(Criterion expression) {
> 		if ( expression != null ) {
> 			add( this, expression );
> 		}
> 		return this;
> 	}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list