Fix some issues with tests migration
This commit is contained in:
parent
491cbabc6c
commit
e0e44433a9
|
@ -0,0 +1,58 @@
|
||||||
|
/*
|
||||||
|
* 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.cacheable.api;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
|
|
||||||
|
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.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Steve Ebersole
|
||||||
|
*/
|
||||||
|
// TODO Convert to annotation based testing? Setting the CachingRegionFactory as below leads to a CNFE
|
||||||
|
@Jpa(
|
||||||
|
annotatedClasses = Order.class,
|
||||||
|
integrationSettings = {
|
||||||
|
@Setting(name = AvailableSettings.CACHE_REGION_FACTORY, value = "org.hibernate.testing.cache.CachingRegionFactory"),
|
||||||
|
@Setting(name = AvailableSettings.JPA_SHARED_CACHE_MODE, value = "ALL"),
|
||||||
|
@Setting(name = AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, value = "read-write"),
|
||||||
|
@Setting(name = AvailableSettings.USE_SECOND_LEVEL_CACHE, value = "true"),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
public class JpaCacheApiUsageTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEviction(EntityManagerFactoryScope scope) {
|
||||||
|
// first create an Order
|
||||||
|
scope.inTransaction(
|
||||||
|
entityManager ->
|
||||||
|
entityManager.persist( new Order( 1, 500 ) )
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
final EntityManagerFactory entityManagerFactory = scope.getEntityManagerFactory();
|
||||||
|
assertTrue( entityManagerFactory.getCache().contains( Order.class, 1 ) );
|
||||||
|
|
||||||
|
scope.inTransaction(
|
||||||
|
entityManager -> {
|
||||||
|
assertTrue( entityManagerFactory.getCache().contains( Order.class, 1 ) );
|
||||||
|
entityManager.createQuery( "delete Order" ).executeUpdate();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
assertFalse( entityManagerFactory.getCache().contains( Order.class, 1 ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -4,7 +4,7 @@
|
||||||
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
|
* 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>.
|
* 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.cacheable.api;
|
package org.hibernate.jpa.test.cacheable.api;
|
||||||
|
|
||||||
import javax.persistence.Cacheable;
|
import javax.persistence.Cacheable;
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
|
@ -1,110 +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.orm.test.jpa.cacheable.api;
|
|
||||||
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.SharedCacheMode;
|
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.hibernate.NotYetImplementedFor6Exception;
|
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
|
||||||
import org.hibernate.jpa.test.BaseEntityManagerFunctionalTestCase;
|
|
||||||
import org.hibernate.testing.cache.CachingRegionFactory;
|
|
||||||
import org.hibernate.testing.orm.junit.NotImplementedYet;
|
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Steve Ebersole
|
|
||||||
*/
|
|
||||||
// TODO Convert to annotation based testing? Setting the CachingRegionFactory as below leads to a CNFE
|
|
||||||
//@DomainModel(
|
|
||||||
// annotatedClasses = Order.class
|
|
||||||
//)
|
|
||||||
//@ServiceRegistry(
|
|
||||||
// settings = {
|
|
||||||
// @Setting(name = AvailableSettings.CACHE_REGION_FACTORY, value = "CachingRegionFactory.class"),
|
|
||||||
// @Setting(name = AvailableSettings.JPA_SHARED_CACHE_MODE, value = "ALL")
|
|
||||||
// }
|
|
||||||
//)
|
|
||||||
//@SessionFactory
|
|
||||||
public class JpaCacheApiUsageTest /*extends BaseEntityManagerFunctionalTestCase*/ {
|
|
||||||
// @Override
|
|
||||||
// protected Class<?>[] getAnnotatedClasses() {
|
|
||||||
// return new Class[] { Order.class };
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @Override
|
|
||||||
// @SuppressWarnings("unchecked")
|
|
||||||
// protected void addConfigOptions(Map options) {
|
|
||||||
//// options.put( AvailableSettings.USE_SECOND_LEVEL_CACHE, "true" );
|
|
||||||
// options.put( AvailableSettings.CACHE_REGION_FACTORY, CachingRegionFactory.class.getName() );
|
|
||||||
//// options.put( AvailableSettings.DEFAULT_CACHE_CONCURRENCY_STRATEGY, "read-write" );
|
|
||||||
// options.put( org.hibernate.jpa.AvailableSettings.SHARED_CACHE_MODE, SharedCacheMode.ALL );
|
|
||||||
// }
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@NotImplementedYet(reason = "BulkOperationCleanupAction is never created", expectedVersion = "6.0")
|
|
||||||
public void testEviction() {
|
|
||||||
throw new NotYetImplementedFor6Exception();
|
|
||||||
// first create an Order
|
|
||||||
// EntityManager em = getOrCreateEntityManager();
|
|
||||||
// em.getTransaction().begin();
|
|
||||||
// em.persist( new Order( 1, 500 ) );
|
|
||||||
// em.getTransaction().commit();
|
|
||||||
// em.close();
|
|
||||||
//
|
|
||||||
// assertTrue( entityManagerFactory().getCache().contains( Order.class, 1 ) );
|
|
||||||
//
|
|
||||||
// em = getOrCreateEntityManager();
|
|
||||||
// em.getTransaction().begin();
|
|
||||||
// assertTrue( entityManagerFactory().getCache().contains( Order.class, 1 ) );
|
|
||||||
// em.createQuery( "delete Order" ).executeUpdate();
|
|
||||||
// em.getTransaction().commit();
|
|
||||||
// em.close();
|
|
||||||
//
|
|
||||||
// assertFalse( entityManagerFactory().getCache().contains( Order.class, 1 ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
// @Test
|
|
||||||
// public void testEviction(SessionFactoryScope scope) {
|
|
||||||
// scope.inTransaction(
|
|
||||||
// session -> {
|
|
||||||
// session.getTransaction().begin();
|
|
||||||
// session.persist( new Order( 1, 500 ) );
|
|
||||||
// session.getTransaction().commit();
|
|
||||||
// session.close();
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// scope.inSession(
|
|
||||||
// session -> {
|
|
||||||
// assertTrue( session.getEntityManagerFactory().getCache().contains( Order.class, 1 ) );
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// scope.inTransaction(
|
|
||||||
// session -> {
|
|
||||||
// session.getTransaction().begin();
|
|
||||||
// assertTrue( session.getEntityManagerFactory().getCache().contains( Order.class, 1 ) );
|
|
||||||
// session.createQuery( "delete Order" ).executeUpdate();
|
|
||||||
// session.getTransaction().commit();
|
|
||||||
// session.close();
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
//
|
|
||||||
// scope.inSession(
|
|
||||||
// session -> {
|
|
||||||
// assertFalse( session.getEntityManagerFactory().getCache().contains( Order.class, 1 ) );
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
// }
|
|
||||||
}
|
|
|
@ -15,7 +15,6 @@ import org.hibernate.jpa.test.Kitten;
|
||||||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||||
import org.hibernate.testing.orm.junit.Jpa;
|
import org.hibernate.testing.orm.junit.Jpa;
|
||||||
import org.hibernate.testing.orm.junit.Setting;
|
import org.hibernate.testing.orm.junit.Setting;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
@ -39,28 +38,19 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
public class CallbacksDisabledTest {
|
public class CallbacksDisabledTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCallbacksAreDisabled(EntityManagerFactoryScope scope) throws Exception {
|
public void testCallbacksAreDisabled(EntityManagerFactoryScope scope) {
|
||||||
int id = scope.fromTransaction(
|
scope.inTransaction(
|
||||||
entityManager -> {
|
entityManager -> {
|
||||||
Cat c = new Cat();
|
Cat c = new Cat();
|
||||||
c.setName( "Kitty" );
|
c.setName( "Kitty" );
|
||||||
c.setDateOfBirth( new Date( 90, 11, 15 ) );
|
c.setDateOfBirth( new Date( 90, 11, 15 ) );
|
||||||
entityManager.persist( c );
|
entityManager.persist( c );
|
||||||
return c.getId();
|
entityManager.getTransaction().commit();
|
||||||
}
|
entityManager.clear();
|
||||||
);
|
entityManager.getTransaction().begin();
|
||||||
|
Cat _c = entityManager.find( Cat.class, c.getId() );
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
Cat _c = entityManager.find( Cat.class, id );
|
|
||||||
assertTrue( _c.getAge() == 0 ); // With listeners enabled this would be false. Proven by org.hibernate.orm.test.jpa.callbacks.CallbacksTest.testCallbackMethod
|
assertTrue( _c.getAge() == 0 ); // With listeners enabled this would be false. Proven by org.hibernate.orm.test.jpa.callbacks.CallbacksTest.testCallbackMethod
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Cat" ).executeUpdate();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ import org.hibernate.jpa.test.Kitten;
|
||||||
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
|
||||||
import org.hibernate.testing.orm.junit.FailureExpected;
|
import org.hibernate.testing.orm.junit.FailureExpected;
|
||||||
import org.hibernate.testing.orm.junit.Jpa;
|
import org.hibernate.testing.orm.junit.Jpa;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
@ -38,34 +38,40 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
Rythm.class
|
Rythm.class
|
||||||
})
|
})
|
||||||
public class CallbacksTest {
|
public class CallbacksTest {
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void tearDown(EntityManagerFactoryScope scope) {
|
||||||
|
scope.inTransaction(
|
||||||
|
entityManager -> {
|
||||||
|
entityManager.createQuery( "delete from Cat" ).executeUpdate();
|
||||||
|
entityManager.createQuery( "delete from Television" ).executeUpdate();
|
||||||
|
entityManager.createQuery( "delete from Plant" ).executeUpdate();
|
||||||
|
entityManager.createQuery( "delete from Kitten" ).executeUpdate();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCallbackMethod(EntityManagerFactoryScope scope) {
|
public void testCallbackMethod(EntityManagerFactoryScope scope) {
|
||||||
int id = scope.fromTransaction(
|
scope.inTransaction(
|
||||||
entityManager -> {
|
entityManager -> {
|
||||||
Cat c = new Cat();
|
Cat c = new Cat();
|
||||||
c.setName( "Kitty" );
|
c.setName( "Kitty" );
|
||||||
c.setDateOfBirth( new Date( 90, 11, 15 ) );
|
c.setDateOfBirth( new Date( 90, 11, 15 ) );
|
||||||
entityManager.persist( c );
|
entityManager.persist( c );
|
||||||
return c.getId();
|
entityManager.getTransaction().commit();
|
||||||
}
|
|
||||||
);
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
Cat _c = entityManager.find( Cat.class, id );
|
|
||||||
assertFalse( _c.getAge() == 0 );
|
|
||||||
_c.setName( "Tomcat" ); //update this entity
|
|
||||||
}
|
|
||||||
);
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
Cat _c = entityManager.find( Cat.class, id );
|
|
||||||
assertEquals( "Tomcat", _c.getName() );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
scope.inTransaction(
|
entityManager.clear();
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Cat" ).executeUpdate();
|
entityManager.getTransaction().begin();
|
||||||
|
c = entityManager.find( Cat.class, c.getId() );
|
||||||
|
assertFalse( c.getAge() == 0 );
|
||||||
|
c.setName( "Tomcat" ); //update this entity
|
||||||
|
entityManager.getTransaction().commit();
|
||||||
|
entityManager.clear();
|
||||||
|
entityManager.getTransaction().begin();
|
||||||
|
c = entityManager.find( Cat.class, c.getId() );
|
||||||
|
assertEquals( "Tomcat", c.getName() );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -76,44 +82,38 @@ public class CallbacksTest {
|
||||||
PV(int version) {
|
PV(int version) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int version;
|
private int version;
|
||||||
|
|
||||||
private void set(int version) {
|
private void set(int version) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int get() {
|
private int get() {
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Cat c = new Cat();
|
|
||||||
c.setName( "Kitty" );
|
|
||||||
c.setLength( 12 );
|
|
||||||
c.setDateOfBirth( new Date( 90, 11, 15 ) );
|
|
||||||
PV previousVersion = new PV( c.getManualVersion() );
|
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> entityManager.persist( c )
|
|
||||||
);
|
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
entityManager -> {
|
entityManager -> {
|
||||||
Cat _c = entityManager.find( Cat.class, c.getId() );
|
Cat c = new Cat();
|
||||||
assertNotNull( _c.getLastUpdate() );
|
c.setName( "Kitty" );
|
||||||
assertTrue( previousVersion.get() < _c.getManualVersion() );
|
c.setLength( 12 );
|
||||||
assertEquals( 12, _c.getLength() );
|
c.setDateOfBirth( new Date( 90, 11, 15 ) );
|
||||||
previousVersion.set( _c.getManualVersion() );
|
PV previousVersion = new PV( c.getManualVersion() );
|
||||||
_c.setName( "new name" );
|
entityManager.persist( c );
|
||||||
}
|
entityManager.getTransaction().commit();
|
||||||
);
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
Cat _c = entityManager.find( Cat.class, c.getId() );
|
|
||||||
assertTrue( previousVersion.get() < _c.getManualVersion() );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
scope.inTransaction(
|
entityManager.getTransaction().begin();
|
||||||
entityManager -> {
|
c = entityManager.find( Cat.class, c.getId() );
|
||||||
entityManager.createQuery( "delete from Cat" ).executeUpdate();
|
assertNotNull( c.getLastUpdate() );
|
||||||
|
assertTrue( previousVersion.get() < c.getManualVersion() );
|
||||||
|
assertEquals( 12, c.getLength() );
|
||||||
|
previousVersion.set( c.getManualVersion() );
|
||||||
|
c.setName( "new name" );
|
||||||
|
entityManager.getTransaction().commit();
|
||||||
|
entityManager.getTransaction().begin();
|
||||||
|
c = entityManager.find( Cat.class, c.getId() );
|
||||||
|
assertTrue( previousVersion.get() < c.getManualVersion() );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -135,12 +135,6 @@ public class CallbacksTest {
|
||||||
List ids = Cat.getIdList();
|
List ids = Cat.getIdList();
|
||||||
Object id = Cat.getIdList().get( ids.size() - 1 );
|
Object id = Cat.getIdList().get( ids.size() - 1 );
|
||||||
assertNotNull( id );
|
assertNotNull( id );
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Cat" ).executeUpdate();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Not a test since the spec did not make the proper change on listeners
|
//Not a test since the spec did not make the proper change on listeners
|
||||||
|
@ -173,20 +167,14 @@ public class CallbacksTest {
|
||||||
public void testPrePersistOnCascade(EntityManagerFactoryScope scope) {
|
public void testPrePersistOnCascade(EntityManagerFactoryScope scope) {
|
||||||
scope.inTransaction(
|
scope.inTransaction(
|
||||||
entityManager -> {
|
entityManager -> {
|
||||||
Television tv = new Television();
|
Television tv = new Television();
|
||||||
RemoteControl rc = new RemoteControl();
|
RemoteControl rc = new RemoteControl();
|
||||||
entityManager.persist( tv );
|
entityManager.persist( tv );
|
||||||
entityManager.flush();
|
entityManager.flush();
|
||||||
tv.setControl( rc );
|
tv.setControl( rc );
|
||||||
tv.init();
|
tv.init();
|
||||||
entityManager.flush();
|
entityManager.flush();
|
||||||
assertNotNull( rc.getCreationDate() );
|
assertNotNull( rc.getCreationDate() );
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Television" ).executeUpdate();
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -205,12 +193,6 @@ public class CallbacksTest {
|
||||||
assertTrue( tv.isLast );
|
assertTrue( tv.isLast );
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Television" ).executeUpdate();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -229,7 +211,8 @@ public class CallbacksTest {
|
||||||
}
|
}
|
||||||
catch (Exception e) {
|
catch (Exception e) {
|
||||||
fail( "should have raised an ArythmeticException:" + e.getClass() );
|
fail( "should have raised an ArythmeticException:" + e.getClass() );
|
||||||
} finally {
|
}
|
||||||
|
finally {
|
||||||
entityManager.getTransaction().rollback();
|
entityManager.getTransaction().rollback();
|
||||||
entityManager.close();
|
entityManager.close();
|
||||||
}
|
}
|
||||||
|
@ -247,81 +230,62 @@ public class CallbacksTest {
|
||||||
entityManager.flush();
|
entityManager.flush();
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Plant" ).executeUpdate();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@FailureExpected(reason = "collection change does not trigger an event", jiraKey = "EJB-288")
|
@FailureExpected(reason = "collection change does not trigger an event", jiraKey = "EJB-288")
|
||||||
public void testPostUpdateCollection(EntityManagerFactoryScope scope) {
|
public void testPostUpdateCollection(EntityManagerFactoryScope scope) {
|
||||||
// create a cat
|
scope.inEntityManager(
|
||||||
Cat cat = new Cat();
|
|
||||||
cat.setLength( 23 );
|
|
||||||
cat.setAge( 2 );
|
|
||||||
cat.setName( "Beetle" );
|
|
||||||
cat.setDateOfBirth( new Date() );
|
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
entityManager -> {
|
||||||
entityManager.persist( cat );
|
try {
|
||||||
}
|
// create a cat
|
||||||
);
|
Cat cat = new Cat();
|
||||||
|
cat.setLength( 23 );
|
||||||
|
cat.setAge( 2 );
|
||||||
|
cat.setName( "Beetle" );
|
||||||
|
cat.setDateOfBirth( new Date() );
|
||||||
|
entityManager.getTransaction().begin();
|
||||||
|
entityManager.persist( cat );
|
||||||
|
entityManager.getTransaction().commit();
|
||||||
|
// assert it is persisted
|
||||||
|
List ids = Cat.getIdList();
|
||||||
|
Object id = Cat.getIdList().get( ids.size() - 1 );
|
||||||
|
assertNotNull( id );
|
||||||
|
|
||||||
// assert it is persisted
|
// add a kitten to the cat - triggers PostCollectionRecreateEvent
|
||||||
List ids = Cat.getIdList();
|
int postVersion = Cat.postVersion;
|
||||||
Object id = Cat.getIdList().get( ids.size() - 1 );
|
entityManager.getTransaction().begin();
|
||||||
assertNotNull( id );
|
Kitten kitty = new Kitten();
|
||||||
|
kitty.setName( "kitty" );
|
||||||
|
List kittens = new ArrayList<Kitten>();
|
||||||
|
kittens.add( kitty );
|
||||||
|
cat.setKittens( kittens );
|
||||||
|
entityManager.getTransaction().commit();
|
||||||
|
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
|
||||||
|
|
||||||
// add a kitten to the cat - triggers PostCollectionRecreateEvent
|
Kitten tom = new Kitten();
|
||||||
scope.inTransaction(
|
tom.setName( "Tom" );
|
||||||
entityManager -> {
|
|
||||||
int postVersion = Cat.postVersion;
|
|
||||||
Kitten kitty = new Kitten();
|
|
||||||
kitty.setName( "kitty" );
|
|
||||||
List kittens = new ArrayList<Kitten>();
|
|
||||||
kittens.add( kitty );
|
|
||||||
cat.setKittens( kittens );
|
|
||||||
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
Kitten tom = new Kitten();
|
// add another kitten - triggers PostCollectionUpdateEvent.
|
||||||
tom.setName( "Tom" );
|
postVersion = Cat.postVersion;
|
||||||
scope.inTransaction(
|
entityManager.getTransaction().begin();
|
||||||
entityManager -> {
|
cat.getKittens().add( tom );
|
||||||
// add another kitten - triggers PostCollectionUpdateEvent.
|
entityManager.getTransaction().commit();
|
||||||
int postVersion = Cat.postVersion;
|
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
|
||||||
cat.getKittens().add( tom );
|
|
||||||
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
scope.inTransaction(
|
// delete a kitty - triggers PostCollectionUpdateEvent
|
||||||
entityManager -> {
|
postVersion = Cat.postVersion;
|
||||||
// delete a kitty - triggers PostCollectionUpdateEvent
|
entityManager.getTransaction().begin();
|
||||||
int postVersion = Cat.postVersion;
|
cat.getKittens().remove( tom );
|
||||||
cat.getKittens().remove( tom );
|
entityManager.getTransaction().commit();
|
||||||
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
|
assertEquals( postVersion + 1, Cat.postVersion, "Post version should have been incremented." );
|
||||||
}
|
}
|
||||||
);
|
catch (Exception e) {
|
||||||
|
if ( entityManager.getTransaction().isActive() ) {
|
||||||
scope.inTransaction(
|
entityManager.getTransaction().rollback();
|
||||||
entityManager -> {
|
}
|
||||||
// delete and recreate kittens - triggers PostCollectionRemoveEvent and PostCollectionRecreateEvent)
|
throw e;
|
||||||
int postVersion = Cat.postVersion;
|
}
|
||||||
cat.setKittens( new ArrayList<Kitten>() );
|
|
||||||
assertEquals( postVersion + 2, Cat.postVersion, "Post version should have been incremented." );
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
scope.inTransaction(
|
|
||||||
entityManager -> {
|
|
||||||
entityManager.createQuery( "delete from Cat" ).executeUpdate();
|
|
||||||
entityManager.createQuery( "delete from Kitten" ).executeUpdate();
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,11 +35,9 @@ public class CascadeTest {
|
||||||
student.setPrimaryTeacher( teacher );
|
student.setPrimaryTeacher( teacher );
|
||||||
|
|
||||||
entityManager.persist( teacher );
|
entityManager.persist( teacher );
|
||||||
}
|
entityManager.getTransaction().commit();
|
||||||
);
|
|
||||||
|
|
||||||
scope.inTransaction(
|
entityManager.getTransaction().begin();
|
||||||
entityManager -> {
|
|
||||||
Teacher foundTeacher = (Teacher) entityManager.createQuery( "select t from Teacher as t" )
|
Teacher foundTeacher = (Teacher) entityManager.createQuery( "select t from Teacher as t" )
|
||||||
.getSingleResult();
|
.getSingleResult();
|
||||||
|
|
||||||
|
|
|
@ -11,8 +11,9 @@ import java.lang.reflect.Method;
|
||||||
import javax.persistence.EntityManagerFactory;
|
import javax.persistence.EntityManagerFactory;
|
||||||
|
|
||||||
import org.hibernate.testing.junit4.ClassLoadingIsolater;
|
import org.hibernate.testing.junit4.ClassLoadingIsolater;
|
||||||
|
import org.hibernate.testing.orm.junit.BaseUnitTest;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
*
|
*
|
||||||
* @author Steve Ebersole
|
* @author Steve Ebersole
|
||||||
*/
|
*/
|
||||||
|
@BaseUnitTest
|
||||||
public class NoCdiAvailableTest {
|
public class NoCdiAvailableTest {
|
||||||
public static final String[] EXCLUDED_PACKAGES = new String[] {
|
public static final String[] EXCLUDED_PACKAGES = new String[] {
|
||||||
"javax.enterprise.inject.",
|
"javax.enterprise.inject.",
|
||||||
|
|
|
@ -1,38 +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.orm.test.jpa.cdi.extended;
|
|
||||||
|
|
||||||
import javax.ejb.LocalBean;
|
|
||||||
import javax.ejb.Stateful;
|
|
||||||
import javax.ejb.TransactionAttribute;
|
|
||||||
import javax.ejb.TransactionAttributeType;
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.persistence.PersistenceContextType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Vlad Mihalcea
|
|
||||||
*/
|
|
||||||
@Stateful
|
|
||||||
@LocalBean
|
|
||||||
public class ConversationalEventManager {
|
|
||||||
|
|
||||||
@PersistenceContext(type = PersistenceContextType.EXTENDED)
|
|
||||||
private EntityManager em;
|
|
||||||
|
|
||||||
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
|
|
||||||
public Event saveEvent(String eventName) {
|
|
||||||
final Event event = new Event();
|
|
||||||
event.setName( eventName );
|
|
||||||
em.persist( event );
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
@TransactionAttribute(TransactionAttributeType.REQUIRED)
|
|
||||||
public void endConversation() {
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,41 +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.orm.test.jpa.cdi.extended;
|
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.GeneratedValue;
|
|
||||||
import javax.persistence.GenerationType;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Vlad Mihalcea
|
|
||||||
*/
|
|
||||||
@Entity(name = "Event")
|
|
||||||
public class Event {
|
|
||||||
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private Long id;
|
|
||||||
|
|
||||||
private String name;
|
|
||||||
|
|
||||||
public Long getId() {
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setId(Long id) {
|
|
||||||
this.id = id;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getName() {
|
|
||||||
return name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setName(String name) {
|
|
||||||
this.name = name;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,44 +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.orm.test.jpa.cdi.extended;
|
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
|
||||||
import javax.ejb.LocalBean;
|
|
||||||
import javax.ejb.Stateful;
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.persistence.PersistenceContextType;
|
|
||||||
|
|
||||||
import org.hibernate.FlushMode;
|
|
||||||
import org.hibernate.Session;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Vlad Mihalcea
|
|
||||||
*/
|
|
||||||
@Stateful
|
|
||||||
@LocalBean
|
|
||||||
public class ManualFlushConversationalEventManager {
|
|
||||||
|
|
||||||
@PersistenceContext(type = PersistenceContextType.EXTENDED)
|
|
||||||
private EntityManager em;
|
|
||||||
|
|
||||||
@PostConstruct
|
|
||||||
public void init() {
|
|
||||||
em.unwrap( Session.class ).setHibernateFlushMode( FlushMode.MANUAL );
|
|
||||||
}
|
|
||||||
|
|
||||||
public Event saveEvent(String eventName) {
|
|
||||||
final Event event = new Event();
|
|
||||||
event.setName( eventName );
|
|
||||||
em.persist( event );
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void endConversation() {
|
|
||||||
em.flush();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,35 +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.orm.test.jpa.cdi.extended;
|
|
||||||
|
|
||||||
import javax.ejb.LocalBean;
|
|
||||||
import javax.ejb.Stateful;
|
|
||||||
import javax.persistence.EntityManager;
|
|
||||||
import javax.persistence.PersistenceContext;
|
|
||||||
import javax.persistence.PersistenceContextType;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Vlad Mihalcea
|
|
||||||
*/
|
|
||||||
@Stateful
|
|
||||||
@LocalBean
|
|
||||||
public class NonConversationalEventManager {
|
|
||||||
|
|
||||||
@PersistenceContext(type = PersistenceContextType.EXTENDED)
|
|
||||||
private EntityManager em;
|
|
||||||
|
|
||||||
public Event saveEvent(String eventName) {
|
|
||||||
final Event event = new Event();
|
|
||||||
event.setName( eventName );
|
|
||||||
em.persist( event );
|
|
||||||
return event;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void endConversation() {
|
|
||||||
em.flush();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -13,7 +13,6 @@ import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
import org.junit.jupiter.api.extension.ExtendWith;
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
import org.junit.jupiter.api.extension.ParameterContext;
|
|
||||||
|
|
||||||
@Inherited
|
@Inherited
|
||||||
@Target( ElementType.TYPE )
|
@Target( ElementType.TYPE )
|
||||||
|
|
Loading…
Reference in New Issue