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;
|
package org.hibernate.test.any;
|
||||||
|
|
||||||
import javax.persistence.PersistenceException;
|
|
||||||
|
|
||||||
import org.hibernate.JDBCException;
|
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.cfg.Configuration;
|
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.TestForIssue;
|
||||||
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
|
||||||
|
@ -38,28 +35,28 @@ public class AnyTypeTest extends BaseCoreFunctionalTestCase {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFlushProcessing() {
|
public void testFlushProcessing() {
|
||||||
Session session = openSession();
|
|
||||||
session.beginTransaction();
|
|
||||||
Person person = new Person();
|
Person person = new Person();
|
||||||
Address address = new Address();
|
Address address = new Address();
|
||||||
person.setData( address );
|
person.setData( address );
|
||||||
|
inTransaction(
|
||||||
|
session -> {
|
||||||
session.saveOrUpdate( person );
|
session.saveOrUpdate( person );
|
||||||
session.saveOrUpdate( address );
|
session.saveOrUpdate( address );
|
||||||
session.getTransaction().commit();
|
}
|
||||||
session.close();
|
);
|
||||||
|
|
||||||
session = openSession();
|
inTransaction(
|
||||||
session.beginTransaction();
|
session -> {
|
||||||
person = (Person) session.load( Person.class, person.getId() );
|
Person p = session.load( Person.class, person.getId() );
|
||||||
person.setName("makingpersondirty");
|
p.setName( "makingpersondirty" );
|
||||||
session.getTransaction().commit();
|
}
|
||||||
session.close();
|
);
|
||||||
|
|
||||||
session = openSession();
|
inTransaction(
|
||||||
session.beginTransaction();
|
session -> {
|
||||||
session.delete( person );
|
session.delete( person );
|
||||||
session.getTransaction().commit();
|
}
|
||||||
session.close();
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -73,11 +70,16 @@ public class AnyTypeTest extends BaseCoreFunctionalTestCase {
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e) {
|
catch (IllegalArgumentException e) {
|
||||||
//expected
|
//expected
|
||||||
assertTyping( QuerySyntaxException.class, e.getCause() );
|
assertTyping( SemanticException.class, e.getCause() );
|
||||||
|
if ( session.getTransaction().isActive() ) {
|
||||||
session.getTransaction().rollback();
|
session.getTransaction().rollback();
|
||||||
}
|
}
|
||||||
catch (QuerySyntaxException qe) {
|
}
|
||||||
|
catch (SemanticException qe) {
|
||||||
//expected
|
//expected
|
||||||
|
if ( session.getTransaction().isActive() ) {
|
||||||
|
session.getTransaction().rollback();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
finally {
|
finally {
|
||||||
session.close();
|
session.close();
|
||||||
|
|
|
@ -7,10 +7,11 @@
|
||||||
package org.hibernate.test.pagination;
|
package org.hibernate.test.pagination;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
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.NativeQuery;
|
||||||
import org.hibernate.query.Query;
|
import org.hibernate.query.Query;
|
||||||
|
@ -18,14 +19,13 @@ import org.hibernate.Session;
|
||||||
|
|
||||||
import org.hibernate.testing.DialectChecks;
|
import org.hibernate.testing.DialectChecks;
|
||||||
import org.hibernate.testing.RequiresDialectFeature;
|
import org.hibernate.testing.RequiresDialectFeature;
|
||||||
import org.hibernate.testing.TestForIssue;
|
|
||||||
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static java.lang.String.format;
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.junit.Assert.fail;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Gavin King
|
* @author Gavin King
|
||||||
|
@ -49,11 +49,8 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
comment = "Dialect does not support limit"
|
comment = "Dialect does not support limit"
|
||||||
)
|
)
|
||||||
public void testLimit() {
|
public void testLimit() {
|
||||||
prepareTestData();
|
inTransaction(
|
||||||
|
session -> {
|
||||||
Session session = openSession();
|
|
||||||
session.beginTransaction();
|
|
||||||
|
|
||||||
int count;
|
int count;
|
||||||
|
|
||||||
count = generateBaseHQLQuery( session )
|
count = generateBaseHQLQuery( session )
|
||||||
|
@ -62,7 +59,7 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
.size();
|
.size();
|
||||||
assertEquals( 5, count );
|
assertEquals( 5, count );
|
||||||
|
|
||||||
count = generateBaseCriteria( session )
|
count = generateBaseQuery( session )
|
||||||
.setMaxResults( 18 )
|
.setMaxResults( 18 )
|
||||||
.list()
|
.list()
|
||||||
.size();
|
.size();
|
||||||
|
@ -73,18 +70,14 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
.list()
|
.list()
|
||||||
.size();
|
.size();
|
||||||
assertEquals( 13, count );
|
assertEquals( 13, count );
|
||||||
|
}
|
||||||
session.getTransaction().commit();
|
);
|
||||||
session.close();
|
|
||||||
|
|
||||||
cleanupTestData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testOffset() {
|
public void testOffset() {
|
||||||
prepareTestData();
|
inTransaction(
|
||||||
Session session = openSession();
|
session -> {
|
||||||
session.beginTransaction();
|
|
||||||
List result;
|
List result;
|
||||||
|
|
||||||
result = generateBaseHQLQuery( session )
|
result = generateBaseHQLQuery( session )
|
||||||
|
@ -92,52 +85,19 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
.list();
|
.list();
|
||||||
DataPoint firstDataPointHQL = (DataPoint) result.get( 0 );
|
DataPoint firstDataPointHQL = (DataPoint) result.get( 0 );
|
||||||
|
|
||||||
result = generateBaseCriteria( session )
|
result = generateBaseQuery( session )
|
||||||
.setFirstResult( 3 )
|
.setFirstResult( 3 )
|
||||||
.list();
|
.list();
|
||||||
DataPoint firstDataPointCriteria = (DataPoint) result.get( 0 );
|
DataPoint firstDataPointCriteria = (DataPoint) result.get( 0 );
|
||||||
|
|
||||||
assertEquals( "The first entry should be the same in HQL and Criteria", firstDataPointHQL, firstDataPointHQL );
|
assertEquals(
|
||||||
|
"The first entry should be the same in HQL and Criteria",
|
||||||
|
firstDataPointHQL,
|
||||||
|
firstDataPointHQL
|
||||||
|
);
|
||||||
assertEquals( "Wrong first result", 3, firstDataPointCriteria.getSequence() );
|
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));
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -146,11 +106,8 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
comment = "Dialect does not support limit+offset"
|
comment = "Dialect does not support limit+offset"
|
||||||
)
|
)
|
||||||
public void testLimitOffset() {
|
public void testLimitOffset() {
|
||||||
prepareTestData();
|
inTransaction(
|
||||||
|
session -> {
|
||||||
Session session = openSession();
|
|
||||||
session.beginTransaction();
|
|
||||||
|
|
||||||
List result;
|
List result;
|
||||||
|
|
||||||
result = generateBaseHQLQuery( session )
|
result = generateBaseHQLQuery( session )
|
||||||
|
@ -161,7 +118,7 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( 0, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
assertEquals( 0, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||||
assertEquals( 1, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
assertEquals( 1, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
||||||
|
|
||||||
result = generateBaseCriteria( session )
|
result = generateBaseQuery( session )
|
||||||
.setFirstResult( 1 )
|
.setFirstResult( 1 )
|
||||||
.setMaxResults( 20 )
|
.setMaxResults( 20 )
|
||||||
.list();
|
.list();
|
||||||
|
@ -169,7 +126,7 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( 1, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
assertEquals( 1, ( (DataPoint) result.get( 0 ) ).getSequence() );
|
||||||
assertEquals( 2, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
assertEquals( 2, ( (DataPoint) result.get( 1 ) ).getSequence() );
|
||||||
|
|
||||||
result = generateBaseCriteria( session )
|
result = generateBaseQuery( session )
|
||||||
.setFirstResult( 99 )
|
.setFirstResult( 99 )
|
||||||
.setMaxResults( Integer.MAX_VALUE - 200 )
|
.setMaxResults( Integer.MAX_VALUE - 200 )
|
||||||
.list();
|
.list();
|
||||||
|
@ -185,7 +142,8 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
assertEquals( "Description: 3", result.get( 1 ) );
|
assertEquals( "Description: 3", result.get( 1 ) );
|
||||||
assertEquals( "Description: 4", result.get( 2 ) );
|
assertEquals( "Description: 4", result.get( 2 ) );
|
||||||
|
|
||||||
result = session.createNativeQuery( "select description, xval, yval from DataPoint order by xval, yval" )
|
result = session.createNativeQuery(
|
||||||
|
"select description, xval, yval from DataPoint order by xval, yval" )
|
||||||
.setFirstResult( 2 )
|
.setFirstResult( 2 )
|
||||||
.setMaxResults( 5 )
|
.setMaxResults( 5 )
|
||||||
.list();
|
.list();
|
||||||
|
@ -199,20 +157,21 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
.list();
|
.list();
|
||||||
assertEquals( 5, result.size() );
|
assertEquals( 5, result.size() );
|
||||||
|
|
||||||
|
}
|
||||||
session.getTransaction().commit();
|
);
|
||||||
session.close();
|
|
||||||
|
|
||||||
cleanupTestData();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Query generateBaseHQLQuery(Session session) {
|
private Query generateBaseHQLQuery(Session session) {
|
||||||
return session.createQuery( "select dp from DataPoint dp order by dp.sequence" );
|
return session.createQuery( "select dp from DataPoint dp order by dp.sequence" );
|
||||||
}
|
}
|
||||||
|
|
||||||
private Criteria generateBaseCriteria(Session session) {
|
private Query generateBaseQuery(Session session) {
|
||||||
return session.createCriteria( DataPoint.class )
|
CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
|
||||||
.addOrder( Order.asc( "sequence" ) );
|
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) {
|
private NativeQuery generateBaseSQLQuery(Session session) {
|
||||||
|
@ -220,23 +179,28 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
.addEntity( DataPoint.class );
|
.addEntity( DataPoint.class );
|
||||||
}
|
}
|
||||||
|
|
||||||
private void prepareTestData() {
|
@Before
|
||||||
Session session = openSession();
|
public void prepareTestData() {
|
||||||
session.beginTransaction();
|
inTransaction(
|
||||||
|
session -> {
|
||||||
for ( int i = 0; i < NUMBER_OF_TEST_ROWS; i++ ) {
|
for ( int i = 0; i < NUMBER_OF_TEST_ROWS; i++ ) {
|
||||||
DataPoint dataPoint = new DataPoint();
|
DataPoint dataPoint = new DataPoint();
|
||||||
dataPoint.setSequence( i );
|
dataPoint.setSequence( i );
|
||||||
dataPoint.setDescription( "data point #" + i );
|
dataPoint.setDescription( "data point #" + i );
|
||||||
BigDecimal x = new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN );
|
BigDecimal x = new BigDecimal( i * 0.1d ).setScale( 19, BigDecimal.ROUND_DOWN );
|
||||||
dataPoint.setX( x );
|
dataPoint.setX( x );
|
||||||
dataPoint.setY( new BigDecimal( Math.cos( x.doubleValue() ) ).setScale( 19, BigDecimal.ROUND_DOWN ) );
|
dataPoint.setY( new BigDecimal( Math.cos( x.doubleValue() ) ).setScale(
|
||||||
|
19,
|
||||||
|
BigDecimal.ROUND_DOWN
|
||||||
|
) );
|
||||||
dataPoint.setDescription( "Description: " + i % 5 );
|
dataPoint.setDescription( "Description: " + i % 5 );
|
||||||
session.save( dataPoint );
|
session.save( dataPoint );
|
||||||
}
|
}
|
||||||
session.getTransaction().commit();
|
}
|
||||||
session.close();
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
public void cleanupTestData() {
|
public void cleanupTestData() {
|
||||||
Session session = openSession();
|
Session session = openSession();
|
||||||
session.beginTransaction();
|
session.beginTransaction();
|
||||||
|
@ -245,4 +209,3 @@ public class PaginationTest extends BaseNonConfigCoreFunctionalTestCase {
|
||||||
session.close();
|
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