Skip to content

Commit

Permalink
Test case for HHH-11258
Browse files Browse the repository at this point in the history
  • Loading branch information
beikov authored and sebersole committed Nov 23, 2016
1 parent 45a65da commit 4d07611
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 10 deletions.
Expand Up @@ -245,7 +245,7 @@ public void testWithClauseAsSubqueryWithKey() {
@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();
Expand Down
@@ -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;
}
}
@@ -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();
}
}

}
Expand Up @@ -10,6 +10,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.util.*;
import java.util.function.Consumer;
import javax.persistence.SharedCacheMode;

import org.hibernate.HibernateException;
Expand Down Expand Up @@ -95,16 +96,14 @@ protected Session openSession(Interceptor interceptor) throws HibernateException
@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 );
Expand All @@ -115,10 +114,10 @@ protected void buildSessionFactory(Map<String, String> properties) {
}

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;
}
Expand All @@ -132,7 +131,7 @@ protected void rebuildSessionFactory(Map<String, String> properties) {
catch (Exception ignore) {
}

buildSessionFactory( properties );
buildSessionFactory( configurationAdapter );
}

protected Configuration buildConfiguration() {
Expand Down

0 comments on commit 4d07611

Please sign in to comment.