From 7695f03bf991105109e39e516d804964c571bead Mon Sep 17 00:00:00 2001 From: Jan Schatteman Date: Wed, 10 Mar 2021 22:36:17 +0100 Subject: [PATCH] HHH-14503 - Migration of tests from jpa/test to orm/test/jpa Signed-off-by: Jan Schatteman --- .../hibernate/jpa/test/ops/ContainsTest.java | 62 ---- .../org/hibernate/jpa/test/ops/FindTest.java | 63 ---- .../test/ops/GetLoadJpaComplianceTest.java | 156 --------- .../hibernate/jpa/test/ops/GetLoadTest.java | 285 ---------------- .../hibernate/jpa/test/ops/MergeNewTest.java | 68 ---- .../org/hibernate/jpa/test/ops/MergeTest.java | 119 ------- .../hibernate/jpa/test/ops/PersistTest.java | 235 -------------- .../jpa/test/ops/RemoveOrderingTest.java | 104 ------ .../test/jpa/association/AssociationTest.java | 4 +- .../test => orm/test/jpa}/ops/Animal.java | 2 +- .../orm/test/jpa/ops/ContainsTest.java | 69 ++++ .../test => orm/test/jpa}/ops/Employee.java | 2 +- .../test => orm/test/jpa}/ops/Employer.java | 2 +- .../hibernate/orm/test/jpa/ops/FindTest.java | 65 ++++ ...oadJpaComplianceDifferentSessionsTest.java | 48 +-- .../jpa/ops/GetLoadJpaComplianceTest.java | 147 +++++++++ .../orm/test/jpa/ops/GetLoadTest.java | 303 ++++++++++++++++++ .../test => orm/test/jpa}/ops/Mammal.java | 2 +- .../orm/test/jpa/ops/MergeNewTest.java | 86 +++++ .../hibernate/orm/test/jpa/ops/MergeTest.java | 129 ++++++++ .../{jpa/test => orm/test/jpa}/ops/Node.java | 2 +- .../test/jpa}/ops/NumberedNode.java | 2 +- .../orm/test/jpa/ops/PersistTest.java | 285 ++++++++++++++++ .../orm/test/jpa/ops/RemoveOrderingTest.java | 93 ++++++ .../test => orm/test/jpa}/ops/Reptile.java | 2 +- .../test => orm/test/jpa}/ops/Workload.java | 2 +- .../test/jpa}/ops/Employer.hbm.xml | 2 +- .../test => orm/test/jpa}/ops/Node.hbm.xml | 2 +- ...tityManagerFactoryBasedFunctionalTest.java | 4 +- .../junit5/EntityManagerFactoryScope.java | 13 +- 30 files changed, 1230 insertions(+), 1128 deletions(-) delete mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/ContainsTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/FindTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceTest.java delete mode 100755 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeNewTest.java delete mode 100755 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeTest.java delete mode 100755 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/PersistTest.java delete mode 100644 hibernate-core/src/test/java/org/hibernate/jpa/test/ops/RemoveOrderingTest.java rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Animal.java (94%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/ContainsTest.java rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Employee.java (94%) rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Employer.java (94%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/GetLoadJpaComplianceDifferentSessionsTest.java (57%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceTest.java create mode 100755 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadTest.java rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Mammal.java (92%) create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeNewTest.java create mode 100755 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeTest.java rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Node.java (96%) rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/NumberedNode.java (92%) create mode 100755 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/PersistTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/RemoveOrderingTest.java rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Reptile.java (93%) rename hibernate-core/src/test/java/org/hibernate/{jpa/test => orm/test/jpa}/ops/Workload.java (94%) rename hibernate-core/src/test/resources/org/hibernate/{jpa/test => orm/test/jpa}/ops/Employer.hbm.xml (95%) rename hibernate-core/src/test/resources/org/hibernate/{jpa/test => orm/test/jpa}/ops/Node.hbm.xml (95%) diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/ContainsTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/ContainsTest.java deleted file mode 100644 index bfb82e230bd9..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/ContainsTest.java +++ /dev/null @@ -1,62 +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 . - */ -package org.hibernate.jpa.test.ops; - -import javax.persistence.Entity; -import javax.persistence.Id; - -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.junit.Test; - -import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -/** - * @author Vlad Mihalcea - */ -public class ContainsTest extends BaseEntityManagerFunctionalTestCase { - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { - Person.class, - }; - } - - @Test - public void testLifecycle() { - Person _person = doInJPA( this::entityManagerFactory, entityManager -> { - Person person = new Person(); - person.id = 1L; - person.name = "John Doe"; - entityManager.persist( person ); - - assertTrue(entityManager.contains( person )); - - return person; - } ); - doInJPA( this::entityManagerFactory, entityManager -> { - assertFalse(entityManager.contains( _person )); - - Person person = entityManager.find( Person.class, 1L ); - - assertTrue(entityManager.contains( person )); - } ); - } - - @Entity(name = "PersonEntity") - public static class Person { - - @Id - private Long id; - - private String name; - } - -} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/FindTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/FindTest.java deleted file mode 100644 index 6239d65392a1..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/FindTest.java +++ /dev/null @@ -1,63 +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 . - */ -package org.hibernate.jpa.test.ops; - -import javax.persistence.EntityManager; - -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.testing.TestForIssue; -import org.junit.Assert; -import org.junit.Test; - -import static org.junit.Assert.fail; - -/** - * @author Emmanuel Bernard - */ -public class FindTest extends BaseEntityManagerFunctionalTestCase { - @Test - public void testSubclassWrongId() throws Exception { - Mammal mammal = new Mammal(); - mammal.setMamalNbr( 2 ); - mammal.setName( "Human" ); - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.persist( mammal ); - em.flush(); - Assert.assertNull( em.find( Reptile.class, 1l ) ); - em.getTransaction().rollback(); - em.close(); - } - - @Test - @TestForIssue( jiraKey = "HHH-9856" ) - public void testNonEntity() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - try { - em.find( String.class, 1 ); - fail( "Expecting a failure" ); - } - catch (IllegalArgumentException ignore) { - // expected - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - Mammal.class, - Reptile.class, - Animal.class - }; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceTest.java deleted file mode 100644 index 370e9be1f9ea..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceTest.java +++ /dev/null @@ -1,156 +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 . - */ -package org.hibernate.jpa.test.ops; - -import java.util.Map; -import javax.persistence.EntityManager; -import javax.persistence.EntityNotFoundException; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.cfg.AvailableSettings; -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.AbstractHANADialect; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * @author Gavin King - * @author Hardy Ferentschik - */ -@RequiresDialectFeature(DialectChecks.SupportsNoColumnInsert.class) -public class GetLoadJpaComplianceTest extends BaseEntityManagerFunctionalTestCase { - - @Override - @SuppressWarnings( {"unchecked"}) - protected void addConfigOptions(Map options) { - options.put( AvailableSettings.JPA_PROXY_COMPLIANCE, true ); - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testLoadIdNotFound_FieldBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - Session s = (Session) em.getDelegate(); - - assertNull( s.get( Workload.class, 999 ) ); - - Workload proxy = s.load( Workload.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - fail( "Should have failed because there is no Employee Entity with id == 999" ); - } - catch (EntityNotFoundException ex) { - // expected - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testReferenceIdNotFound_FieldBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - - assertNull( em.find( Workload.class, 999 ) ); - - Workload proxy = em.getReference( Workload.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - fail( "Should have failed because there is no Workload Entity with id == 999" ); - } - catch (EntityNotFoundException ex) { - // expected - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testLoadIdNotFound_PropertyBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - Session s = (Session) em.getDelegate(); - - assertNull( s.get( Employee.class, 999 ) ); - - Employee proxy = s.load( Employee.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - fail( "Should have failed because there is no Employee Entity with id == 999" ); - } - catch (EntityNotFoundException ex) { - // expected - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testReferenceIdNotFound_PropertyBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - - assertNull( em.find( Employee.class, 999 ) ); - - Employee proxy = em.getReference( Employee.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - fail( "Should have failed because there is no Employee Entity with id == 999" ); - } - catch (EntityNotFoundException ex) { - // expected - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Override - protected String[] getMappings() { - return new String[] { - "org/hibernate/jpa/test/ops/Node.hbm.xml", - "org/hibernate/jpa/test/ops/Employer.hbm.xml" - }; - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Workload.class }; - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadTest.java deleted file mode 100755 index e67fc573e67a..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadTest.java +++ /dev/null @@ -1,285 +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 . - */ -package org.hibernate.jpa.test.ops; - -import java.util.Map; -import javax.persistence.EntityManager; -import javax.persistence.EntityNotFoundException; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.AbstractHANADialect; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.SkipForDialect; -import org.hibernate.testing.TestForIssue; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -/** - * @author Gavin King - * @author Hardy Ferentschik - */ -@RequiresDialectFeature(DialectChecks.SupportsNoColumnInsert.class) -public class GetLoadTest extends BaseEntityManagerFunctionalTestCase { - @Test - @SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column") - public void testGet() { - clearCounts(); - - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Session s = ( Session ) em.getDelegate(); - - Employer emp = new Employer(); - s.persist( emp ); - Node node = new Node( "foo" ); - Node parent = new Node( "bar" ); - parent.addChild( node ); - s.persist( parent ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - s = ( Session ) em.getDelegate(); - emp = ( Employer ) s.get( Employer.class, emp.getId() ); - assertTrue( Hibernate.isInitialized( emp ) ); - assertFalse( Hibernate.isInitialized( emp.getEmployees() ) ); - node = ( Node ) s.get( Node.class, node.getName() ); - assertTrue( Hibernate.isInitialized( node ) ); - assertFalse( Hibernate.isInitialized( node.getChildren() ) ); - assertFalse( Hibernate.isInitialized( node.getParent() ) ); - assertNull( s.get( Node.class, "xyz" ) ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - s = ( Session ) em.getDelegate(); - emp = ( Employer ) s.get( Employer.class.getName(), emp.getId() ); - assertTrue( Hibernate.isInitialized( emp ) ); - node = ( Node ) s.get( Node.class.getName(), node.getName() ); - assertTrue( Hibernate.isInitialized( node ) ); - em.getTransaction().commit(); - em.close(); - - assertFetchCount( 0 ); - } - - @Test - @SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column") - public void testLoad() { - clearCounts(); - - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Session s = ( Session ) em.getDelegate(); - - Employer emp = new Employer(); - s.persist( emp ); - Node node = new Node( "foo" ); - Node parent = new Node( "bar" ); - parent.addChild( node ); - s.persist( parent ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - s = ( Session ) em.getDelegate(); - emp = ( Employer ) s.load( Employer.class, emp.getId() ); - emp.getId(); - assertFalse( Hibernate.isInitialized( emp ) ); - node = ( Node ) s.load( Node.class, node.getName() ); - assertEquals( node.getName(), "foo" ); - assertFalse( Hibernate.isInitialized( node ) ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - s = ( Session ) em.getDelegate(); - emp = ( Employer ) s.load( Employer.class.getName(), emp.getId() ); - emp.getId(); - assertFalse( Hibernate.isInitialized( emp ) ); - node = ( Node ) s.load( Node.class.getName(), node.getName() ); - assertEquals( node.getName(), "foo" ); - assertFalse( Hibernate.isInitialized( node ) ); - em.getTransaction().commit(); - em.close(); - - assertFetchCount( 0 ); - } - - private void clearCounts() { - entityManagerFactory().getStatistics().clear(); - } - - private void assertFetchCount(int count) { - int fetches = ( int ) entityManagerFactory().getStatistics().getEntityFetchCount(); - assertEquals( count, fetches ); - } - - @Test - @TestForIssue( jiraKey = "HHH-9856" ) - public void testNonEntity() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - try { - em.getReference( String.class, 1 ); - fail( "Expecting a failure" ); - } - catch (IllegalArgumentException ignore) { - // expected - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-11838") - public void testLoadGetId() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Session s = ( Session ) em.getDelegate(); - Workload workload = new Workload(); - s.persist(workload); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - s = ( Session ) em.getDelegate(); - - Workload proxy = s.load(Workload.class, workload.id); - proxy.getId(); - - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getName(); - - assertTrue( Hibernate.isInitialized( proxy ) ); - - em.getTransaction().commit(); - em.close(); - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testLoadIdNotFound_FieldBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - Session s = (Session) em.getDelegate(); - - assertNull( s.get( Workload.class, 999 ) ); - - Workload proxy = s.load( Workload.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testReferenceIdNotFound_FieldBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - - assertNull( em.find( Workload.class, 999 ) ); - - Workload proxy = em.getReference( Workload.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testLoadIdNotFound_PropertyBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - Session s = (Session) em.getDelegate(); - - assertNull( s.get( Employee.class, 999 ) ); - - Employee proxy = s.load( Employee.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Test - @TestForIssue( jiraKey = "HHH-12034") - public void testReferenceIdNotFound_PropertyBasedAccess() { - EntityManager em = getOrCreateEntityManager(); - try { - em.getTransaction().begin(); - - assertNull( em.find( Employee.class, 999 ) ); - - Employee proxy = em.getReference( Employee.class, 999 ); - assertFalse( Hibernate.isInitialized( proxy ) ); - - proxy.getId(); - } - finally { - em.getTransaction().rollback(); - em.close(); - } - } - - @Override - @SuppressWarnings( {"unchecked"}) - protected void addConfigOptions(Map options) { - options.put( Environment.GENERATE_STATISTICS, "true" ); - options.put( Environment.STATEMENT_BATCH_SIZE, "0" ); - } - - @Override - protected String[] getMappings() { - return new String[] { - "org/hibernate/jpa/test/ops/Node.hbm.xml", - "org/hibernate/jpa/test/ops/Employer.hbm.xml" - }; - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Workload.class }; - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeNewTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeNewTest.java deleted file mode 100644 index 1790b3a78492..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeNewTest.java +++ /dev/null @@ -1,68 +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 . - */ -package org.hibernate.jpa.test.ops; - -import javax.persistence.EntityManager; - -import org.junit.Test; - -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import static org.junit.Assert.assertNotNull; - -/** - * @author Emmanuel Bernard - */ -public class MergeNewTest extends BaseEntityManagerFunctionalTestCase { - @Test - public void testMergeNew() throws Exception { - Workload load = new Workload(); - load.name = "Cleaning"; - load.load = 10; - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - load = em.merge( load ); - assertNotNull( load.id ); - em.flush(); - assertNotNull( load.id ); - em.getTransaction().rollback(); - em.close(); - } - - @Test - public void testMergeAfterRemove() throws Exception { - Workload load = new Workload(); - load.name = "Cleaning"; - load.load = 10; - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - load = em.merge( load ); - em.flush(); - em.getTransaction().commit(); - em.close(); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - load = em.find( Workload.class, load.id ); - em.remove( load ); - em.flush(); - em.getTransaction().commit(); - em.close(); - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.merge( load ); - em.flush(); - em.getTransaction().commit(); - em.close(); - } - - @Override - public Class[] getAnnotatedClasses() { - return new Class[] { - Workload.class - }; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeTest.java deleted file mode 100755 index c3c7b0e718b2..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/MergeTest.java +++ /dev/null @@ -1,119 +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 . - */ -package org.hibernate.jpa.test.ops; - -import java.util.Map; -import javax.persistence.EntityManager; - -import org.hibernate.cfg.Environment; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.junit.Assert; -import org.junit.Test; - -/** - * @author Gavin King - * @author Hardy Ferentschik - */ -public class MergeTest extends BaseEntityManagerFunctionalTestCase { - @Test - public void testMergeTree() { - clearCounts(); - - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Node root = new Node( "root" ); - Node child = new Node( "child" ); - root.addChild( child ); - em.persist( root ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 2 ); - clearCounts(); - - root.setDescription( "The root node" ); - child.setDescription( "The child node" ); - - Node secondChild = new Node( "second child" ); - - root.addChild( secondChild ); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.merge( root ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 2 ); - } - - public void testMergeTreeWithGeneratedId() { - clearCounts(); - - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - NumberedNode root = new NumberedNode( "root" ); - NumberedNode child = new NumberedNode( "child" ); - root.addChild( child ); - em.persist( root ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 2 ); - clearCounts(); - - root.setDescription( "The root node" ); - child.setDescription( "The child node" ); - - NumberedNode secondChild = new NumberedNode( "second child" ); - - root.addChild( secondChild ); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.merge( root ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 1 ); - assertUpdateCount( 2 ); - } - - private void clearCounts() { - entityManagerFactory().getStatistics().clear(); - } - - private void assertInsertCount(int count) { - int inserts = ( int ) entityManagerFactory().getStatistics().getEntityInsertCount(); - Assert.assertEquals( count, inserts ); - } - - private void assertUpdateCount(int count) { - int updates = ( int ) entityManagerFactory().getStatistics().getEntityUpdateCount(); - Assert.assertEquals( count, updates ); - } - - @Override - @SuppressWarnings( {"unchecked"}) - protected void addConfigOptions(Map options) { - options.put( Environment.GENERATE_STATISTICS, "true" ); - options.put( Environment.STATEMENT_BATCH_SIZE, "0" ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[0]; - } - - @Override - protected String[] getMappings() { - return new String[] { "org/hibernate/jpa/test/ops/Node.hbm.xml" }; - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/PersistTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/PersistTest.java deleted file mode 100755 index 5d23d298fda6..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/PersistTest.java +++ /dev/null @@ -1,235 +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 . - */ - -// $Id$ - -package org.hibernate.jpa.test.ops; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import javax.persistence.EntityManager; -import javax.persistence.PersistenceException; -import javax.persistence.RollbackException; - -import org.hibernate.cfg.Environment; -import org.hibernate.dialect.AbstractHANADialect; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.hibernate.testing.DialectChecks; -import org.hibernate.testing.RequiresDialectFeature; -import org.hibernate.testing.SkipForDialect; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; - -/** - * @author Gavin King - * @author Hardy Ferentschik - */ -@RequiresDialectFeature(DialectChecks.SupportsNoColumnInsert.class) -public class PersistTest extends BaseEntityManagerFunctionalTestCase { - @Test - public void testCreateTree() { - - clearCounts(); - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Node root = new Node( "root" ); - Node child = new Node( "child" ); - root.addChild( child ); - em.persist( root ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 2 ); - assertUpdateCount( 0 ); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - root = em.find( Node.class, "root" ); - Node child2 = new Node( "child2" ); - root.addChild( child2 ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 3 ); - assertUpdateCount( 0 ); - } - - @Test - public void testCreateTreeWithGeneratedId() { - clearCounts(); - - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - NumberedNode root = new NumberedNode( "root" ); - NumberedNode child = new NumberedNode( "child" ); - root.addChild( child ); - em.persist( root ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 2 ); - assertUpdateCount( 0 ); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - root = em.find( NumberedNode.class, root.getId() ); - NumberedNode child2 = new NumberedNode( "child2" ); - root.addChild( child2 ); - em.getTransaction().commit(); - em.close(); - - assertInsertCount( 3 ); - assertUpdateCount( 0 ); - } - - @Test - public void testCreateException() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Node dupe = new Node( "dupe" ); - em.persist( dupe ); - em.persist( dupe ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.persist( dupe ); - try { - em.getTransaction().commit(); - fail( "Cannot persist() twice the same entity" ); - } - catch ( Exception cve ) { - //verify that an exception is thrown! - } - em.close(); - - Node nondupe = new Node( "nondupe" ); - nondupe.addChild( dupe ); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - em.persist( nondupe ); - try { - em.getTransaction().commit(); - assertFalse( true ); - } - catch ( RollbackException e ) { - //verify that an exception is thrown! - } - em.close(); - } - - @Test - public void testCreateExceptionWithGeneratedId() { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - NumberedNode dupe = new NumberedNode( "dupe" ); - em.persist( dupe ); - em.persist( dupe ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - try { - em.persist( dupe ); - fail(); - } - catch ( PersistenceException poe ) { - //verify that an exception is thrown! - } - em.getTransaction().rollback(); - em.close(); - - NumberedNode nondupe = new NumberedNode( "nondupe" ); - nondupe.addChild( dupe ); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - try { - em.persist( nondupe ); - fail(); - } - catch ( PersistenceException poe ) { - //verify that an exception is thrown! - } - em.getTransaction().rollback(); - em.close(); - } - - @Test - @SkipForDialect(value = AbstractHANADialect.class, comment = " HANA doesn't support tables consisting of only a single auto-generated column") - public void testBasic() throws Exception { - - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - Employer er = new Employer(); - Employee ee = new Employee(); - em.persist( ee ); - Collection erColl = new ArrayList(); - Collection eeColl = new ArrayList(); - erColl.add( ee ); - eeColl.add( er ); - er.setEmployees( erColl ); - ee.setEmployers( eeColl ); - em.getTransaction().commit(); - em.close(); - - em = getOrCreateEntityManager(); - em.getTransaction().begin(); - er = em.find( Employer.class, er.getId() ); - assertNotNull( er ); - assertNotNull( er.getEmployees() ); - assertEquals( 1, er.getEmployees().size() ); - Employee eeFromDb = ( Employee ) er.getEmployees().iterator().next(); - assertEquals( ee.getId(), eeFromDb.getId() ); - em.getTransaction().commit(); - em.close(); - } - - private void clearCounts() { - entityManagerFactory().getStatistics().clear(); - } - - private void assertInsertCount(int count) { - int inserts = ( int ) entityManagerFactory().getStatistics().getEntityInsertCount(); - assertEquals( count, inserts ); - } - - private void assertUpdateCount(int count) { - int updates = ( int ) entityManagerFactory().getStatistics().getEntityUpdateCount(); - assertEquals( count, updates ); - } - - @Override - @SuppressWarnings( {"unchecked"}) - protected void addConfigOptions(Map options) { - options.put( Environment.GENERATE_STATISTICS, "true" ); - options.put( Environment.STATEMENT_BATCH_SIZE, "0" ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Node.class }; - } - - @Override - protected String[] getMappings() { - return new String[] { - "org/hibernate/jpa/test/ops/Node.hbm.xml", - "org/hibernate/jpa/test/ops/Employer.hbm.xml" - }; - } -} - diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/RemoveOrderingTest.java b/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/RemoveOrderingTest.java deleted file mode 100644 index 3cbe72f63267..000000000000 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/RemoveOrderingTest.java +++ /dev/null @@ -1,104 +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 . - */ -package org.hibernate.jpa.test.ops; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.EntityManager; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.Table; -import java.util.Map; - -import org.hibernate.jpa.AvailableSettings; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; - -import org.junit.Test; - -import org.hibernate.testing.FailureExpected; -import org.hibernate.testing.TestForIssue; - -/** - * @author Steve Ebersole - */ -public class RemoveOrderingTest extends BaseEntityManagerFunctionalTestCase { - @Override - protected void addConfigOptions(Map options) { - super.addConfigOptions( options ); - options.put( AvailableSettings.VALIDATION_MODE, "NONE" ); - } - - @Override - protected Class[] getAnnotatedClasses() { - return new Class[] { Person.class, Company.class }; - } - - @Test - @TestForIssue( jiraKey = "HHH-8550" ) - @FailureExpected( jiraKey = "HHH-8550" ) - public void testManyToOne() throws Exception { - EntityManager em = getOrCreateEntityManager(); - em.getTransaction().begin(); - try { - Company company = new Company( 1, "acme" ); - Person person = new Person( 1, "joe", company ); - em.persist( person ); - em.flush(); - - em.remove( company ); - em.remove( person ); - em.flush(); - - em.persist( person ); - em.flush(); - - em.getTransaction().commit(); - } - catch (Exception e) { - em.getTransaction().rollback(); - throw e; - } - em.close(); - } - - @Entity( name="Company" ) - @Table( name = "COMPANY" ) - public static class Company { - @Id - public Integer id; - public String name; - - public Company() { - } - - public Company(Integer id, String name) { - this.id = id; - this.name = name; - } - } - - @Entity( name="Person" ) - @Table( name = "PERSON" ) - public static class Person { - @Id - public Integer id; - public String name; - @ManyToOne( cascade= CascadeType.ALL, optional = false ) - @JoinColumn( name = "EMPLOYER_FK" ) - public Company employer; - - public Person() { - } - - public Person(Integer id, String name, Company employer) { - this.id = id; - this.name = name; - this.employer = employer; - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/association/AssociationTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/association/AssociationTest.java index 49de951b2bc4..5d20e5f53b53 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/association/AssociationTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/association/AssociationTest.java @@ -46,7 +46,7 @@ public void testBidirOneToOne() { @Test public void testMergeAndBidirOneToOne() { - final Oven persistedOven = inTransaction( + final Oven persistedOven = fromTransaction( entityManager -> { Oven oven = new Oven(); Kitchen kitchen = new Kitchen(); @@ -57,7 +57,7 @@ public void testMergeAndBidirOneToOne() { return oven; } ); - Oven mergedOven = inTransaction( + Oven mergedOven = fromTransaction( entityManager -> { return entityManager.merge( persistedOven ); } diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Animal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Animal.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Animal.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Animal.java index 5b598334c7af..8793514aa501 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Animal.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Animal.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/ContainsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/ContainsTest.java new file mode 100644 index 000000000000..6b9a452a64d7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/ContainsTest.java @@ -0,0 +1,69 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.ops; + +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * @author Vlad Mihalcea + */ +@Jpa(annotatedClasses = { + ContainsTest.Person.class +}) +public class ContainsTest { + + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "delete from PersonEntity" ).executeUpdate(); + } + ); + } + + @Test + public void testLifecycle(EntityManagerFactoryScope scope) { + Person _person = scope.fromTransaction( + entityManager -> { + Person person = new Person(); + person.id = 1L; + person.name = "John Doe"; + entityManager.persist( person ); + + assertTrue( entityManager.contains( person ) ); + + return person; + } + ); + scope.inTransaction( + entityManager -> { + assertFalse(entityManager.contains( _person )); + Person person = entityManager.find( Person.class, 1L ); + assertTrue(entityManager.contains( person )); + } + ); + } + + @Entity(name = "PersonEntity") + public static class Person { + + @Id + private Long id; + + private String name; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Employee.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Employee.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Employee.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Employee.java index 2cc99c8fbf68..7356ce12ea4c 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Employee.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Employee.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import java.io.Serializable; import java.util.Collection; diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Employer.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Employer.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Employer.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Employer.java index b1caf2311724..a33439bc38ce 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Employer.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Employer.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import java.io.Serializable; import java.util.Collection; diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java new file mode 100644 index 000000000000..6d186add287e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/FindTest.java @@ -0,0 +1,65 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.ops; + +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 org.junit.jupiter.api.Assertions; + +/** + * @author Emmanuel Bernard + */ +@Jpa(annotatedClasses = { + Mammal.class, + Reptile.class, + Animal.class +}) +public class FindTest { + @Test + public void testSubclassWrongId(EntityManagerFactoryScope scope) { + Mammal mammal = new Mammal(); + mammal.setMamalNbr( 2 ); + mammal.setName( "Human" ); + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + entityManager.persist( mammal ); + entityManager.flush(); + Assertions.assertNull( entityManager.find( Reptile.class, 1l ) ); + entityManager.getTransaction().rollback(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-9856" ) + public void testNonEntity(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + entityManager.getTransaction().begin(); + try { + entityManager.find( String.class, 1 ); + Assertions.fail( "Expecting a failure" ); + } + catch (IllegalArgumentException ignore) { + // expected + } + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceDifferentSessionsTest.java similarity index 57% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceDifferentSessionsTest.java index 5b512f69d752..b30fa575d255 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/GetLoadJpaComplianceDifferentSessionsTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceDifferentSessionsTest.java @@ -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 . */ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import java.util.Map; import javax.persistence.EntityManagerFactory; @@ -12,37 +12,44 @@ import org.hibernate.Session; import org.hibernate.cfg.AvailableSettings; import org.hibernate.jpa.boot.spi.Bootstrap; -import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase; import org.hibernate.testing.TestForIssue; -import org.junit.Test; +import org.hibernate.testing.junit5.EntityManagerFactoryBasedFunctionalTest; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; import static org.hibernate.testing.transaction.TransactionUtil.doInJPA; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; /** * @author Vlad Mihalcea */ -@TestForIssue( jiraKey = "HHH-12273" ) -public class GetLoadJpaComplianceDifferentSessionsTest extends BaseEntityManagerFunctionalTestCase { +@TestForIssue(jiraKey = "HHH-12273") +@Jpa( + annotatedClasses = Workload.class, + integrationSettings = { @Setting(name = AvailableSettings.JPA_PROXY_COMPLIANCE, value = "false") } +) +public class GetLoadJpaComplianceDifferentSessionsTest extends EntityManagerFactoryBasedFunctionalTest { @Override protected Class[] getAnnotatedClasses() { return new Class[] { - Workload.class, + Workload.class, }; } @Override - @SuppressWarnings( "unchecked" ) + @SuppressWarnings("unchecked") protected void addConfigOptions(Map options) { options.put( AvailableSettings.JPA_PROXY_COMPLIANCE, Boolean.FALSE.toString() ); } @Test - @TestForIssue( jiraKey = "HHH-9856" ) + @TestForIssue(jiraKey = "HHH-9856") public void testReattachEntityToSessionWithJpaComplianceProxy() { - final Integer _workloadId = doInJPA( this::entityManagerFactory, entityManager -> { + final Integer _workloadId = fromTransaction( entityManager -> { Workload workload = new Workload(); workload.load = 123; workload.name = "Package"; @@ -51,26 +58,28 @@ public void testReattachEntityToSessionWithJpaComplianceProxy() { return workload.getId(); } ); - Workload _workload = doInJPA( this::entityManagerFactory, entityManager -> { - return entityManager.getReference( Workload.class, _workloadId ); - } ); + Workload _workload = fromTransaction( + entityManager -> + entityManager.getReference( Workload.class, _workloadId ) + ); Map settings = buildSettings(); settings.put( AvailableSettings.JPA_PROXY_COMPLIANCE, Boolean.TRUE.toString() ); settings.put( AvailableSettings.HBM2DDL_AUTO, "none" ); - EntityManagerFactory newEntityManagerFactory = Bootstrap - .getEntityManagerFactoryBuilder( - new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ), - settings ) - .build(); + EntityManagerFactory newEntityManagerFactory = Bootstrap + .getEntityManagerFactoryBuilder( + new TestingPersistenceUnitDescriptorImpl( getClass().getSimpleName() ), + settings + ) + .build(); try { doInJPA( () -> newEntityManagerFactory, entityManager -> { entityManager.unwrap( Session.class ).update( _workload ); _workload.getId(); - }); + } ); } finally { newEntityManagerFactory.close(); @@ -78,5 +87,6 @@ public void testReattachEntityToSessionWithJpaComplianceProxy() { assertEquals( "Package", _workload.getName() ); } + } diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceTest.java new file mode 100644 index 000000000000..87d5ddd7e5c0 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadJpaComplianceTest.java @@ -0,0 +1,147 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.ops; + +import javax.persistence.EntityNotFoundException; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Gavin King + * @author Hardy Ferentschik + */ +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsNoColumnInsert.class) +@Jpa( + annotatedClasses = { + Workload.class + }, + integrationSettings = { @Setting(name = AvailableSettings.JPA_PROXY_COMPLIANCE, value = "true") }, + xmlMappings = { "org/hibernate/orm/test/jpa/ops/Node.hbm.xml", "org/hibernate/orm/test/jpa/ops/Employer.hbm.xml" } +) +public class GetLoadJpaComplianceTest { + + @Test + @TestForIssue(jiraKey = "HHH-12034") + public void testLoadIdNotFound_FieldBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + Session s = (Session) entityManager.getDelegate(); + + assertNull( s.get( Workload.class, 999 ) ); + + Workload proxy = s.load( Workload.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + fail( "Should have failed because there is no Employee Entity with id == 999" ); + } + catch (EntityNotFoundException ex) { + // expected + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-12034") + public void testReferenceIdNotFound_FieldBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + + assertNull( entityManager.find( Workload.class, 999 ) ); + + Workload proxy = entityManager.getReference( Workload.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + fail( "Should have failed because there is no Workload Entity with id == 999" ); + } + catch (EntityNotFoundException ex) { + // expected + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-12034") + public void testLoadIdNotFound_PropertyBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + Session s = (Session) entityManager.getDelegate(); + + assertNull( s.get( Employee.class, 999 ) ); + + Employee proxy = s.load( Employee.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + fail( "Should have failed because there is no Employee Entity with id == 999" ); + } + catch (EntityNotFoundException ex) { + // expected + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue(jiraKey = "HHH-12034") + public void testReferenceIdNotFound_PropertyBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + + assertNull( entityManager.find( Employee.class, 999 ) ); + + Employee proxy = entityManager.getReference( Employee.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + fail( "Should have failed because there is no Employee Entity with id == 999" ); + } + catch (EntityNotFoundException ex) { + // expected + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadTest.java new file mode 100755 index 000000000000..d22cf1d0ca52 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/GetLoadTest.java @@ -0,0 +1,303 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.ops; + +import org.hibernate.Hibernate; +import org.hibernate.Session; +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.AbstractHANADialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.Setting; +import org.hibernate.testing.orm.junit.SkipForDialect; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Gavin King + * @author Hardy Ferentschik + */ +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsNoColumnInsert.class) +@Jpa( + annotatedClasses = { + Workload.class + }, + integrationSettings = { + @Setting(name = Environment.GENERATE_STATISTICS, value = "true"), + @Setting(name = Environment.STATEMENT_BATCH_SIZE, value = "0") + }, + xmlMappings = { + "org/hibernate/orm/test/jpa/ops/Node.hbm.xml", + "org/hibernate/orm/test/jpa/ops/Employer.hbm.xml" + } +) +public class GetLoadTest { + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "delete from Employer" ).executeUpdate(); + entityManager.createQuery( "delete from Workload" ).executeUpdate(); + entityManager.createQuery( "update Node set parent = null" ).executeUpdate(); + entityManager.createQuery( "delete from Node" ).executeUpdate(); + } + ); + } + + @Test + @SkipForDialect(dialectClass = AbstractHANADialect.class, reason = " HANA doesn't support tables consisting of only a single auto-generated column") + public void testGet(EntityManagerFactoryScope scope) { + clearCounts(scope); + String nodeName = "foo"; + + Integer empId = scope.fromTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + + Employer emp = new Employer(); + s.persist( emp ); + Node node = new Node( nodeName ); + Node parent = new Node( "bar" ); + parent.addChild( node ); + s.persist( parent ); + return emp.getId(); + } + ); + + scope.inTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + Employer emp = s.get( Employer.class, empId ); + assertTrue( Hibernate.isInitialized( emp ) ); + assertFalse( Hibernate.isInitialized( emp.getEmployees() ) ); + Node node = s.get( Node.class, nodeName ); + assertTrue( Hibernate.isInitialized( node ) ); + assertFalse( Hibernate.isInitialized( node.getChildren() ) ); + assertFalse( Hibernate.isInitialized( node.getParent() ) ); + assertNull( s.get( Node.class, "xyz" ) ); + } + ); + + scope.inTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + Employer emp = ( Employer ) s.get( Employer.class.getName(), empId ); + assertTrue( Hibernate.isInitialized( emp ) ); + Node node = ( Node ) s.get( Node.class.getName(), nodeName ); + assertTrue( Hibernate.isInitialized( node ) ); + } + ); + + assertFetchCount( scope, 0 ); + } + + @Test + @SkipForDialect(dialectClass = AbstractHANADialect.class, reason = " HANA doesn't support tables consisting of only a single auto-generated column") + public void testLoad(EntityManagerFactoryScope scope) { + clearCounts(scope); + String nodeName = "foo"; + + Integer empId = scope.fromTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + + Employer emp = new Employer(); + s.persist( emp ); + Node node = new Node( nodeName ); + Node parent = new Node( "bar" ); + parent.addChild( node ); + s.persist( parent ); + return emp.getId(); + } + ); + + scope.inTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + Employer emp = s.load( Employer.class, empId ); + emp.getId(); + assertFalse( Hibernate.isInitialized( emp ) ); + Node node = s.load( Node.class, nodeName ); + assertEquals( node.getName(), nodeName ); + assertFalse( Hibernate.isInitialized( node ) ); + } + ); + + scope.inTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + Employer emp = ( Employer ) s.load( Employer.class.getName(), empId ); + emp.getId(); + assertFalse( Hibernate.isInitialized( emp ) ); + Node node = ( Node ) s.load( Node.class.getName(), nodeName ); + assertEquals( node.getName(), nodeName ); + assertFalse( Hibernate.isInitialized( node ) ); + } + ); + + assertFetchCount( scope, 0 ); + } + + private void clearCounts(EntityManagerFactoryScope scope) { + scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getStatistics().clear(); + } + + private void assertFetchCount(EntityManagerFactoryScope scope, int count) { + int fetches = ( int ) scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getStatistics().getEntityFetchCount(); + assertEquals( count, fetches ); + } + + @Test + @TestForIssue( jiraKey = "HHH-9856" ) + public void testNonEntity(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + entityManager.getTransaction().begin(); + try { + entityManager.getReference( String.class, 1 ); + fail( "Expecting a failure" ); + } + catch (IllegalArgumentException ignore) { + // expected + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-11838") + public void testLoadGetId(EntityManagerFactoryScope scope) { + Workload workload = scope.fromTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + Workload _workload = new Workload(); + s.persist(_workload); + return _workload; + } + ); + + scope.inTransaction( + entityManager -> { + Session s = ( Session ) entityManager.getDelegate(); + + Workload proxy = s.load(Workload.class, workload.id); + proxy.getId(); + + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getName(); + + assertTrue( Hibernate.isInitialized( proxy ) ); + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12034") + public void testLoadIdNotFound_FieldBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + Session s = (Session) entityManager.getDelegate(); + + assertNull( s.get( Workload.class, 999 ) ); + + Workload proxy = s.load( Workload.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12034") + public void testReferenceIdNotFound_FieldBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + + assertNull( entityManager.find( Workload.class, 999 ) ); + + Workload proxy = entityManager.getReference( Workload.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12034") + public void testLoadIdNotFound_PropertyBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + Session s = (Session) entityManager.getDelegate(); + + assertNull( s.get( Employee.class, 999 ) ); + + Employee proxy = s.load( Employee.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12034") + public void testReferenceIdNotFound_PropertyBasedAccess(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + + assertNull( entityManager.find( Employee.class, 999 ) ); + + Employee proxy = entityManager.getReference( Employee.class, 999 ); + assertFalse( Hibernate.isInitialized( proxy ) ); + + proxy.getId(); + } + finally { + entityManager.getTransaction().rollback(); + } + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Mammal.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Mammal.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Mammal.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Mammal.java index e92247303fb6..4434936b13cb 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Mammal.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Mammal.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import javax.persistence.Entity; /** diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeNewTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeNewTest.java new file mode 100644 index 000000000000..d0b851ff35cf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeNewTest.java @@ -0,0 +1,86 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.ops; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * @author Emmanuel Bernard + */ +@Jpa(annotatedClasses = { + Workload.class +}) +public class MergeNewTest { + + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "delete from Workload" ).executeUpdate(); + } + ); + } + + @Test + public void testMergeNew(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + Workload load = new Workload(); + load.name = "Cleaning"; + load.load = 10; + entityManager.getTransaction().begin(); + load = entityManager.merge( load ); + assertNotNull( load.id ); + entityManager.flush(); + assertNotNull( load.id ); + } + finally { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + } + } + ); + } + + @Test + public void testMergeAfterRemove(EntityManagerFactoryScope scope) { + + Integer load_id = scope.fromTransaction( + entityManager -> { + Workload _load = new Workload(); + _load.name = "Cleaning"; + _load.load = 10; + _load = entityManager.merge( _load ); + entityManager.flush(); + return _load.getId(); + } + ); + + Workload load =scope.fromTransaction( + entityManager -> { + Workload _load = entityManager.find( Workload.class, load_id ); + entityManager.remove( _load ); + entityManager.flush(); + return _load; + } + ); + + scope.inTransaction( + entityManager -> { + entityManager.merge( load ); + entityManager.flush(); + } + ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeTest.java new file mode 100755 index 000000000000..017e0190a751 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/MergeTest.java @@ -0,0 +1,129 @@ +/* + * 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 . + */ +package org.hibernate.orm.test.jpa.ops; + +import org.hibernate.cfg.Environment; +import org.hibernate.engine.spi.SessionFactoryImplementor; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +/** + * @author Gavin King + * @author Hardy Ferentschik + */ +@Jpa( + integrationSettings = { + @Setting(name = Environment.GENERATE_STATISTICS, value = "true"), + @Setting(name = Environment.STATEMENT_BATCH_SIZE, value = "0") + }, + xmlMappings = { + "org/hibernate/orm/test/jpa/ops/Node.hbm.xml" + } +) +public class MergeTest { + + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "update Node set parent = null" ).executeUpdate(); + entityManager.createQuery( "delete from Node" ).executeUpdate(); + } + ); + } + + @Test + public void testMergeTree(EntityManagerFactoryScope scope) { + clearCounts( scope ); + + Node root = new Node( "root" ); + Node child = new Node( "child" ); + + scope.inTransaction( + entityManager -> { + root.addChild( child ); + entityManager.persist( root ); + } + ); + + assertInsertCount( scope, 2 ); + clearCounts( scope ); + + root.setDescription( "The root node" ); + child.setDescription( "The child node" ); + + Node secondChild = new Node( "second child" ); + + root.addChild( secondChild ); + + scope.inTransaction( + entityManager -> + entityManager.merge( root ) + ); + + assertInsertCount( scope, 1 ); + assertUpdateCount( scope, 2 ); + } + + @Test + public void testMergeTreeWithGeneratedId(EntityManagerFactoryScope scope) { + clearCounts( scope ); + + NumberedNode root = new NumberedNode( "root" ); + NumberedNode child = new NumberedNode( "child" ); + root.addChild( child ); + + scope.inTransaction( + entityManager -> + entityManager.persist( root ) + ); + + assertInsertCount( scope, 2 ); + clearCounts( scope ); + + root.setDescription( "The root node" ); + child.setDescription( "The child node" ); + + NumberedNode secondChild = new NumberedNode( "second child" ); + + root.addChild( secondChild ); + + scope.inTransaction( + entityManager -> + entityManager.merge( root ) + ); + + assertInsertCount( scope, 1 ); + assertUpdateCount( scope, 2 ); + } + + private void clearCounts(EntityManagerFactoryScope scope) { + scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getStatistics().clear(); + } + + private void assertInsertCount(EntityManagerFactoryScope scope, int count) { + int inserts = (int) scope.getEntityManagerFactory() + .unwrap( SessionFactoryImplementor.class ) + .getStatistics() + .getEntityInsertCount(); + Assertions.assertEquals( count, inserts ); + } + + private void assertUpdateCount(EntityManagerFactoryScope scope, int count) { + int updates = (int) scope.getEntityManagerFactory() + .unwrap( SessionFactoryImplementor.class ) + .getStatistics() + .getEntityUpdateCount(); + Assertions.assertEquals( count, updates ); + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Node.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Node.java similarity index 96% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Node.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Node.java index 2a39baec16dd..c7f8ab17380a 100755 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Node.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Node.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import java.util.HashSet; import java.util.Set; diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/NumberedNode.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/NumberedNode.java similarity index 92% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/NumberedNode.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/NumberedNode.java index 7838a0d5187c..b8701cf81ff8 100755 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/NumberedNode.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/NumberedNode.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; /** diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/PersistTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/PersistTest.java new file mode 100755 index 000000000000..6797feb397e1 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/PersistTest.java @@ -0,0 +1,285 @@ +/* + * 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 . + */ + +// $Id$ + +package org.hibernate.orm.test.jpa.ops; + +import java.util.ArrayList; +import java.util.Collection; +import javax.persistence.PersistenceException; +import javax.persistence.RollbackException; + +import org.hibernate.cfg.Environment; +import org.hibernate.dialect.AbstractHANADialect; +import org.hibernate.engine.spi.SessionFactoryImplementor; + +import org.hibernate.testing.orm.junit.DialectFeatureChecks; +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.RequiresDialectFeature; +import org.hibernate.testing.orm.junit.Setting; +import org.hibernate.testing.orm.junit.SkipForDialect; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * @author Gavin King + * @author Hardy Ferentschik + */ +@RequiresDialectFeature(feature = DialectFeatureChecks.SupportsNoColumnInsert.class) +@Jpa( + annotatedClasses = { + Node.class + }, + integrationSettings = { + @Setting(name = Environment.GENERATE_STATISTICS, value = "true"), + @Setting(name = Environment.STATEMENT_BATCH_SIZE, value = "0") + }, + xmlMappings = { + "org/hibernate/orm/test/jpa/ops/Node.hbm.xml", + "org/hibernate/orm/test/jpa/ops/Employer.hbm.xml" + } +) +public class PersistTest { + @AfterEach + public void tearDown(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + entityManager.createQuery( "update Node set parent = null" ).executeUpdate(); + entityManager.createQuery( "delete from Node" ).executeUpdate(); + entityManager.createQuery( "update NumberedNode set parent = null" ).executeUpdate(); + entityManager.createQuery( "delete from NumberedNode" ).executeUpdate(); + } + ); + } + + @Test + public void testCreateTree(EntityManagerFactoryScope scope) { + clearCounts( scope ); + + scope.inTransaction( + entityManager -> { + Node root = new Node( "root" ); + Node child = new Node( "child" ); + root.addChild( child ); + entityManager.persist( root ); + } + ); + + assertInsertCount( scope, 2 ); + assertUpdateCount( scope, 0 ); + + scope.inTransaction( + entityManager -> { + Node _root = entityManager.find( Node.class, "root" ); + Node child2 = new Node( "child2" ); + _root.addChild( child2 ); + } + ); + + assertInsertCount( scope, 3 ); + assertUpdateCount( scope, 0 ); + } + + @Test + public void testCreateTreeWithGeneratedId(EntityManagerFactoryScope scope) { + clearCounts( scope ); + + Long rootId = scope.fromTransaction( + entityManager -> { + NumberedNode root = new NumberedNode( "root" ); + NumberedNode child = new NumberedNode( "child" ); + root.addChild( child ); + entityManager.persist( root ); + return root.getId(); + } + ); + + assertInsertCount( scope, 2 ); + assertUpdateCount( scope, 0 ); + + scope.inTransaction( + entityManager -> { + NumberedNode _root = entityManager.find( NumberedNode.class, rootId ); + NumberedNode child2 = new NumberedNode( "child2" ); + _root.addChild( child2 ); + } + ); + + assertInsertCount( scope, 3 ); + assertUpdateCount( scope, 0 ); + } + + @Test + public void testCreateException(EntityManagerFactoryScope scope) { + Node dupe = scope.fromTransaction( + entityManager -> { + Node _dupe = new Node( "dupe" ); + entityManager.persist( _dupe ); + entityManager.persist( _dupe ); + return _dupe; + } + ); + + scope.inEntityManager( + entityManager -> { + entityManager.getTransaction().begin(); + entityManager.persist( dupe ); + try { + entityManager.getTransaction().commit(); + fail( "Cannot persist() twice the same entity" ); + } + catch (Exception cve) { + //verify that an exception is thrown! + } + finally { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + } + } + ); + + Node nondupe = new Node( "nondupe" ); + nondupe.addChild( dupe ); + + scope.inEntityManager( + entityManager -> { + entityManager.getTransaction().begin(); + entityManager.persist( nondupe ); + try { + entityManager.getTransaction().commit(); + fail(); + } + catch (RollbackException e) { + //verify that an exception is thrown! + } + finally { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + } + } + ); + } + + @Test + public void testCreateExceptionWithGeneratedId(EntityManagerFactoryScope scope) { + NumberedNode dupe = scope.fromTransaction( + entityManager -> { + NumberedNode _dupe = new NumberedNode( "dupe" ); + entityManager.persist( _dupe ); + entityManager.persist( _dupe ); + return _dupe; + } + ); + + scope.inEntityManager( + entityManager -> { + entityManager.getTransaction().begin(); + try { + entityManager.persist( dupe ); + fail(); + } + catch (PersistenceException poe) { + //verify that an exception is thrown! + } + finally { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + } + } + ); + + NumberedNode nondupe = new NumberedNode( "nondupe" ); + nondupe.addChild( dupe ); + + scope.inEntityManager( + entityManager -> { + entityManager.getTransaction().begin(); + try { + entityManager.persist( nondupe ); + fail(); + } + catch (PersistenceException poe) { + //verify that an exception is thrown! + } + finally { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + } + } + ); + } + + @Test + @SkipForDialect(dialectClass = AbstractHANADialect.class, reason = " HANA doesn't support tables consisting of only a single auto-generated column") + public void testBasic(EntityManagerFactoryScope scope) { + scope.inEntityManager( + entityManager -> { + try { + entityManager.getTransaction().begin(); + Employer er = new Employer(); + Employee ee = new Employee(); + entityManager.persist( ee ); + Collection erColl = new ArrayList<>(); + Collection eeColl = new ArrayList<>(); + erColl.add( ee ); + eeColl.add( er ); + er.setEmployees( erColl ); + ee.setEmployers( eeColl ); + entityManager.getTransaction().commit(); + entityManager.close(); + + entityManager = scope.getEntityManagerFactory().createEntityManager(); + entityManager.getTransaction().begin(); + er = entityManager.find( Employer.class, er.getId() ); + assertNotNull( er ); + assertNotNull( er.getEmployees() ); + assertEquals( 1, er.getEmployees().size() ); + Employee eeFromDb = ( Employee ) er.getEmployees().iterator().next(); + assertEquals( ee.getId(), eeFromDb.getId() ); + entityManager.getTransaction().commit(); + } + catch (Exception e) { + if ( entityManager.getTransaction().isActive() ) { + entityManager.getTransaction().rollback(); + } + throw e; + } + } + ); + } + + private void clearCounts(EntityManagerFactoryScope scope) { + scope.getEntityManagerFactory().unwrap( SessionFactoryImplementor.class ).getStatistics().clear(); + } + + private void assertInsertCount(EntityManagerFactoryScope scope, int count) { + int inserts = (int) scope.getEntityManagerFactory() + .unwrap( SessionFactoryImplementor.class ) + .getStatistics() + .getEntityInsertCount(); + assertEquals( count, inserts ); + } + + private void assertUpdateCount(EntityManagerFactoryScope scope, int count) { + int updates = (int) scope.getEntityManagerFactory() + .unwrap( SessionFactoryImplementor.class ) + .getStatistics() + .getEntityUpdateCount(); + assertEquals( count, updates ); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/RemoveOrderingTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/RemoveOrderingTest.java new file mode 100644 index 000000000000..69635789aee7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/RemoveOrderingTest.java @@ -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 . + */ +package org.hibernate.orm.test.jpa.ops; + +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.hibernate.cfg.AvailableSettings; + +import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; +import org.hibernate.testing.orm.junit.FailureExpected; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.orm.junit.Jpa; +import org.hibernate.testing.orm.junit.Setting; + +import org.junit.jupiter.api.Test; + +/** + * @author Steve Ebersole + */ +@Jpa( + annotatedClasses = { + RemoveOrderingTest.Person.class, RemoveOrderingTest.Company.class + }, + integrationSettings = { @Setting(name = AvailableSettings.JPA_VALIDATION_MODE, value = "NONE") } +) +public class RemoveOrderingTest { + + @Test + @TestForIssue(jiraKey = "HHH-8550") + @FailureExpected(jiraKey = "HHH-8550") + public void testManyToOne(EntityManagerFactoryScope scope) { + scope.inTransaction( + entityManager -> { + Company company = new Company( 1, "acme" ); + Person person = new Person( 1, "joe", company ); + entityManager.persist( person ); + entityManager.flush(); + + entityManager.remove( company ); + entityManager.remove( person ); + entityManager.flush(); + + entityManager.persist( person ); + entityManager.flush(); + } + ); + } + + @Entity(name = "Company") + @Table(name = "COMPANY") + public static class Company { + @Id + public Integer id; + public String name; + + public Company() { + } + + public Company(Integer id, String name) { + this.id = id; + this.name = name; + } + } + + @Entity(name = "Person") + @Table(name = "PERSON") + public static class Person { + @Id + public Integer id; + public String name; + @ManyToOne(cascade = CascadeType.ALL, optional = false) + @JoinColumn(name = "EMPLOYER_FK") + public Company employer; + + public Person() { + } + + public Person(Integer id, String name, Company employer) { + this.id = id; + this.name = name; + this.employer = employer; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Reptile.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Reptile.java similarity index 93% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Reptile.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Reptile.java index ea3fc91f6946..89103c3e29e1 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Reptile.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Reptile.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import javax.persistence.Entity; /** diff --git a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Workload.java b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Workload.java similarity index 94% rename from hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Workload.java rename to hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Workload.java index ddaed2ca3b55..c4fcf4ff800d 100644 --- a/hibernate-core/src/test/java/org/hibernate/jpa/test/ops/Workload.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/jpa/ops/Workload.java @@ -6,7 +6,7 @@ */ //$Id$ -package org.hibernate.jpa.test.ops; +package org.hibernate.orm.test.jpa.ops; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/ops/Employer.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/jpa/ops/Employer.hbm.xml similarity index 95% rename from hibernate-core/src/test/resources/org/hibernate/jpa/test/ops/Employer.hbm.xml rename to hibernate-core/src/test/resources/org/hibernate/orm/test/jpa/ops/Employer.hbm.xml index ace69ec9fcf2..d674a2b42847 100644 --- a/hibernate-core/src/test/resources/org/hibernate/jpa/test/ops/Employer.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/jpa/ops/Employer.hbm.xml @@ -13,7 +13,7 @@ --> - + diff --git a/hibernate-core/src/test/resources/org/hibernate/jpa/test/ops/Node.hbm.xml b/hibernate-core/src/test/resources/org/hibernate/orm/test/jpa/ops/Node.hbm.xml similarity index 95% rename from hibernate-core/src/test/resources/org/hibernate/jpa/test/ops/Node.hbm.xml rename to hibernate-core/src/test/resources/org/hibernate/orm/test/jpa/ops/Node.hbm.xml index 1c08fe762671..1ee74338fd07 100755 --- a/hibernate-core/src/test/resources/org/hibernate/jpa/test/ops/Node.hbm.xml +++ b/hibernate-core/src/test/resources/org/hibernate/orm/test/jpa/ops/Node.hbm.xml @@ -13,7 +13,7 @@ --> - + diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryBasedFunctionalTest.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryBasedFunctionalTest.java index 6c5fef1d832a..7b6577c947b1 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryBasedFunctionalTest.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryBasedFunctionalTest.java @@ -292,7 +292,7 @@ protected void inTransaction(Consumer action) { entityManagerFactoryScope().inTransaction( action ); } - protected T inTransaction(Function action) { - return entityManagerFactoryScope().inTransaction( action ); + protected T fromTransaction(Function action) { + return entityManagerFactoryScope().fromTransaction( action ); } } diff --git a/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryScope.java b/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryScope.java index cc1e2bac7238..2716d5d0c236 100644 --- a/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryScope.java +++ b/hibernate-testing/src/main/java/org/hibernate/testing/junit5/EntityManagerFactoryScope.java @@ -64,8 +64,15 @@ public void inTransaction(Consumer action) { inTransaction( getEntityManagerFactory(), action ); } - public T inTransaction(Function action) { - return inTransaction( getEntityManagerFactory().createEntityManager(), action ); + public T fromTransaction(Function action) { + EntityManager entityManager = getEntityManagerFactory().createEntityManager(); + try { + T result = fromTransaction( entityManager, action ); + return result; + } + finally { + entityManager.close(); + } } public void inTransaction(EntityManagerFactory factory, Consumer action) { @@ -82,7 +89,7 @@ public void inTransaction(EntityManagerFactory factory, Consumer } } - public T inTransaction(EntityManager entityManager, Function action) { + public T fromTransaction(EntityManager entityManager, Function action) { log.trace( "inTransaction(entityManager, action)" ); final EntityTransaction trx = entityManager.getTransaction(); final T result;