HHH-14503 - Migrate tests from jpa/test to orm/test/jpa

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2021-06-01 22:52:21 +02:00 committed by Christian Beikov
parent d11abccefe
commit 2dc07c2ffe
5 changed files with 438 additions and 410 deletions

View File

@ -1,257 +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.jpa.test.query;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.FlushMode;
import org.hibernate.annotations.FlushModeType;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.NamedQuery;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
import org.hibernate.testing.transaction.TransactionUtil;
import org.junit.Assert;
import org.junit.Test;
/**
* @author Yoann Rodiere
*/
@TestForIssue(jiraKey = "HHH-12795")
public class NamedQueryFlushModeTest extends BaseCoreFunctionalTestCase {
@Override
public Class[] getAnnotatedClasses() {
return new Class[] { TestEntity.class };
}
@Test
public void testNamedQueryWithFlushModeManual() {
String queryName = "NamedQueryFlushModeManual";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Query<?> query = s.getNamedQuery( queryName );
Assert.assertEquals( FlushMode.MANUAL, query.getHibernateFlushMode() );
// JPA flush mode is an approximation
Assert.assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
} );
}
@Test
public void testNamedQueryWithFlushModeCommit() {
String queryName = "NamedQueryFlushModeCommit";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Query<?> query = s.getNamedQuery( queryName );
Assert.assertEquals( FlushMode.COMMIT, query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
} );
}
@Test
public void testNamedQueryWithFlushModeAuto() {
String queryName = "NamedQueryFlushModeAuto";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Query<?> query = s.getNamedQuery( queryName );
Assert.assertEquals( FlushMode.AUTO, query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
} );
}
@Test
public void testNamedQueryWithFlushModeAlways() {
String queryName = "NamedQueryFlushModeAlways";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Query<?> query = s.getNamedQuery( queryName );
Assert.assertEquals( FlushMode.ALWAYS, query.getHibernateFlushMode() );
// JPA flush mode is an approximation
Assert.assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
} );
}
@Test
public void testNamedQueryWithFlushModePersistenceContext() {
String queryName = "NamedQueryFlushModePersistenceContext";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
Query<?> query;
// A null Hibernate flush mode means we will use whatever mode is set on the session
// JPA doesn't allow null flush modes, so we expect some approximation of the flush mode to be returned
s.setHibernateFlushMode( FlushMode.MANUAL );
query = s.getNamedQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.COMMIT );
query = s.getNamedQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.AUTO );
query = s.getNamedQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.ALWAYS );
query = s.getNamedQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
} );
}
@Test
public void testNamedNativeQueryWithFlushModeManual() {
String queryName = "NamedNativeQueryFlushModeManual";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
Assert.assertEquals( FlushMode.MANUAL, query.getHibernateFlushMode() );
} );
}
@Test
public void testNamedNativeQueryWithFlushModeCommit() {
String queryName = "NamedNativeQueryFlushModeCommit";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
Assert.assertEquals( FlushMode.COMMIT, query.getHibernateFlushMode() );
} );
}
@Test
public void testNamedNativeQueryWithFlushModeAuto() {
String queryName = "NamedNativeQueryFlushModeAuto";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
Assert.assertEquals( FlushMode.AUTO, query.getHibernateFlushMode() );
} );
}
@Test
public void testNamedNativeQueryWithFlushModeAlways() {
String queryName = "NamedNativeQueryFlushModeAlways";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
Assert.assertEquals( FlushMode.ALWAYS, query.getHibernateFlushMode() );
} );
}
@Test
public void testNamedNativeQueryWithFlushModePersistenceContext() {
String queryName = "NamedNativeQueryFlushModePersistenceContext";
TransactionUtil.doInHibernate( this::sessionFactory, s -> {
NativeQuery<?> query;
// A null Hibernate flush mode means we will use whatever mode is set on the session
// JPA doesn't allow null flush modes, so we expect some approximation of the flush mode to be returned
s.setHibernateFlushMode( FlushMode.MANUAL );
query = s.getNamedNativeQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.COMMIT );
query = s.getNamedNativeQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.AUTO );
query = s.getNamedNativeQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.ALWAYS );
query = s.getNamedNativeQuery( queryName );
Assert.assertNull( query.getHibernateFlushMode() );
Assert.assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
} );
}
@Entity(name = "TestEntity")
@NamedQuery(
name = "NamedQueryFlushModeManual",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.MANUAL
)
@NamedQuery(
name = "NamedQueryFlushModeCommit",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.COMMIT
)
@NamedQuery(
name = "NamedQueryFlushModeAuto",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.AUTO
)
@NamedQuery(
name = "NamedQueryFlushModeAlways",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.ALWAYS
)
@NamedQuery(
name = "NamedQueryFlushModePersistenceContext",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.PERSISTENCE_CONTEXT
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeManual",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.MANUAL
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeCommit",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.COMMIT
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeAuto",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.AUTO
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeAlways",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.ALWAYS
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModePersistenceContext",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.PERSISTENCE_CONTEXT
)
public static class TestEntity {
@Id
@GeneratedValue
private Long id;
private String text;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
}

