HHH-14503 - Migration of tests from jpa/test to orm/test/jpa

Signed-off-by: Jan Schatteman <jschatte@redhat.com>
This commit is contained in:
Jan Schatteman 2021-03-10 22:36:17 +01:00 committed by Christian Beikov
parent 3f2afe6b40
commit 7695f03bf9
30 changed files with 1230 additions and 1128 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
};
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 };
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 };
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
};
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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" };
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
// $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<Employee> erColl = new ArrayList<Employee>();
Collection<Employer> eeColl = new ArrayList<Employer>();
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"
};
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}
}

View File

@ -46,7 +46,7 @@ public class AssociationTest extends EntityManagerFactoryBasedFunctionalTest {
@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 class AssociationTest extends EntityManagerFactoryBasedFunctionalTest {
return oven;
} );
Oven mergedOven = inTransaction(
Oven mergedOven = fromTransaction(
entityManager -> {
return entityManager.merge( persistedOven );
}

View File

@ -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;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
}
}
);
}
}

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.ops;
package org.hibernate.orm.test.jpa.ops;
import java.util.Map;
import javax.persistence.EntityManagerFactory;
@ -12,37 +12,44 @@ import javax.persistence.EntityManagerFactory;
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 class GetLoadJpaComplianceDifferentSessionsTest extends BaseEntityManager
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 class GetLoadJpaComplianceDifferentSessionsTest extends BaseEntityManager
assertEquals( "Package", _workload.getName() );
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
}
);
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
}
);
}
}

View File

@ -6,7 +6,7 @@
*/
//$Id$
package org.hibernate.jpa.test.ops;
package org.hibernate.orm.test.jpa.ops;
import javax.persistence.Entity;
/**

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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();
}
);
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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 );
}
}

View File

@ -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;

View File

@ -6,7 +6,7 @@
*/
//$Id$
package org.hibernate.jpa.test.ops;
package org.hibernate.orm.test.jpa.ops;
/**

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
// $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<Employee> erColl = new ArrayList<>();
Collection<Employer> 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 );
}
}

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.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;
}
}
}

View File

@ -6,7 +6,7 @@
*/
//$Id$
package org.hibernate.jpa.test.ops;
package org.hibernate.orm.test.jpa.ops;
import javax.persistence.Entity;
/**

View File

@ -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;

View File

@ -13,7 +13,7 @@
-->
<hibernate-mapping package="org.hibernate.jpa.test.ops">
<hibernate-mapping package="org.hibernate.orm.test.jpa.ops">
<class name="Employer" polymorphism="explicit">
<id name="id">

View File

@ -13,7 +13,7 @@
-->
<hibernate-mapping package="org.hibernate.jpa.test.ops">
<hibernate-mapping package="org.hibernate.orm.test.jpa.ops">
<class name="Node" polymorphism="explicit">
<id name="name">

View File

@ -292,7 +292,7 @@ public class EntityManagerFactoryBasedFunctionalTest
entityManagerFactoryScope().inTransaction( action );
}
protected <T> T inTransaction(Function<EntityManager, T> action) {
return entityManagerFactoryScope().inTransaction( action );
protected <T> T fromTransaction(Function<EntityManager, T> action) {
return entityManagerFactoryScope().fromTransaction( action );
}
}

View File

@ -64,8 +64,15 @@ public class EntityManagerFactoryScope implements EntityManagerFactoryAccess {
inTransaction( getEntityManagerFactory(), action );
}
public <T> T inTransaction(Function<EntityManager, T> action) {
return inTransaction( getEntityManagerFactory().createEntityManager(), action );
public <T> T fromTransaction(Function<EntityManager, T> action) {
EntityManager entityManager = getEntityManagerFactory().createEntityManager();
try {
T result = fromTransaction( entityManager, action );
return result;
}
finally {
entityManager.close();
}
}
public void inTransaction(EntityManagerFactory factory, Consumer<EntityManager> action) {
@ -82,7 +89,7 @@ public class EntityManagerFactoryScope implements EntityManagerFactoryAccess {
}
}
public <T> T inTransaction(EntityManager entityManager, Function<EntityManager, T> action) {
public <T> T fromTransaction(EntityManager entityManager, Function<EntityManager, T> action) {
log.trace( "inTransaction(entityManager, action)" );
final EntityTransaction trx = entityManager.getTransaction();
final T result;