Test case for HHH-11258

This commit is contained in:
Christian Beikov 2016-11-18 10:25:06 +01:00 committed by Steve Ebersole
parent 45a65daf02
commit 4d076111a3
4 changed files with 148 additions and 10 deletions

View File

@ -245,7 +245,7 @@ public class WithClauseTest extends BaseCoreFunctionalTestCase {
@Test
@TestForIssue(jiraKey = "HHH-11157")
public void testWithClauseAsNonSubqueryWithKey() {
rebuildSessionFactory( Collections.singletonMap( AvailableSettings.COLLECTION_JOIN_SUBQUERY, "false" ) );
rebuildSessionFactory( c -> c.setProperty( AvailableSettings.COLLECTION_JOIN_SUBQUERY, "false" ) );
try {
TestData data = new TestData();

View File

@ -0,0 +1,44 @@
/*
* 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>.
*/
//$Id$
package org.hibernate.test.subquery;
import javax.persistence.*;
@Entity
public class EntityA {
@Id
@Column(name = "id", nullable = false)
private int id;
private String name;
public EntityA() {
}
public EntityA(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

View File

@ -0,0 +1,95 @@
/*
* 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>.
*/
//$Id$
package org.hibernate.test.subquery;
import org.hibernate.QueryException;
import org.hibernate.Session;
import org.hibernate.dialect.function.SQLFunction;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.type.Type;
import org.junit.Test;
import javax.persistence.Query;
import java.util.List;
import java.util.function.Consumer;
/**
* Test some subquery scenarios.
*
* @author Christian Beikov
*/
public class SubqueryTest extends BaseCoreFunctionalTestCase {
private static final SQLFunction LIMIT_FUNCTION = new SQLFunction() {
public boolean hasArguments() {
return true;
}
public boolean hasParenthesesIfNoArguments() {
return true;
}
public Type getReturnType(Type type, Mapping mpng) throws QueryException {
return type;
}
public String render(Type type, List list, SessionFactoryImplementor sfi) throws QueryException {
String subquery = list.get(0).toString();
return subquery.substring(0, subquery.length() - 1) + " limit " + list.get(1) + ")";
}
};
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[]{
EntityA.class
};
}
@Test
public void testNestedOrderBySubqueryInFunction() {
withLimit(s -> {
Query q = s.createQuery(
"SELECT a.id FROM EntityA a " +
"ORDER BY CASE WHEN (" +
"SELECT 1 FROM EntityA s1 " +
"WHERE s1.id IN(" +
"LIMIT(" +
"(" +
"SELECT 1 FROM EntityA sub " +
"ORDER BY " +
"CASE WHEN sub.name IS NULL THEN 1 ELSE 0 END, " +
"sub.name DESC, " +
"CASE WHEN sub.id IS NULL THEN 1 ELSE 0 END, " +
"sub.id DESC" +
")," +
"1)" +
")" +
") = 1 THEN 1 ELSE 0 END"
);
q.getResultList();
});
}
private void withLimit(Consumer<Session> consumer) {
rebuildSessionFactory( c -> c.addSqlFunction( "limit", LIMIT_FUNCTION ) );
try {
Session s = openSession();
consumer.accept( s );
} finally {
// Rebuild to remove the function
rebuildSessionFactory();
}
}
}

View File

@ -10,6 +10,7 @@ import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Consumer;
import javax.persistence.SharedCacheMode;
import org.hibernate.HibernateException;
@ -95,16 +96,14 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
@BeforeClassOnce
@SuppressWarnings( {"UnusedDeclaration"})
protected void buildSessionFactory() {
buildSessionFactory( Collections.emptyMap() );
buildSessionFactory( null );
}
protected void buildSessionFactory(Map<String, String> properties) {
protected void buildSessionFactory(Consumer<Configuration> configurationAdapter) {
// for now, build the configuration to get all the property settings
configuration = constructAndConfigureConfiguration();
if ( properties != null && !properties.isEmpty() ) {
for ( Map.Entry<String, String> entry : properties.entrySet() ) {
configuration.setProperty( entry.getKey(), entry.getValue() );
}
if ( configurationAdapter != null ) {
configurationAdapter.accept(configuration);
}
BootstrapServiceRegistry bootRegistry = buildBootstrapServiceRegistry();
serviceRegistry = buildServiceRegistry( bootRegistry, configuration );
@ -115,10 +114,10 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
}
protected void rebuildSessionFactory() {
rebuildSessionFactory( Collections.emptyMap() );
rebuildSessionFactory( null );
}
protected void rebuildSessionFactory(Map<String, String> properties) {
protected void rebuildSessionFactory(Consumer<Configuration> configurationAdapter) {
if ( sessionFactory == null ) {
return;
}
@ -132,7 +131,7 @@ public abstract class BaseCoreFunctionalTestCase extends BaseUnitTestCase {
catch (Exception ignore) {
}
buildSessionFactory( properties );
buildSessionFactory( configurationAdapter );
}
protected Configuration buildConfiguration() {