View File

@ -1,95 +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.jpa.test.query;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.testing.TestForIssue;
import org.junit.Test;
/**
* Based on the test developed by Hans Desmet to reproduce the bug reported in HHH-9230
*
* @author Steve Ebersole
*/
@TestForIssue(jiraKey = "HHH-9230")
public class QueryWithInParamListAndNamedEntityGraphTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] {Person.class};
}
@Test
public void testInClause() {
// this test works
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Set<Long> ids = new HashSet<Long>();
ids.add( 1L );
ids.add( 2L );
TypedQuery<Person> query = em.createQuery( "select p from Person p where p.id in :ids", Person.class );
query.setParameter( "ids", ids );
query.getResultList();
em.getTransaction().commit();
em.close();
}
@Test
public void testEntityGraph() {
// this test works
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
TypedQuery<Person> query = em.createQuery( "select p from Person p", Person.class );
query.setHint( "javax.persistence.loadgraph", em.createEntityGraph( "withBoss" ) );
query.getResultList();
em.getTransaction().commit();
em.close();
}
@Test
public void testEntityGraphAndInClause() {
// this test fails
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Set<Long> ids = new HashSet<Long>();
ids.add( 1L );
ids.add( 2L );
TypedQuery<Person> query = em.createQuery( "select p from Person p where p.id in :ids", Person.class );
query.setHint( "javax.persistence.loadgraph", em.createEntityGraph( "withBoss" ) );
query.setParameter( "ids", ids );
query.getResultList();
em.getTransaction().commit();
em.close();
}
@Entity(name = "Person")
@Table(name = "Person")
@NamedEntityGraph(name = "withBoss", attributeNodes = @NamedAttributeNode("boss"))
public static class Person {
@Id
@GeneratedValue
private long id;
private String name;
@ManyToOne
@JoinColumn
private Person boss;
}
}

View File

@ -0,0 +1,289 @@
/*
* 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.jpa.query;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import org.hibernate.FlushMode;
import org.hibernate.Session;
import org.hibernate.annotations.FlushModeType;
import org.hibernate.annotations.NamedNativeQuery;
import org.hibernate.annotations.NamedQuery;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
/**
* @author Yoann Rodiere
*/
@TestForIssue(jiraKey = "HHH-12795")
@Jpa(annotatedClasses = {
NamedQueryFlushModeTest.TestEntity.class
})
public class NamedQueryFlushModeTest {
@Test
public void testNamedQueryWithFlushModeManual(EntityManagerFactoryScope scope) {
String queryName = "NamedQueryFlushModeManual";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
Query<?> query = s.getNamedQuery( queryName );
assertEquals( FlushMode.MANUAL, query.getHibernateFlushMode() );
// JPA flush mode is an approximation
assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
}
);
}
@Test
public void testNamedQueryWithFlushModeCommit(EntityManagerFactoryScope scope) {
String queryName = "NamedQueryFlushModeCommit";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
Query<?> query = s.getNamedQuery( queryName );
assertEquals( FlushMode.COMMIT, query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
}
);
}
@Test
public void testNamedQueryWithFlushModeAuto(EntityManagerFactoryScope scope) {
String queryName = "NamedQueryFlushModeAuto";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
Query<?> query = s.getNamedQuery( queryName );
assertEquals( FlushMode.AUTO, query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
}
);
}
@Test
public void testNamedQueryWithFlushModeAlways(EntityManagerFactoryScope scope) {
String queryName = "NamedQueryFlushModeAlways";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
Query<?> query = s.getNamedQuery( queryName );
assertEquals( FlushMode.ALWAYS, query.getHibernateFlushMode() );
// JPA flush mode is an approximation
assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
}
);
}
@Test
public void testNamedQueryWithFlushModePersistenceContext(EntityManagerFactoryScope scope) {
String queryName = "NamedQueryFlushModePersistenceContext";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
Query<?> query;
// A null Hibernate flush mode means we will use whatever mode is set on the session
// JPA doesn't allow null flush modes, so we expect some approximation of the flush mode to be returned
s.setHibernateFlushMode( FlushMode.MANUAL );
query = s.getNamedQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.COMMIT );
query = s.getNamedQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.AUTO );
query = s.getNamedQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.ALWAYS );
query = s.getNamedQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
}
);
}
@Test
public void testNamedNativeQueryWithFlushModeManual(EntityManagerFactoryScope scope) {
String queryName = "NamedNativeQueryFlushModeManual";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
assertEquals( FlushMode.MANUAL, query.getHibernateFlushMode() );
}
);
}
@Test
public void testNamedNativeQueryWithFlushModeCommit(EntityManagerFactoryScope scope) {
String queryName = "NamedNativeQueryFlushModeCommit";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
assertEquals( FlushMode.COMMIT, query.getHibernateFlushMode() );
}
);
}
@Test
public void testNamedNativeQueryWithFlushModeAuto(EntityManagerFactoryScope scope) {
String queryName = "NamedNativeQueryFlushModeAuto";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
assertEquals( FlushMode.AUTO, query.getHibernateFlushMode() );
}
);
}
@Test
public void testNamedNativeQueryWithFlushModeAlways(EntityManagerFactoryScope scope) {
String queryName = "NamedNativeQueryFlushModeAlways";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
NativeQuery<?> query = s.getNamedNativeQuery( queryName );
assertEquals( FlushMode.ALWAYS, query.getHibernateFlushMode() );
}
);
}
@Test
public void testNamedNativeQueryWithFlushModePersistenceContext(EntityManagerFactoryScope scope) {
String queryName = "NamedNativeQueryFlushModePersistenceContext";
scope.inEntityManager(
entityManager -> {
Session s = entityManager.unwrap( Session.class );
NativeQuery<?> query;
// A null Hibernate flush mode means we will use whatever mode is set on the session
// JPA doesn't allow null flush modes, so we expect some approximation of the flush mode to be returned
s.setHibernateFlushMode( FlushMode.MANUAL );
query = s.getNamedNativeQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.COMMIT );
query = s.getNamedNativeQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.COMMIT, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.AUTO );
query = s.getNamedNativeQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
s.setHibernateFlushMode( FlushMode.ALWAYS );
query = s.getNamedNativeQuery( queryName );
assertNull( query.getHibernateFlushMode() );
assertEquals( javax.persistence.FlushModeType.AUTO, query.getFlushMode() );
}
);
}
@Entity(name = "TestEntity")
@NamedQuery(
name = "NamedQueryFlushModeManual",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.MANUAL
)
@NamedQuery(
name = "NamedQueryFlushModeCommit",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.COMMIT
)
@NamedQuery(
name = "NamedQueryFlushModeAuto",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.AUTO
)
@NamedQuery(
name = "NamedQueryFlushModeAlways",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.ALWAYS
)
@NamedQuery(
name = "NamedQueryFlushModePersistenceContext",
query = "select e from TestEntity e where e.text = :text",
flushMode = FlushModeType.PERSISTENCE_CONTEXT
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeManual",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.MANUAL
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeCommit",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.COMMIT
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeAuto",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.AUTO
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModeAlways",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.ALWAYS
)
@NamedNativeQuery(
name = "NamedNativeQueryFlushModePersistenceContext",
query = "select * from TestEntity e where e.text = :text",
resultClass = TestEntity.class,
flushMode = FlushModeType.PERSISTENCE_CONTEXT
)
public static class TestEntity {
@Id
@GeneratedValue
private Long id;
private String text;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
}

View File

@ -0,0 +1,93 @@
/*
* 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.jpa.query;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedAttributeNode;
import javax.persistence.NamedEntityGraph;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import org.junit.jupiter.api.Test;
/**
* Based on the test developed by Hans Desmet to reproduce the bug reported in HHH-9230
*
* @author Steve Ebersole
*/
@TestForIssue(jiraKey = "HHH-9230")
@Jpa(annotatedClasses = {
QueryWithInParamListAndNamedEntityGraphTest.Person.class
})
public class QueryWithInParamListAndNamedEntityGraphTest {
@Test
public void testInClause(EntityManagerFactoryScope scope) {
// this test works
scope.inTransaction(
entityManager -> {
Set<Long> ids = new HashSet<>();
ids.add( 1L );
ids.add( 2L );
TypedQuery<Person> query = entityManager.createQuery( "select p from Person p where p.id in :ids", Person.class );
query.setParameter( "ids", ids );
query.getResultList();
}
);
}
@Test
public void testEntityGraph(EntityManagerFactoryScope scope) {
// this test works
scope.inTransaction(
entityManager -> {
TypedQuery<Person> query = entityManager.createQuery( "select p from Person p", Person.class );
query.setHint( "javax.persistence.loadgraph", entityManager.createEntityGraph( "withBoss" ) );
query.getResultList();
}
);
}
@Test
public void testEntityGraphAndInClause(EntityManagerFactoryScope scope) {
// this test fails
scope.inTransaction(
entityManager -> {
Set<Long> ids = new HashSet<>();
ids.add( 1L );
ids.add( 2L );
TypedQuery<Person> query = entityManager.createQuery( "select p from Person p where p.id in :ids", Person.class );
query.setHint( "javax.persistence.loadgraph", entityManager.createEntityGraph( "withBoss" ) );
query.setParameter( "ids", ids );
query.getResultList();
}
);
}
@Entity(name = "Person")
@Table(name = "Person")
@NamedEntityGraph(name = "withBoss", attributeNodes = @NamedAttributeNode("boss"))
public static class Person {
@Id
@GeneratedValue
private long id;
private String name;
@ManyToOne
@JoinColumn
private Person boss;
}
}

View File

@ -4,7 +4,7 @@
* 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.jpa.test.query;
package org.hibernate.orm.test.jpa.query;
import java.io.Serializable;
import java.sql.PreparedStatement;
@ -15,7 +15,6 @@ import java.util.Arrays;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.Id;
import javax.persistence.Query;
import javax.persistence.Table;
@ -24,87 +23,86 @@ import javax.persistence.TypedQuery;
import org.hibernate.HibernateException;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
import org.hibernate.type.CustomType;
import org.hibernate.usertype.UserType;
import org.junit.Before;
import org.junit.Test;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
import org.hibernate.testing.orm.junit.Jpa;
import static org.junit.Assert.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* @author Steve Ebersole
*/
public class TypedValueParametersTest extends BaseEntityManagerFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class[] { Document.class };
@Jpa(annotatedClasses = {
TypedValueParametersTest.Document.class
})
public class TypedValueParametersTest {
@BeforeEach
public void init(EntityManagerFactoryScope scope) throws Exception {
scope.inTransaction(
entityManager -> {
Document a = new Document();
a.getTags().add("important");
a.getTags().add("business");
entityManager.persist(a);
}
);
}
private int docId;
@Before
public void init() throws Exception {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
Document a = new Document();
a.getTags().add("important");
a.getTags().add("business");
em.persist(a);
docId = a.getId();
em.getTransaction().commit();
em.close();
@AfterEach
public void tearDown(EntityManagerFactoryScope scope) {
scope.inTransaction(
entityManager -> entityManager.createQuery( "delete from Document" ).executeUpdate()
);
}
@Test
public void testNative() {
test(new Binder() {
public void testNative(EntityManagerFactoryScope scope) {
test(scope,
q -> {
final CustomType customType = new CustomType(
TagUserType.INSTANCE,
scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getTypeConfiguration()
);
public void bind(Query q) {
final CustomType customType = new CustomType(
TagUserType.INSTANCE,
entityManagerFactory().getTypeConfiguration()
);
org.hibernate.query.Query hibernateQuery = q.unwrap(org.hibernate.query.Query.class);
hibernateQuery.setParameter( "tags", Arrays.asList("important","business"), customType );
}
});
org.hibernate.query.Query hibernateQuery = q.unwrap( org.hibernate.query.Query.class );
hibernateQuery.setParameter( "tags", Arrays.asList( "important", "business" ), customType );
}
);
}
@Test
public void testJpa() {
test(new Binder() {
public void bind(Query q) {
public void testJpa(EntityManagerFactoryScope scope) {
test(scope,
q -> {
final CustomType customType = new CustomType(
TagUserType.INSTANCE,
entityManagerFactory().getTypeConfiguration()
scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getTypeConfiguration()
);
q.setParameter("tags", new TypedParameterValue( customType, Arrays.asList("important","business")));
}
});
);
}
private void test(Binder b) {
EntityManager em = getOrCreateEntityManager();
em.getTransaction().begin();
private void test(EntityManagerFactoryScope scope, Binder b) {
scope.inTransaction(
entityManager -> {
TypedQuery<Long> q = entityManager.createQuery( "select count(*) from Document d where d.tags = :tags", Long.class );
b.bind( q );
TypedQuery<Long> q = em.createQuery( "select count(*) from Document d where d.tags = :tags", Long.class );
b.bind( q );
Long count = q.getSingleResult();
em.getTransaction().commit();
em.close();
assertEquals( 1, count.intValue() );
Long count = q.getSingleResult();
assertEquals( 1, count.intValue() );
}
);
}
private interface Binder {
@ -121,7 +119,7 @@ public class TypedValueParametersTest extends BaseEntityManagerFunctionalTestCas
@Type(type = "tagList")
@Column(name = "tags")
private List<String> tags = new ArrayList<String>();
private List<String> tags = new ArrayList<>();
public int getId() {
return id;
@ -207,7 +205,7 @@ public class TypedValueParametersTest extends BaseEntityManagerFunctionalTestCas
@Override
@SuppressWarnings("unchecked")
public Object deepCopy(final Object o) throws HibernateException {
return o == null ? null : new ArrayList<String>((List<String>) o);
return o == null ? null : new ArrayList<>((List<String>) o);
}
@Override