mirror of
https://github.com/hibernate/hibernate-orm
synced 2025-02-28 06:49:09 +00:00
test enablement - org.hibernate.query package
This commit is contained in:
parent
b7bbbcc19c
commit
dbed6112c8
@ -6,39 +6,33 @@
|
||||
*/
|
||||
package org.hibernate.orm.test.query.hql;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import javax.persistence.Tuple;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.jdbc.SQLStatementInspector;
|
||||
import org.hibernate.testing.orm.domain.StandardDomainModel;
|
||||
import org.hibernate.testing.orm.domain.contacts.Contact;
|
||||
import org.hibernate.testing.orm.domain.gambit.EntityOfBasics;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Christian Beikov
|
||||
* @author Jan-Willem Gmelig Meyling
|
||||
* @author Sayra Ranjha
|
||||
*/
|
||||
@ServiceRegistry
|
||||
@DomainModel(standardModels = StandardDomainModel.CONTACTS)
|
||||
@SessionFactory
|
||||
@SessionFactory( statementInspectorClass = SQLStatementInspector.class )
|
||||
public class GroupByTest {
|
||||
|
||||
@BeforeAll
|
||||
public void prepareData(SessionFactoryScope scope) {
|
||||
scope.inTransaction(
|
||||
em -> {
|
||||
Contact entity1 = new Contact();
|
||||
entity1.setId( 123 );
|
||||
em.persist( entity1 );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-1615")
|
||||
public void testGroupByEntity(SessionFactoryScope scope) {
|
||||
@ -49,4 +43,87 @@ public void testGroupByEntity(SessionFactoryScope scope) {
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-9301" )
|
||||
public void testGroupByAliasedBasicPart(SessionFactoryScope scope) {
|
||||
final SQLStatementInspector sqlStatementInspector = (SQLStatementInspector) scope.getStatementInspector();
|
||||
sqlStatementInspector.clear();
|
||||
|
||||
scope.inSession( (session) -> {
|
||||
final String qryString = "select c.id as id_alias, count(1) as occurrences"
|
||||
+ " from Contact c"
|
||||
+ " group by id_alias"
|
||||
+ " order by id_alias";
|
||||
final Tuple result = session.createQuery( qryString, Tuple.class ).uniqueResult();
|
||||
assertThat( result ).isNotNull();
|
||||
assertThat( result.get( "id_alias" ) ).isEqualTo( 123 );
|
||||
assertThat( result.get( "occurrences" ) ).isEqualTo( 1L );
|
||||
|
||||
assertThat( sqlStatementInspector.getSqlQueries() ).hasSize( 1 );
|
||||
assertThat( sqlStatementInspector.getSqlQueries().get( 0 ) ).isNotNull();
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-9301" )
|
||||
public void testGroupByAliasedCompositePart(SessionFactoryScope scope) {
|
||||
final SQLStatementInspector sqlStatementInspector = (SQLStatementInspector) scope.getStatementInspector();
|
||||
sqlStatementInspector.clear();
|
||||
|
||||
scope.inTransaction( (session) -> {
|
||||
final String qryString = "select c.name as name_alias, count(1) as occurrences"
|
||||
+ " from Contact c"
|
||||
+ " group by name_alias"
|
||||
+ " order by name_alias";
|
||||
final Tuple result = session.createQuery( qryString, Tuple.class ).uniqueResult();
|
||||
assertThat( result ).isNotNull();
|
||||
assertThat( result.get( "name_alias" ) ).isInstanceOf( Contact.Name.class );
|
||||
final Contact.Name name = result.get( "name_alias", Contact.Name.class );
|
||||
assertThat( name.getFirst() ).isEqualTo( "Johnny" );
|
||||
assertThat( name.getLast() ).isEqualTo( "Lawrence" );
|
||||
assertThat( result.get( "occurrences" ) ).isEqualTo( 1L );
|
||||
|
||||
assertThat( sqlStatementInspector.getSqlQueries() ).hasSize( 1 );
|
||||
assertThat( sqlStatementInspector.getSqlQueries().get( 0 ) ).isNotNull();
|
||||
} );
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@TestForIssue( jiraKey = "HHH-9301" )
|
||||
public void testGroupByMultipleAliases(SessionFactoryScope scope) {
|
||||
final SQLStatementInspector sqlStatementInspector = (SQLStatementInspector) scope.getStatementInspector();
|
||||
sqlStatementInspector.clear();
|
||||
|
||||
scope.inTransaction( (session) -> {
|
||||
final String qryString = "select c.id as id_alias, c.gender as gender_alias, count(1) as occurrences"
|
||||
+ " from Contact c"
|
||||
+ " group by id_alias, gender_alias"
|
||||
+ " order by id_alias, gender_alias";
|
||||
final Tuple result = session.createQuery( qryString, Tuple.class ).uniqueResult();
|
||||
assertThat( result ).isNotNull();
|
||||
assertThat( result.get( "id_alias" ) ).isEqualTo( 123 );
|
||||
assertThat( result.get( "gender_alias" ) ).isEqualTo( Contact.Gender.MALE );
|
||||
assertThat( result.get( "occurrences" ) ).isEqualTo( 1L );
|
||||
|
||||
assertThat( sqlStatementInspector.getSqlQueries() ).hasSize( 1 );
|
||||
assertThat( sqlStatementInspector.getSqlQueries().get( 0 ) ).isNotNull();
|
||||
} );
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
public void prepareData(SessionFactoryScope scope) {
|
||||
scope.inTransaction( (em) -> {
|
||||
Contact entity1 = new Contact( 123, new Contact.Name( "Johnny", "Lawrence" ), Contact.Gender.MALE, LocalDate.EPOCH );
|
||||
em.persist( entity1 );
|
||||
} );
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void dropTestData(SessionFactoryScope scope) {
|
||||
scope.inTransaction( (session) -> {
|
||||
session.createQuery( "delete Contact" ).executeUpdate();
|
||||
} );
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
/*
|
||||
* 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.orm.test.query.hql;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.ServiceRegistry;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-6686")
|
||||
@ServiceRegistry
|
||||
@DomainModel( annotatedClasses = IsEmptyPredicateTest.Person.class )
|
||||
@SessionFactory
|
||||
public class IsEmptyPredicateTest {
|
||||
|
||||
private final Integer personWithoutNicknameId = 1;
|
||||
private final Integer personaWithSingleNicknameId = 2;
|
||||
private final Integer personWithMultipleNicknamesId = 3;
|
||||
|
||||
@Test
|
||||
public void testEmptinessPredicates(SessionFactoryScope scope) {
|
||||
scope.inTransaction( (session) -> {
|
||||
final List<Integer> ids = session.createQuery( "select p.id from Person p where p.nicknames is not empty", Integer.class ).list();
|
||||
assertThat( ids ).contains( personaWithSingleNicknameId, personWithMultipleNicknamesId );
|
||||
assertThat( ids ).doesNotContain( personWithoutNicknameId );
|
||||
} );
|
||||
|
||||
scope.inTransaction( (session) -> {
|
||||
final List<Integer> ids = session.createQuery( "select p.id from Person p where p.nicknames is empty", Integer.class ).list();
|
||||
assertThat( ids ).contains( personWithoutNicknameId );
|
||||
assertThat( ids ).doesNotContain( personaWithSingleNicknameId, personWithMultipleNicknamesId );
|
||||
} );
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
protected void prepareTestData(SessionFactoryScope scope) {
|
||||
scope.inTransaction( (session) -> {
|
||||
Person personaWithoutNickname = new Person();
|
||||
personaWithoutNickname.setId(personWithoutNicknameId);
|
||||
|
||||
Person personaWithSingleNickname = new Person();
|
||||
personaWithSingleNickname.getNicknames().add( "nickname" );
|
||||
personaWithSingleNickname.setId(personaWithSingleNicknameId);
|
||||
|
||||
Person personWithMultipleNicknames = new Person();
|
||||
personWithMultipleNicknames.getNicknames().addAll( Arrays.asList( "nickName1", "nickName2" ) );
|
||||
personWithMultipleNicknames.setId(personWithMultipleNicknamesId);
|
||||
|
||||
session.persist( personaWithoutNickname );
|
||||
session.persist( personaWithSingleNickname );
|
||||
session.persist( personWithMultipleNicknames );
|
||||
} );
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
public void dropTestData(SessionFactoryScope scope) {
|
||||
scope.inTransaction( (session) -> {
|
||||
session.createQuery( "delete Person" ).executeUpdate();
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
@ElementCollection
|
||||
private List<String> nicknames = new ArrayList<>();
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<String> getNicknames() {
|
||||
return nicknames;
|
||||
}
|
||||
|
||||
public void setNicknames(List<String> nicknames) {
|
||||
this.nicknames = nicknames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,74 @@
|
||||
/*
|
||||
* 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.orm.test.query.hql;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.jdbc.SQLStatementInspector;
|
||||
import org.hibernate.testing.orm.junit.DomainModel;
|
||||
import org.hibernate.testing.orm.junit.SessionFactory;
|
||||
import org.hibernate.testing.orm.junit.SessionFactoryScope;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* @author Christian Beikov
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-14201" )
|
||||
@DomainModel( annotatedClasses = { JoinOrderTest.EntityA.class, JoinOrderTest.EntityB.class, JoinOrderTest.EntityC.class } )
|
||||
@SessionFactory( statementInspectorClass = SQLStatementInspector.class )
|
||||
public class JoinOrderTest {
|
||||
|
||||
@Test
|
||||
public void testJoinOrder(SessionFactoryScope scope) {
|
||||
scope.inTransaction( (session) -> {
|
||||
final SQLStatementInspector sqlStatementInspector = (SQLStatementInspector) scope.getStatementInspector();
|
||||
sqlStatementInspector.clear();
|
||||
|
||||
final String hql = "select 1"
|
||||
+ " from EntityA a"
|
||||
+ " join EntityB b on b.a = a "
|
||||
+ " join a.c c on c.b = b";
|
||||
session.createQuery( hql ).getResultList();
|
||||
|
||||
assertThat( sqlStatementInspector.getSqlQueries() ).hasSize( 1 );
|
||||
assertThat( sqlStatementInspector.getSqlQueries().get( 0 ) ).matches( "^.+(?: join EntityB ).+(?: join EntityC ).+$" );
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity(name = "EntityA")
|
||||
public static class EntityA {
|
||||
@Id
|
||||
int id;
|
||||
|
||||
@ManyToOne
|
||||
EntityC c;
|
||||
}
|
||||
|
||||
@Entity(name = "EntityB")
|
||||
public static class EntityB {
|
||||
@Id
|
||||
int id;
|
||||
|
||||
@ManyToOne
|
||||
EntityA a;
|
||||
}
|
||||
|
||||
@Entity(name = "EntityC")
|
||||
public static class EntityC {
|
||||
@Id
|
||||
int id;
|
||||
|
||||
@ManyToOne
|
||||
EntityB b;
|
||||
}
|
||||
}
|
@ -1,13 +1,14 @@
|
||||
/*
|
||||
* 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>.
|
||||
* 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.query.sqm.tree.select;
|
||||
package org.hibernate.orm.test.query.sqm;
|
||||
|
||||
import org.hibernate.query.criteria.JpaOrder;
|
||||
import org.hibernate.query.sqm.tree.expression.SqmExpression;
|
||||
import org.hibernate.query.sqm.tree.select.SqmSortSpecification;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
@ -23,7 +24,7 @@
|
||||
* @author seregamorph
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-13884")
|
||||
public class HHH13884Test {
|
||||
public class SortSpecificationReversalTests {
|
||||
|
||||
@Test
|
||||
public void testDefaultSqmSortSpecificationReverse() {
|
@ -1,227 +0,0 @@
|
||||
/*
|
||||
* 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.query;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.IdClass;
|
||||
import javax.persistence.ManyToOne;
|
||||
import javax.persistence.Tuple;
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
/**
|
||||
* @author Jan-Willem Gmelig Meyling
|
||||
* @author Sayra Ranjha
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-9301" )
|
||||
public class GroupByAliasTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
public static final int MAX_COUNT = 15;
|
||||
|
||||
private SQLStatementInterceptor sqlStatementInterceptor;
|
||||
|
||||
@Override
|
||||
protected void addConfigOptions(Map options) {
|
||||
sqlStatementInterceptor = new SQLStatementInterceptor( options );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class[] getAnnotatedClasses() {
|
||||
return new Class[] {
|
||||
Person.class,
|
||||
Association.class
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterEntityManagerFactoryBuilt() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
for ( int i = 0; i < MAX_COUNT; i++ ) {
|
||||
Association association = new Association();
|
||||
association.setId( i );
|
||||
association.setName(String.format( "Association nr %d", i ) );
|
||||
|
||||
Person person = new Person();
|
||||
person.setId( i );
|
||||
person.setName( String.format( "Person nr %d", i ) );
|
||||
person.setAssociation(association);
|
||||
person.setAge(5);
|
||||
entityManager.persist( person );
|
||||
}
|
||||
} );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleIdAlias() {
|
||||
sqlStatementInterceptor.clear();
|
||||
|
||||
List<Tuple> list = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return entityManager.createQuery(
|
||||
"select p.id as id_alias, sum(p.age) " +
|
||||
"from Person p group by id_alias order by id_alias", Tuple.class)
|
||||
.getResultList();
|
||||
});
|
||||
|
||||
String s = sqlStatementInterceptor.getSqlQueries().get(0);
|
||||
assertNotNull(s);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCompoundIdAlias() {
|
||||
sqlStatementInterceptor.clear();
|
||||
|
||||
List<Tuple> list = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return entityManager.createQuery(
|
||||
"select p.association as id_alias, sum(p.age) " +
|
||||
"from Person p group by id_alias, p.association.id, p.association.name order by id_alias", Tuple.class)
|
||||
.getResultList();
|
||||
});
|
||||
|
||||
String s = sqlStatementInterceptor.getSqlQueries().get(0);
|
||||
assertNotNull(s);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testMultiIdAlias() {
|
||||
sqlStatementInterceptor.clear();
|
||||
|
||||
List<Tuple> list = doInJPA(this::entityManagerFactory, entityManager -> {
|
||||
return entityManager.createQuery(
|
||||
"select p.id as id_alias_1, p.association.id as id_alias_2, sum(p.age) " +
|
||||
"from Person p group by id_alias_1, id_alias_2 order by id_alias_1, id_alias_2 ", Tuple.class)
|
||||
.getResultList();
|
||||
});
|
||||
|
||||
String s = sqlStatementInterceptor.getSqlQueries().get(0);
|
||||
assertNotNull(s);
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
private String name;
|
||||
|
||||
private Integer age;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
||||
private Association association;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Integer getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(Integer age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public Association getAssociation() {
|
||||
return association;
|
||||
}
|
||||
|
||||
public void setAssociation(Association association) {
|
||||
this.association = association;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@IdClass(Association.IdClass.class)
|
||||
@Entity(name = "Association")
|
||||
public static class Association {
|
||||
|
||||
public static class IdClass implements Serializable {
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
IdClass id1 = (IdClass) o;
|
||||
return Objects.equals(id, id1.id) &&
|
||||
Objects.equals(name, id1.name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name);
|
||||
}
|
||||
}
|
||||
|
||||
@Id
|
||||
private Integer id;
|
||||
|
||||
@Id
|
||||
private String name;
|
||||
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Integer id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -1,97 +0,0 @@
|
||||
package org.hibernate.query;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.junit.Test;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
@TestForIssue(jiraKey = "HHH-6686")
|
||||
public class IsEmptyJQLTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
private Long personWithoutNicknameId = 1L;
|
||||
private Long personaWithSingleNicknameId = 2L;
|
||||
private Long personWithMultipleNicknamesId = 3L;
|
||||
|
||||
@Override
|
||||
public Class<?>[] getAnnotatedClasses() {
|
||||
return new Class[] { Person.class };
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJQLContainingEmpty() {
|
||||
List<Person> personWithNicknames = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
return entityManager.createQuery(
|
||||
"select p from Person p where p.nicknames is not empty", Person.class )
|
||||
.getResultList();
|
||||
});
|
||||
|
||||
assertEquals( new HashSet<>( Arrays.asList(personaWithSingleNicknameId, personWithMultipleNicknamesId)),
|
||||
personWithNicknames.stream().map( Person::getId ).collect( Collectors.toSet() ));
|
||||
|
||||
List<Person> personWithOutNickname = doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
return entityManager.createQuery(
|
||||
"select p from Person p where p.nicknames is empty", Person.class )
|
||||
.getResultList();
|
||||
});
|
||||
|
||||
assertEquals( Collections.singleton(personWithoutNicknameId),
|
||||
personWithOutNickname.stream().map( Person::getId ).collect( Collectors.toSet() ));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void afterEntityManagerFactoryBuilt() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
Person personaWithoutNickname = new Person();
|
||||
personaWithoutNickname.setId(personWithoutNicknameId);
|
||||
|
||||
Person personaWithSingleNickname = new Person();
|
||||
personaWithSingleNickname.getNicknames().add( "nickname" );
|
||||
personaWithSingleNickname.setId(personaWithSingleNicknameId);
|
||||
|
||||
Person personWithMultipleNicknames = new Person();
|
||||
personWithMultipleNicknames.getNicknames().addAll( Arrays.asList( "nickName1", "nickName2" ) );
|
||||
personWithMultipleNicknames.setId(personWithMultipleNicknamesId);
|
||||
|
||||
entityManager.persist( personaWithoutNickname );
|
||||
entityManager.persist( personaWithSingleNickname );
|
||||
entityManager.persist( personWithMultipleNicknames );
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity(name = "Person")
|
||||
public static class Person {
|
||||
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
@ElementCollection
|
||||
private List<String> nicknames = new ArrayList<>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public List<String> getNicknames() {
|
||||
return nicknames;
|
||||
}
|
||||
|
||||
public void setNicknames(List<String> nicknames) {
|
||||
this.nicknames = nicknames;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,86 +0,0 @@
|
||||
package org.hibernate.query;
|
||||
|
||||
import java.util.Map;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.ManyToOne;
|
||||
|
||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
||||
|
||||
import org.hibernate.testing.TestForIssue;
|
||||
import org.hibernate.testing.jdbc.SQLStatementInterceptor;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.hibernate.testing.transaction.TransactionUtil.doInJPA;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Christian Beikov
|
||||
* @author Nathan Xu
|
||||
*/
|
||||
@TestForIssue( jiraKey = "HHH-14201" )
|
||||
public class JoinOrderTest extends BaseEntityManagerFunctionalTestCase {
|
||||
|
||||
private SQLStatementInterceptor sqlStatementInterceptor;
|
||||
|
||||
@Override
|
||||
protected void addConfigOptions(Map options) {
|
||||
sqlStatementInterceptor = new SQLStatementInterceptor( options );
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<?>[] getAnnotatedClasses() {
|
||||
return new Class<?>[] {
|
||||
EntityA.class,
|
||||
EntityB.class,
|
||||
EntityC.class
|
||||
};
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJoinOrder() {
|
||||
doInJPA( this::entityManagerFactory, entityManager -> {
|
||||
|
||||
sqlStatementInterceptor.clear();
|
||||
|
||||
final String hql =
|
||||
"SELECT 1 " +
|
||||
"FROM EntityA a " +
|
||||
"JOIN EntityB b ON b.a = a " +
|
||||
"JOIN a.c c ON c.b = b";
|
||||
entityManager.createQuery( hql ).getResultList();
|
||||
|
||||
sqlStatementInterceptor.assertExecutedCount( 1 );
|
||||
final String sql = sqlStatementInterceptor.getSqlQueries().getFirst();
|
||||
|
||||
assertTrue( sql.matches( "^.+(?: join EntityB ).+(?: join EntityC ).+$" ) );
|
||||
} );
|
||||
}
|
||||
|
||||
@Entity(name = "EntityA")
|
||||
public static class EntityA {
|
||||
@Id
|
||||
int id;
|
||||
|
||||
@ManyToOne
|
||||
EntityC c;
|
||||
}
|
||||
|
||||
@Entity(name = "EntityB")
|
||||
public static class EntityB {
|
||||
@Id
|
||||
int id;
|
||||
|
||||
@ManyToOne
|
||||
EntityA a;
|
||||
}
|
||||
|
||||
@Entity(name = "EntityC")
|
||||
public static class EntityC {
|
||||
@Id
|
||||
int id;
|
||||
|
||||
@ManyToOne
|
||||
EntityB b;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user