HHH-13154 Backport BasicCriteriaExecutionTests from ORM 6
This commit is contained in:
parent
ae1016a1c4
commit
fd1963a6a2
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Hibernate, Relational Persistence for Idiomatic Java
|
||||
*
|
||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
|
||||
* See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
|
||||
*/
|
||||
package org.hibernate.orm.test.query.criteria;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.ParameterExpression;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class BasicCriteriaExecutionTests extends BaseNonConfigCoreFunctionalTestCase {
|
||||
|
||||
@Override
|
||||
protected Class[] getAnnotatedClasses() {
|
||||
return new Class[] { BasicEntity.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutingBasicCriteriaQuery() {
|
||||
final CriteriaBuilder criteriaBuilder = sessionFactory().getCriteriaBuilder();
|
||||
|
||||
final CriteriaQuery<Object> criteria = criteriaBuilder.createQuery();
|
||||
|
||||
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
|
||||
|
||||
criteria.select( root );
|
||||
|
||||
inSession(
|
||||
session -> session.createQuery( criteria ).list()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutingBasicCriteriaQueryLiteralPredicate() {
|
||||
final CriteriaBuilder criteriaBuilder = sessionFactory().getCriteriaBuilder();
|
||||
|
||||
final CriteriaQuery<Object> criteria = criteriaBuilder.createQuery();
|
||||
|
||||
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
|
||||
|
||||
criteria.select( root );
|
||||
|
||||
criteria.where( criteriaBuilder.equal( criteriaBuilder.literal( 1 ), criteriaBuilder.literal( 1 ) ) );
|
||||
|
||||
inSession(
|
||||
session -> session.createQuery( criteria ).list()
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExecutingBasicCriteriaQueryParameterPredicate() {
|
||||
final CriteriaBuilder criteriaBuilder = sessionFactory().getCriteriaBuilder();
|
||||
|
||||
final CriteriaQuery<Object> criteria = criteriaBuilder.createQuery();
|
||||
|
||||
final Root<BasicEntity> root = criteria.from( BasicEntity.class );
|
||||
|
||||
criteria.select( root );
|
||||
|
||||
final ParameterExpression<Integer> param = criteriaBuilder.parameter( Integer.class );
|
||||
|
||||
criteria.where( criteriaBuilder.equal( param, param ) );
|
||||
|
||||
inSession(
|
||||
session -> session.createQuery( criteria ).setParameter( param, 1 ).list()
|
||||
);
|
||||
}
|
||||
|
||||
@Entity
|
||||
public static class BasicEntity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Integer id;
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue