6 - SQM based on JPA type system
This commit is contained in:
parent
4fd34e4a57
commit
ff1650c66c
|
@ -6,13 +6,10 @@
|
|||
*/
|
||||
package org.hibernate.test.any;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import org.hibernate.JDBCException;
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.AvailableSettings;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.hql.internal.ast.QuerySyntaxException;
|
||||
import org.hibernate.query.SemanticException;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||
|
@ -23,7 +20,7 @@ import static org.hibernate.testing.junit4.ExtraAssertions.assertTyping;
|
|||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-1663" )
|
||||
@TestForIssue(jiraKey = "HHH-1663")
|
||||
public class AnyTypeTest extends BaseCoreFunctionalTestCase {
|
||||
@Override
|
||||
public String[] getMappings() {
|
||||
|
@ -38,28 +35,28 @@ public class AnyTypeTest extends BaseCoreFunctionalTestCase {
|
|||
|
||||
@Test
|
||||
public void testFlushProcessing() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Person person = new Person();
|
||||
Address address = new Address();
|
||||
person.setData( address );
|
||||
session.saveOrUpdate(person);
|
||||
session.saveOrUpdate(address);
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.saveOrUpdate( person );
|
||||
session.saveOrUpdate( address );
|
||||
}
|
||||
);
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
person = (Person) session.load( Person.class, person.getId() );
|
||||
person.setName("makingpersondirty");
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
inTransaction(
|
||||
session -> {
|
||||
Person p = session.load( Person.class, person.getId() );
|
||||
p.setName( "makingpersondirty" );
|
||||
}
|
||||
);
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
session.delete( person );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
inTransaction(
|
||||
session -> {
|
||||
session.delete( person );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -73,11 +70,16 @@ public class AnyTypeTest extends BaseCoreFunctionalTestCase {
|
|||
}
|
||||
catch (IllegalArgumentException e) {
|
||||
//expected
|
||||
assertTyping( QuerySyntaxException.class, e.getCause() );
|
||||
session.getTransaction().rollback();
|
||||
assertTyping( SemanticException.class, e.getCause() );
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
}
|
||||
catch (QuerySyntaxException qe) {
|
||||
catch (SemanticException qe) {
|
||||
//expected
|
||||
if ( session.getTransaction().isActive() ) {
|
||||
session.getTransaction().rollback();
|
||||
}
|
||||
}
|
||||
finally {
|
||||
session.close();
|
||||
|
|
|
@ -7,10 +7,11 @@
|
|||
package org.hibernate.test.pagination;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
|
||||
import org.hibernate.query.NativeQuery;
|
||||
import org.hibernate.query.Query;
|
||||
|
@ -18,14 +19,13 @@ import org.hibernate.Session;
|
|||
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
* @author Gavin King
|
||||
|
@ -49,95 +49,55 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
comment = "Dialect does not support limit"
|
||||
)
|
||||
public void testLimit() {
|
||||
prepareTestData();
|
||||
inTransaction(
|
||||
session -> {
|
||||
int count;
|
||||
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
count = generateBaseHQLQuery( session )
|
||||
.setMaxResults( 5 )
|
||||
.list()
|
||||
.size();
|
||||
assertEquals( 5, count );
|
||||
|
||||
int count;
|
||||
count = generateBaseQuery( session )
|
||||
.setMaxResults( 18 )
|
||||
.list()
|
||||
.size();
|
||||
assertEquals( 18, count );
|
||||
|
||||
count = generateBaseHQLQuery( session )
|
||||
.setMaxResults( 5 )
|
||||
.list()
|
||||
.size();
|
||||
assertEquals( 5, count );
|
||||
|
||||
count = generateBaseCriteria( session )
|
||||
.setMaxResults( 18 )
|
||||
.list()
|
||||
.size();
|
||||
assertEquals( 18, count );
|
||||
|
||||
count = generateBaseSQLQuery( session )
|
||||
.setMaxResults( 13 )
|
||||
.list()
|
||||
.size();
|
||||
assertEquals( 13, count );
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
cleanupTestData();
|
||||
count = generateBaseSQLQuery( session )
|
||||
.setMaxResults( 13 )
|
||||
.list()
|
||||
.size();
|
||||
assertEquals( 13, count );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOffset() {
|
||||
prepareTestData();
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
List result;
|
||||
inTransaction(
|
||||
session -> {
|
||||
List result;
|
||||
|
||||
result = generateBaseHQLQuery( session )
|
||||
.setFirstResult( 3 )
|
||||
.list();
|
||||
DataPoint firstDataPointHQL = (DataPoint) result.get( 0 );
|
||||
result = generateBaseHQLQuery( session )
|
||||
.setFirstResult( 3 )
|
||||
.list();
|
||||
DataPoint firstDataPointHQL = (DataPoint) result.get( 0 );
|
||||
|
||||
result = generateBaseCriteria( session )
|
||||
.setFirstResult( 3 )
|
||||
.list();
|
||||
DataPoint firstDataPointCriteria = (DataPoint) result.get( 0 );
|
||||
result = generateBaseQuery( session )
|
||||
.setFirstResult( 3 )
|
||||
.list();
|
||||
DataPoint firstDataPointCriteria = (DataPoint) result.get( 0 );
|
||||
|
||||
assertEquals( "The first entry should be the same in HQL and Criteria", firstDataPointHQL, firstDataPointHQL );
|
||||
assertEquals( "Wrong first result", 3, firstDataPointCriteria.getSequence() );
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
cleanupTestData();
|
||||
}
|
||||
|
||||
/**
|
||||
* @author Piotr Findeisen <piotr.findeisen@gmail.com>
|
||||
*/
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-951" )
|
||||
@RequiresDialectFeature(
|
||||
value = DialectChecks.SupportLimitCheck.class,
|
||||
comment = "Dialect does not support limit"
|
||||
)
|
||||
public void testLimitWithExpreesionAndFetchJoin() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
String hql = "SELECT b, 1 FROM DataMetaPoint b inner join fetch b.dataPoint dp";
|
||||
session.createQuery(hql)
|
||||
.setMaxResults(3)
|
||||
// This should not fail
|
||||
.list();
|
||||
|
||||
HQLQueryPlan queryPlan = new HQLQueryPlan(hql, false, Collections.EMPTY_MAP, sessionFactory());
|
||||
String sqlQuery = queryPlan.getTranslators()[0]
|
||||
.collectSqlStrings().get(0);
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Matcher matcher = Pattern.compile(
|
||||
"(?is)\\b(?<column>\\w+\\.\\w+)\\s+as\\s+(?<alias>\\w+)\\b.*\\k<column>\\s+as\\s+\\k<alias>")
|
||||
.matcher(sqlQuery);
|
||||
if (matcher.find()) {
|
||||
fail(format("Column %s mapped to alias %s twice in generated SQL: %s", matcher.group("column"),
|
||||
matcher.group("alias"), sqlQuery));
|
||||
}
|
||||
assertEquals(
|
||||
"The first entry should be the same in HQL and Criteria",
|
||||
firstDataPointHQL,
|
||||
firstDataPointHQL
|
||||
);
|
||||
assertEquals( "Wrong first result", 3, firstDataPointCriteria.getSequence() );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -146,73 +106,72 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
comment = "Dialect does not support limit+offset"
|
||||
)
|
||||
public void testLimitOffset() {
|
||||
prepareTestData();
|
||||
inTransaction(
|
||||
session -> {
|
||||
List result;
|
||||
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
result = generateBaseHQLQuery( session )
|
||||
.setFirstResult( 0 )
|
||||
.setMaxResults( 20 )
|
||||
.list();
|
||||
assertEquals( 20, result.size() );
|
||||
assertEquals( 0, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||
assertEquals( 1, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
||||
|
||||
List result;
|
||||
result = generateBaseQuery( session )
|
||||
.setFirstResult( 1 )
|
||||
.setMaxResults( 20 )
|
||||
.list();
|
||||
assertEquals( 20, result.size() );
|
||||
assertEquals( 1, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||
assertEquals( 2, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
||||
|
||||
result = generateBaseHQLQuery( session )
|
||||
.setFirstResult( 0 )
|
||||
.setMaxResults( 20 )
|
||||
.list();
|
||||
assertEquals( 20, result.size() );
|
||||
assertEquals( 0, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||
assertEquals( 1, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
||||
result = generateBaseQuery( session )
|
||||
.setFirstResult( 99 )
|
||||
.setMaxResults( Integer.MAX_VALUE - 200 )
|
||||
.list();
|
||||
assertEquals( 1, result.size() );
|
||||
assertEquals( 99, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||
|
||||
result = generateBaseCriteria( session )
|
||||
.setFirstResult( 1 )
|
||||
.setMaxResults( 20 )
|
||||
.list();
|
||||
assertEquals( 20, result.size() );
|
||||
assertEquals( 1, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||
assertEquals( 2, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
||||
result = session.createQuery( "select distinct description from DataPoint order by description" )
|
||||
.setFirstResult( 2 )
|
||||
.setMaxResults( 3 )
|
||||
.list();
|
||||
assertEquals( 3, result.size() );
|
||||
assertEquals( "Description: 2", result.get( 0 ) );
|
||||
assertEquals( "Description: 3", result.get( 1 ) );
|
||||
assertEquals( "Description: 4", result.get( 2 ) );
|
||||
|
||||
result = generateBaseCriteria( session )
|
||||
.setFirstResult( 99 )
|
||||
.setMaxResults( Integer.MAX_VALUE - 200 )
|
||||
.list();
|
||||
assertEquals( 1, result.size() );
|
||||
assertEquals( 99, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||
result = session.createNativeQuery(
|
||||
"select description, xval, yval from DataPoint order by xval, yval" )
|
||||
.setFirstResult( 2 )
|
||||
.setMaxResults( 5 )
|
||||
.list();
|
||||
assertEquals( 5, result.size() );
|
||||
Object[] row = (Object[]) result.get( 0 );
|
||||
assertTrue( row[0] instanceof String );
|
||||
|
||||
result = session.createQuery( "select distinct description from DataPoint order by description" )
|
||||
.setFirstResult( 2 )
|
||||
.setMaxResults( 3 )
|
||||
.list();
|
||||
assertEquals( 3, result.size() );
|
||||
assertEquals( "Description: 2", result.get( 0 ) );
|
||||
assertEquals( "Description: 3", result.get( 1 ) );
|
||||
assertEquals( "Description: 4", result.get( 2 ) );
|
||||
result = session.createNativeQuery( "select * from DataPoint order by xval, yval" )
|
||||
.setFirstResult( 2 )
|
||||
.setMaxResults( 5 )
|
||||
.list();
|
||||
assertEquals( 5, result.size() );
|
||||
|
||||
result = session.createNativeQuery( "select description, xval, yval from DataPoint order by xval, yval" )
|
||||
.setFirstResult( 2 )
|
||||
.setMaxResults( 5 )
|
||||
.list();
|
||||
assertEquals( 5, result.size() );
|
||||
Object[] row = (Object[]) result.get( 0 );
|
||||
assertTrue( row[0] instanceof String );
|
||||
|
||||
result = session.createNativeQuery( "select * from DataPoint order by xval, yval" )
|
||||
.setFirstResult( 2 )
|
||||
.setMaxResults( 5 )
|
||||
.list();
|
||||
assertEquals( 5, result.size() );
|
||||
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
cleanupTestData();
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private Query generateBaseHQLQuery(Session session) {
|
||||
return session.createQuery( "select dp from DataPoint dp order by dp.sequence" );
|
||||
}
|
||||
|
||||
private Criteria generateBaseCriteria(Session session) {
|
||||
return session.createCriteria( DataPoint.class )
|
||||
.addOrder( Order.asc( "sequence" ) );
|
||||
private Query generateBaseQuery(Session session) {
|
||||
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||
CriteriaQuery<DataPoint> criteria = criteriaBuilder.createQuery( DataPoint.class );
|
||||
Root<DataPoint> root = criteria.from( DataPoint.class );
|
||||
return session.createQuery( criteria.orderBy( criteriaBuilder.asc( root ) ) );
|
||||
// return session.createCriteria( DataPoint.class )
|
||||
// .addOrder( Order.asc( "sequence" ) );
|
||||
}
|
||||
|
||||
private NativeQuery generateBaseSQLQuery(Session session) {
|
||||
|
@ -220,23 +179,28 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
.addEntity( DataPoint.class );
|
||||
}
|
||||
|
||||
private void prepareTestData() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
for ( int i = 0; i < NUMBER_OF_TEST_ROWS; i++ ) {
|
||||
DataPoint dataPoint = new DataPoint();
|
||||
dataPoint.setSequence( i );
|
||||
dataPoint.setDescription( "data point #" + i );
|
||||
BigDecimal x = new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN );
|
||||
dataPoint.setX( x );
|
||||
dataPoint.setY( new BigDecimal( Math.cos( x.doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) );
|
||||
dataPoint.setDescription( "Description: " + i % 5 );
|
||||
session.save( dataPoint );
|
||||
}
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
@Before
|
||||
public void prepareTestData() {
|
||||
inTransaction(
|
||||
session -> {
|
||||
for ( int i = 0; i < NUMBER_OF_TEST_ROWS; i++ ) {
|
||||
DataPoint dataPoint = new DataPoint();
|
||||
dataPoint.setSequence( i );
|
||||
dataPoint.setDescription( "data point #" + i );
|
||||
BigDecimal x = new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN );
|
||||
dataPoint.setX( x );
|
||||
dataPoint.setY( new BigDecimal( Math.cos( x.doubleValue() ) ).setScale(
|
||||
19,
|
||||
BigDecimal.ROUND_DOWN
|
||||
) );
|
||||
dataPoint.setDescription( "Description: " + i % 5 );
|
||||
session.save( dataPoint );
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupTestData() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
|
@ -245,4 +209,3 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
|||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/*
|
||||
* 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.test.pagination;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.hibernate.Session;
|
||||
|
||||
import org.hibernate.testing.DialectChecks;
|
||||
import org.hibernate.testing.RequiresDialectFeature;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
import static java.lang.String.format;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
public class LimitWithExpreesionAndFetchJoinTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||
/**
|
||||
* @author Piotr Findeisen <piotr.findeisen@gmail.com>
|
||||
*/
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-951" )
|
||||
@RequiresDialectFeature(
|
||||
value = DialectChecks.SupportLimitCheck.class,
|
||||
comment = "Dialect does not support limit"
|
||||
)
|
||||
public void testLimitWithExpreesionAndFetchJoin() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
|
||||
String hql = "SELECT b, 1 FROM DataMetaPoint b inner join fetch b.dataPoint dp";
|
||||
session.createQuery(hql)
|
||||
.setMaxResults(3)
|
||||
// This should not fail
|
||||
.list();
|
||||
|
||||
HQLQueryPlan queryPlan = new HQLQueryPlan( hql, false, Collections.EMPTY_MAP, sessionFactory());
|
||||
String sqlQuery = queryPlan.getTranslators()[0]
|
||||
.collectSqlStrings().get(0);
|
||||
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
Matcher matcher = Pattern.compile(
|
||||
"(?is)\\b(?<column>\\w+\\.\\w+)\\s+as\\s+(?<alias>\\w+)\\b.*\\k<column>\\s+as\\s+\\k<alias>")
|
||||
.matcher(sqlQuery);
|
||||
if (matcher.find()) {
|
||||
fail(format("Column %s mapped to alias %s twice in generated SQL: %s", matcher.group("column"),
|
||||
matcher.group("alias"), sqlQuery));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue