6 - SQM based on JPA type system -
This commit is contained in:
parent
ffb704b532
commit
3d59cef873
|
@ -15,6 +15,7 @@ import javax.persistence.EntityManager;
|
|||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Predicate;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.persistence.criteria.SetJoin;
|
||||
import javax.persistence.metamodel.EntityType;
|
||||
|
@ -35,8 +36,7 @@ import org.hibernate.jpa.test.metamodel.Product;
|
|||
import org.hibernate.jpa.test.metamodel.ShelfLife;
|
||||
import org.hibernate.jpa.test.metamodel.Spouse;
|
||||
import org.hibernate.metamodel.model.domain.internal.DomainMetamodelImpl;
|
||||
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
|
||||
import org.hibernate.query.criteria.internal.predicate.ComparisonPredicate;
|
||||
import org.hibernate.query.sqm.tree.predicate.SqmComparisonPredicate;
|
||||
|
||||
import org.hibernate.testing.FailureExpected;
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
|
@ -76,33 +76,33 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
DomainMetamodelImpl mm = (DomainMetamodelImpl) em.getMetamodel();
|
||||
|
||||
CriteriaQuery<Integer> cquery = cb.createQuery( Integer.class );
|
||||
CriteriaQuery<Integer> cquery = cb.createQuery( Integer.class );
|
||||
Root<Product> product = cquery.from( Product.class );
|
||||
EntityType<Product> Product_ = mm.entity( Product.class );
|
||||
|
||||
cquery.select(
|
||||
cb.toInteger(
|
||||
product.get(
|
||||
Product_.getSingularAttribute("quantity", Integer.class))
|
||||
Product_.getSingularAttribute( "quantity", Integer.class ) )
|
||||
)
|
||||
);
|
||||
|
||||
ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(
|
||||
SqmComparisonPredicate predicate = (SqmComparisonPredicate) cb.equal(
|
||||
product.get( Product_.getSingularAttribute( "partNumber", Long.class ) ),
|
||||
373767373
|
||||
);
|
||||
assertEquals( Long.class, predicate.getRightHandOperand().getJavaType() );
|
||||
assertEquals( Long.class, predicate.getLeftHandExpression().getJavaType() );
|
||||
cquery.where( predicate );
|
||||
em.createQuery( cquery ).getResultList();
|
||||
|
||||
predicate = (ComparisonPredicate) cb.ge(
|
||||
predicate = (SqmComparisonPredicate) cb.ge(
|
||||
cb.length( product.get( Product_.getSingularAttribute( "name", String.class ) ) ),
|
||||
4L
|
||||
);
|
||||
assertEquals( Integer.class, predicate.getRightHandOperand().getJavaType() );
|
||||
assertEquals( Integer.class, predicate.getLeftHandExpression().getJavaType() );
|
||||
cquery.where( predicate );
|
||||
em.createQuery( cquery ).getResultList();
|
||||
|
||||
|
@ -134,13 +134,13 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
em.getTransaction().begin();
|
||||
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
DomainMetamodelImpl mm = (DomainMetamodelImpl) em.getMetamodel();
|
||||
EntityType<Phone> Phone_ = mm.entity( Phone.class );
|
||||
|
||||
CriteriaQuery<Phone> cquery = cb.createQuery( Phone.class );
|
||||
Root<Phone> phone = cquery.from( Phone.class );
|
||||
ComparisonPredicate predicate = (ComparisonPredicate) cb.equal(
|
||||
Predicate predicate = cb.equal(
|
||||
phone.get( Phone_.getSingularAttribute( "address", Address.class ) ),
|
||||
address
|
||||
);
|
||||
|
@ -156,26 +156,26 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
public void testTypeConversion() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
DomainMetamodelImpl mm = (DomainMetamodelImpl) em.getMetamodel();
|
||||
EntityType<Product> Product_ = mm.entity( Product.class );
|
||||
|
||||
// toFloat
|
||||
CriteriaQuery<Float> floatQuery = cb.createQuery( Float.class );
|
||||
CriteriaQuery<Float> floatQuery = cb.createQuery( Float.class );
|
||||
Root<Product> product = floatQuery.from( Product.class );
|
||||
floatQuery.select(
|
||||
cb.toFloat(
|
||||
product.get(Product_.getSingularAttribute("quantity", Integer.class))
|
||||
product.get( Product_.getSingularAttribute( "quantity", Integer.class ) )
|
||||
)
|
||||
);
|
||||
em.createQuery( floatQuery ).getResultList();
|
||||
|
||||
// toDouble
|
||||
CriteriaQuery<Double> doubleQuery = cb.createQuery(Double.class);
|
||||
CriteriaQuery<Double> doubleQuery = cb.createQuery( Double.class );
|
||||
product = doubleQuery.from( Product.class );
|
||||
doubleQuery.select(
|
||||
cb.toDouble(
|
||||
product.get(Product_.getSingularAttribute("quantity", Integer.class))
|
||||
product.get( Product_.getSingularAttribute( "quantity", Integer.class ) )
|
||||
)
|
||||
);
|
||||
em.createQuery( doubleQuery ).getResultList();
|
||||
|
@ -188,21 +188,21 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
public void testConstructor() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
DomainMetamodelImpl mm = (DomainMetamodelImpl) em.getMetamodel();
|
||||
|
||||
CriteriaQuery<Customer> cquery = cb.createQuery(Customer.class);
|
||||
Root<Customer> customer = cquery.from(Customer.class);
|
||||
CriteriaQuery<Customer> cquery = cb.createQuery( Customer.class );
|
||||
Root<Customer> customer = cquery.from( Customer.class );
|
||||
EntityType<Customer> Customer_ = customer.getModel();
|
||||
|
||||
cquery.select(
|
||||
cb.construct(
|
||||
Customer.class,
|
||||
customer.get(Customer_.getSingularAttribute("id", String.class)),
|
||||
customer.get(Customer_.getSingularAttribute("name", String.class))
|
||||
customer.get( Customer_.getSingularAttribute( "id", String.class ) ),
|
||||
customer.get( Customer_.getSingularAttribute( "name", String.class ) )
|
||||
)
|
||||
);
|
||||
TypedQuery<Customer> tq = em.createQuery(cquery);
|
||||
TypedQuery<Customer> tq = em.createQuery( cquery );
|
||||
tq.getResultList();
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
@ -217,7 +217,7 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Customer> cq = cb.createQuery( Customer.class );
|
||||
Root<Customer> r = cq.from( Customer.class );
|
||||
cq.multiselect(
|
||||
|
@ -235,20 +235,20 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
public void testDateTimeFunctions() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
DomainMetamodelImpl mm = (DomainMetamodelImpl) em.getMetamodel();
|
||||
|
||||
CriteriaQuery<java.sql.Date> dateQuery = cb.createQuery(java.sql.Date.class);
|
||||
CriteriaQuery<java.sql.Date> dateQuery = cb.createQuery( java.sql.Date.class );
|
||||
dateQuery.from( Customer.class );
|
||||
dateQuery.select( cb.currentDate() );
|
||||
em.createQuery( dateQuery ).getResultList();
|
||||
|
||||
CriteriaQuery<java.sql.Time> timeQuery = cb.createQuery(java.sql.Time.class);
|
||||
CriteriaQuery<java.sql.Time> timeQuery = cb.createQuery( java.sql.Time.class );
|
||||
timeQuery.from( Customer.class );
|
||||
timeQuery.select( cb.currentTime() );
|
||||
em.createQuery( timeQuery ).getResultList();
|
||||
|
||||
CriteriaQuery<java.sql.Timestamp> tsQuery = cb.createQuery(java.sql.Timestamp.class);
|
||||
CriteriaQuery<java.sql.Timestamp> tsQuery = cb.createQuery( java.sql.Timestamp.class );
|
||||
tsQuery.from( Customer.class );
|
||||
tsQuery.select( cb.currentTimestamp() );
|
||||
em.createQuery( tsQuery ).getResultList();
|
||||
|
@ -261,7 +261,7 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
public void testFunctionDialectFunctions() {
|
||||
EntityManager em = getOrCreateEntityManager();
|
||||
em.getTransaction().begin();
|
||||
CriteriaBuilderImpl cb = (CriteriaBuilderImpl) em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<Long> criteria = cb.createQuery( Long.class );
|
||||
criteria.select( cb.count( cb.literal( 1 ) ) );
|
||||
Root<Customer> root = criteria.from( Customer.class );
|
||||
|
@ -281,36 +281,36 @@ public class QueryBuilderTest extends BaseEntityManagerFunctionalTestCase {
|
|||
em.getTransaction().commit();
|
||||
em.close();
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@TestForIssue(jiraKey = "HHH-10737")
|
||||
@FailureExpected( jiraKey = "HHH-10737" )
|
||||
@FailureExpected(jiraKey = "HHH-10737")
|
||||
public void testMissingDialectFunction() {
|
||||
doInJPA( this::entityManagerFactory, em -> {
|
||||
Human human = new Human();
|
||||
human.setId(200L);
|
||||
human.setName("2");
|
||||
human.setBorn(new Date());
|
||||
em.persist(human);
|
||||
human.setId( 200L );
|
||||
human.setName( "2" );
|
||||
human.setBorn( new Date() );
|
||||
em.persist( human );
|
||||
|
||||
em.getTransaction().commit();
|
||||
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||
CriteriaQuery<HumanDTO> criteria = cb.createQuery( HumanDTO.class );
|
||||
Root<Human> root = criteria.from( Human.class );
|
||||
|
||||
criteria.select(
|
||||
cb.construct(
|
||||
HumanDTO.class,
|
||||
root.get(Human_.id),
|
||||
root.get(Human_.name),
|
||||
cb.function(
|
||||
"convert",
|
||||
String.class,
|
||||
root.get(Human_.born),
|
||||
cb.literal(110)
|
||||
cb.construct(
|
||||
HumanDTO.class,
|
||||
root.get( Human_.id ),
|
||||
root.get( Human_.name ),
|
||||
cb.function(
|
||||
"convert",
|
||||
String.class,
|
||||
root.get( Human_.born ),
|
||||
cb.literal( 110 )
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
em.createQuery( criteria ).getResultList();
|
||||
|
|
|
@ -21,7 +21,6 @@ import org.hibernate.Session;
|
|||
import org.hibernate.dialect.PostgreSQL82Dialect;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.query.NativeQuery;
|
||||
import org.hibernate.query.spi.NativeQueryImplementor;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
@ -93,24 +92,24 @@ public class NativeQueryOrdinalParametersTest extends BaseEntityManagerFunctiona
|
|||
final String sqlString = "SELECT * FROM GAME g WHERE title = ?";
|
||||
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
NativeQuery sqlQuery = entityManager.unwrap( Session.class ).createSQLQuery( sqlString );
|
||||
sqlQuery.setString( 1, "Super Mario Brothers" ).setCacheable( true );
|
||||
NativeQuery sqlQuery = entityManager.unwrap( Session.class ).createNativeQuery( sqlString );
|
||||
sqlQuery.setParameter( 1, "Super Mario Brothers" ).setCacheable( true );
|
||||
|
||||
List results = sqlQuery.list();
|
||||
assertEquals( 1, results.size() );
|
||||
|
||||
NativeQueryImplementor query = (NativeQueryImplementor) entityManager.createNativeQuery( sqlString );
|
||||
query.setString( 1, "Super Mario Brothers" );
|
||||
NativeQuery query = (NativeQuery) entityManager.createNativeQuery( sqlString );
|
||||
query.setParameter( 1, "Super Mario Brothers" );
|
||||
List list = query.list();
|
||||
assertEquals( 1, list.size() );
|
||||
|
||||
sqlQuery = entityManager.unwrap( Session.class ).createSQLQuery( sqlString );
|
||||
sqlQuery.setString( 1, "Super Mario Brothers" ).setCacheable( true );
|
||||
sqlQuery = entityManager.unwrap( Session.class ).createNativeQuery( sqlString );
|
||||
sqlQuery.setParameter( 1, "Super Mario Brothers" ).setCacheable( true );
|
||||
|
||||
results = sqlQuery.list();
|
||||
assertEquals( 1, results.size() );
|
||||
|
||||
query.setString( 1, "Super Mario Brothers" );
|
||||
query.setParameter( 1, "Super Mario Brothers" );
|
||||
} );
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,9 @@ package org.hibernate.jpa.test.query;
|
|||
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.query.spi.NativeQueryImplementor;
|
||||
import org.hibernate.query.sql.internal.NativeQueryImpl;
|
||||
import org.hibernate.query.sql.spi.NativeQueryImplementor;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.After;
|
||||
|
@ -723,7 +725,10 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Tuple> getStreamedTupleAliasedResult(EntityManager entityManager) {
|
||||
NativeQueryImplementor query = (NativeQueryImplementor) entityManager.createNativeQuery("SELECT id AS alias1, firstname AS alias2 FROM users", Tuple.class);
|
||||
NativeQueryImpl query = (NativeQueryImpl) entityManager.createNativeQuery(
|
||||
"SELECT id AS alias1, firstname AS alias2 FROM users",
|
||||
Tuple.class
|
||||
);
|
||||
return (List<Tuple>) query.stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
@ -740,7 +745,7 @@ public class TupleNativeQueryTest extends BaseEntityManagerFunctionalTestCase {
|
|||
|
||||
@SuppressWarnings("unchecked")
|
||||
private List<Tuple> getStreamedTupleResult(EntityManager entityManager) {
|
||||
NativeQueryImplementor query = (NativeQueryImplementor) entityManager.createNativeQuery("SELECT id, firstname FROM users", Tuple.class);
|
||||
NativeQueryImplementor query = (NativeQueryImplementor) entityManager.createNativeQuery( "SELECT id, firstname FROM users", Tuple.class);
|
||||
return (List<Tuple>) query.stream().collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
|
|
@ -18,7 +18,6 @@ import org.hibernate.Transaction;
|
|||
import org.hibernate.annotations.Formula;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.hibernate.dialect.H2Dialect;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
|
|
|
@ -8,13 +8,15 @@ package org.hibernate.test.annotations.naturalid;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.metadata.ClassMetadata;
|
||||
import org.hibernate.stat.Statistics;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
|
@ -94,7 +96,8 @@ public class NaturalIdOnSingleManyToOneTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
s = openSession();
|
||||
tx = s.beginTransaction();
|
||||
Criteria criteria = s.createCriteria( NaturalIdOnManyToOne.class );
|
||||
CriteriaQuery<NaturalIdOnManyToOne> query = s.getCriteriaBuilder().createQuery( NaturalIdOnManyToOne.class );
|
||||
query.
|
||||
criteria.add( Restrictions.naturalId().set( "citizen", c1 ) );
|
||||
criteria.setCacheable( true );
|
||||
|
||||
|
|
|
@ -8,13 +8,16 @@ package org.hibernate.test.annotations.naturalid;
|
|||
|
||||
import java.util.List;
|
||||
|
||||
import org.hibernate.Criteria;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.NaturalIdLoadAccess;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.metadata.ClassMetadata;
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.stat.Statistics;
|
||||
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
@ -315,10 +318,14 @@ public class NaturalIdTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
|
||||
private State getState(Session s, String name) {
|
||||
Criteria criteria = s.createCriteria( State.class );
|
||||
criteria.add( Restrictions.eq( "name", name ) );
|
||||
criteria.setCacheable( true );
|
||||
return (State) criteria.list().get( 0 );
|
||||
final CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<State> criteria = criteriaBuilder.createQuery( State.class );
|
||||
Root<State> root = criteria.from( State.class );
|
||||
criteria.select( root ).where( criteriaBuilder.equal( root.get( "name" ), name ) );
|
||||
|
||||
Query<State> query = s.createQuery( criteria );
|
||||
query.setCacheable( true );
|
||||
return query.list().get( 0 );
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -10,7 +10,6 @@ import org.hibernate.Session;
|
|||
import org.hibernate.Transaction;
|
||||
import org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.dialect.TeradataDialect;
|
||||
import org.hibernate.testing.SkipForDialect;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
|
|
@ -10,9 +10,13 @@ import java.util.Arrays;
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.LockMode;
|
||||
import org.hibernate.LockOptions;
|
||||
|
@ -20,21 +24,23 @@ import org.hibernate.Session;
|
|||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Order;
|
||||
import org.hibernate.criterion.Projections;
|
||||
import org.hibernate.dialect.SQLServer2005Dialect;
|
||||
import org.hibernate.exception.LockTimeoutException;
|
||||
import org.junit.Test;
|
||||
import org.hibernate.query.Query;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
//import org.hibernate.criterion.Order;
|
||||
//import org.hibernate.criterion.Projections;
|
||||
|
||||
/**
|
||||
* used driver hibernate.connection.driver_class com.microsoft.sqlserver.jdbc.SQLServerDriver
|
||||
*
|
||||
|
@ -257,14 +263,24 @@ public class SQLServerDialectTest extends BaseCoreFunctionalTestCase {
|
|||
session.clear();
|
||||
|
||||
// count number of products in each category
|
||||
List<Object[]> result = session.createCriteria( Category.class, "c" ).createAlias( "products", "p" )
|
||||
.setProjection(
|
||||
Projections.projectionList()
|
||||
.add( Projections.groupProperty( "c.id" ) )
|
||||
.add( Projections.countDistinct( "p.id" ) )
|
||||
)
|
||||
.addOrder( Order.asc( "c.id" ) )
|
||||
.setFirstResult( 1 ).setMaxResults( 3 ).list();
|
||||
// List<Object[]> result = session.createCriteria( Category.class, "c" ).createAlias( "products", "p" )
|
||||
// .setProjection(
|
||||
// Projections.projectionList()
|
||||
// .add( Projections.groupProperty( "c.id" ) )
|
||||
// .add( Projections.countDistinct( "p.id" ) )
|
||||
// )
|
||||
// .addOrder( Order.asc( "c.id" ) )
|
||||
// .setFirstResult( 1 ).setMaxResults( 3 ).list();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<Object[]> criteria = criteriaBuilder.createQuery( Object[].class );
|
||||
Root<Category> root = criteria.from( Category.class );
|
||||
Join<Object, Object> products = root.join( "products", JoinType.INNER );
|
||||
criteria.multiselect( root.get( "id" ), criteriaBuilder.countDistinct( products.get( "id" ) ) );
|
||||
criteria.orderBy( criteriaBuilder.asc( root.get( "id" ) ) );
|
||||
Query<Object[]> query = session.createQuery( criteria );
|
||||
|
||||
List<Object[]> result = query.setFirstResult( 1 ).setMaxResults( 3 ).list();
|
||||
|
||||
assertEquals( 2, result.size() );
|
||||
assertArrayEquals( new Object[] { 2, 2L }, result.get( 0 ) ); // two products of second category
|
||||
|
|
|
@ -20,7 +20,7 @@ import java.util.Map;
|
|||
|
||||
import org.hibernate.Hibernate;
|
||||
import org.hibernate.HibernateException;
|
||||
import org.hibernate.Query;
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.QueryException;
|
||||
import org.hibernate.ScrollableResults;
|
||||
import org.hibernate.Session;
|
||||
|
@ -946,7 +946,7 @@ public class ASTParserLoadingTest extends BaseCoreFunctionalTestCase {
|
|||
// simple syntax checking...
|
||||
Session s = openSession();
|
||||
s.beginTransaction();
|
||||
s.createQuery( "from Human h where h.nickName = '1' || 'ov' || 'tha' || 'few'" ).list();
|
||||
s.createQuery( "from Human h wherFe h.nickName = '1' || 'ov' || 'tha' || 'few'" ).list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
}
|
||||
|
|
|
@ -15,7 +15,6 @@ import org.hibernate.Session;
|
|||
import org.hibernate.Transaction;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.test.jpa.AbstractJPATest;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
|
|
@ -19,9 +19,14 @@ import javax.persistence.FetchType;
|
|||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Fetch;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.dialect.AbstractHANADialect;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.query.Query;
|
||||
|
@ -29,6 +34,7 @@ import org.hibernate.testing.RequiresDialect;
|
|||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.hibernate.test.annotations.naturalid.State;
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
|
@ -118,10 +124,21 @@ public class QueryHintHANATest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
// test Criteria
|
||||
doInHibernate( this::sessionFactory, s -> {
|
||||
Criteria criteria = s.createCriteria( Employee.class )
|
||||
.addQueryHint( "NO_CS_JOIN" )
|
||||
.createCriteria( "department" ).add( Restrictions.eq( "name", "Sales" ) );
|
||||
List<?> results = criteria.list();
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery( Employee.class );
|
||||
Root<Employee> root = criteria.from( Employee.class );
|
||||
Join<Employee, Department> department = root.join( "department", JoinType.INNER );
|
||||
criteria.select( root ).where( criteriaBuilder.equal( department.<String>get("name"), "Sales" ) );
|
||||
|
||||
Query<Employee> query = s.createQuery( criteria );
|
||||
query.addQueryHint( "NO_CS_JOIN" );
|
||||
|
||||
List<Employee> results = query.list();
|
||||
|
||||
// Criteria criteria = s.createCriteria( Employee.class )
|
||||
// .addQueryHint( "NO_CS_JOIN" )
|
||||
// .createCriteria( "department" ).add( Restrictions.eq( "name", "Sales" ) );
|
||||
// List<?> results = criteria.list();
|
||||
|
||||
assertEquals( results.size(), 2 );
|
||||
} );
|
||||
|
|
|
@ -12,12 +12,16 @@ import javax.persistence.Entity;
|
|||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.JoinType;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.query.Query;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.dialect.SQLServer2012Dialect;
|
||||
|
||||
import org.hibernate.testing.RequiresDialect;
|
||||
|
@ -107,9 +111,18 @@ public class QueryHintSQLServer2012Test extends BaseCoreFunctionalTestCase {
|
|||
|
||||
// test Criteria
|
||||
s.getTransaction().begin();
|
||||
Criteria criteria = s.createCriteria( Employee.class ).addQueryHint( "MAXDOP 2" ).createCriteria( "department" )
|
||||
.add( Restrictions.eq( "name", "Sales" ) );
|
||||
results = criteria.list();
|
||||
|
||||
CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery( Employee.class );
|
||||
Root<Employee> root = criteria.from( Employee.class );
|
||||
Join<Object, Object> departement = root.join( "departement", JoinType.INNER );
|
||||
criteria.select( root ).where( criteriaBuilder.equal( departement.get( "name" ), "Sales" ) );
|
||||
// Criteria criteria = s.createCriteria( Employee.class ).addQueryHint( "MAXDOP 2" ).createCriteria( "department" )
|
||||
// .add( Restrictions.eq( "name", "Sales" ) );
|
||||
// results = criteria.list();
|
||||
Query<Employee> criteriaQuery = s.createQuery( criteria );
|
||||
criteriaQuery.addQueryHint( "MAXDOP 2" );
|
||||
results = criteriaQuery.list();
|
||||
s.getTransaction().commit();
|
||||
s.close();
|
||||
|
||||
|
|
|
@ -13,9 +13,12 @@ import javax.persistence.FetchType;
|
|||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Join;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.criterion.Restrictions;
|
||||
import org.hibernate.dialect.Oracle8iDialect;
|
||||
import org.hibernate.engine.spi.SessionFactoryImplementor;
|
||||
import org.hibernate.query.Query;
|
||||
|
@ -117,10 +120,17 @@ public class QueryHintTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
|
||||
// test Criteria
|
||||
doInHibernate( this::sessionFactory, s -> {
|
||||
Criteria criteria = s.createCriteria( Employee.class )
|
||||
.addQueryHint( "ALL_ROWS" )
|
||||
.createCriteria( "department" ).add( Restrictions.eq( "name", "Sales" ) );
|
||||
List results = criteria.list();
|
||||
final CriteriaBuilder criteriaBuilder = s.getCriteriaBuilder();
|
||||
CriteriaQuery<Employee> criteria = criteriaBuilder.createQuery( Employee.class );
|
||||
Root<Employee> root = criteria.from( Employee.class );
|
||||
Join<Object, Object> departmentJoin = root.join( "department" );
|
||||
criteria.select( root ).where( criteriaBuilder.equal( departmentJoin.get( "name" ),"Sales" ) );
|
||||
// Criteria criteria = s.createCriteria( Employee.class )
|
||||
// .addQueryHint( "ALL_ROWS" )
|
||||
// .createCriteria( "department" ).add( Restrictions.eq( "name", "Sales" ) );
|
||||
Query<Employee> query = s.createQuery( criteria );
|
||||
query.addQueryHint( "ALL_ROWS" );
|
||||
List results = query.list();
|
||||
|
||||
assertEquals(results.size(), 2);
|
||||
} );
|
||||
|
|
Loading…
Reference in New Issue