HHH-7209 - Add varargs versions of Restrictions.and/or

This commit is contained in:
Steve Ebersole 2012-03-29 14:46:02 -05:00
parent fd9d168d19
commit ce4e2eda87
2 changed files with 63 additions and 0 deletions

View File

@ -242,6 +242,22 @@ public class Restrictions {
public static LogicalExpression and(Criterion lhs, Criterion rhs) {
return new LogicalExpression(lhs, rhs, "and");
}
/**
* Return the conjuction of multiple expressions
*
* @param predicates The predicates making up the initial junction
*
* @return The conjunction
*/
public static Conjunction and(Criterion... predicates) {
Conjunction conjunction = conjunction();
if ( predicates != null ) {
for ( Criterion predicate : predicates ) {
conjunction.add( predicate );
}
}
return conjunction;
}
/**
* Return the disjuction of two expressions
*
@ -252,6 +268,22 @@ public class Restrictions {
public static LogicalExpression or(Criterion lhs, Criterion rhs) {
return new LogicalExpression(lhs, rhs, "or");
}
/**
* Return the disjuction of multiple expressions
*
* @param predicates The predicates making up the initial junction
*
* @return The conjunction
*/
public static Disjunction or(Criterion... predicates) {
Disjunction disjunction = disjunction();
if ( predicates != null ) {
for ( Criterion predicate : predicates ) {
disjunction.add( predicate );
}
}
return disjunction;
}
/**
* Return the negation of an expression
*

View File

@ -95,6 +95,37 @@ public class CriteriaQueryTest extends BaseCoreFunctionalTestCase {
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
}
@Test
@TestForIssue( jiraKey = "HHH-7209" )
public void testVarargJunctionSyntax() {
Session session = openSession();
session.beginTransaction();
session.createCriteria( Course.class )
.add(
Restrictions.and(
Property.forName( "description" ).like( "Hibernate%" ),
Property.forName( "description" ).like( "%ORM%" )
)
)
.list();
session.createCriteria( Course.class )
.add(
Restrictions.and(
Property.forName( "description" ).like( "Hibernate%" ),
Restrictions.or(
Property.forName( "description" ).like( "%ORM%" ),
Property.forName( "description" ).like( "%Search%" )
)
)
)
.list();
session.getTransaction().commit();
session.close();
}
@Test
public void testEscapeCharacter() {
Session session = openSession();