HHH-11019 - Extend DelayedPostInsertIdentifier support to include checks for FlushMode (EXTENDED PC)
Add tests to demonstrate how PersistenceContextType.EXTENDED works with the IDENTITY generator
This commit is contained in:
parent
6c005239d4
commit
731732d780
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* 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.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() {
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,68 @@
|
||||||
|
/*
|
||||||
|
* 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.cdi.extended;
|
||||||
|
|
||||||
|
import javax.ejb.EJB;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.Archive;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||||
|
import org.jboss.shrinkwrap.api.asset.StringAsset;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||||
|
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
|
||||||
|
import org.jboss.shrinkwrap.descriptor.api.persistence21.PersistenceDescriptor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class ConversationalPersistenceContextExtendedTest {
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static Archive<?> buildDeployment() {
|
||||||
|
return ShrinkWrap.create( JavaArchive.class, "test.jar" )
|
||||||
|
.addClass( Event.class )
|
||||||
|
.addClass( ConversationalEventManager.class )
|
||||||
|
.addAsManifestResource( EmptyAsset.INSTANCE, "beans.xml" )
|
||||||
|
.addAsManifestResource( new StringAsset( persistenceXml().exportAsString() ), "persistence.xml" );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PersistenceDescriptor persistenceXml() {
|
||||||
|
return Descriptors.create( PersistenceDescriptor.class )
|
||||||
|
.createPersistenceUnit().name( "pu-cdi-basic" )
|
||||||
|
.clazz( Event.class.getName() )
|
||||||
|
.excludeUnlistedClasses( true )
|
||||||
|
.nonJtaDataSource( "java:jboss/datasources/ExampleDS" )
|
||||||
|
.getOrCreateProperties().createProperty().name( "jboss.as.jpa.providerModule" ).value( "org.hibernate:5.2" ).up().up()
|
||||||
|
.getOrCreateProperties().createProperty().name( "hibernate.hbm2ddl.auto" ).value( "create-drop" ).up().up().up();
|
||||||
|
}
|
||||||
|
|
||||||
|
@EJB
|
||||||
|
private ConversationalEventManager eventManager;
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public void testIt() throws Exception {
|
||||||
|
Event event = eventManager.saveEvent( "Untold" );
|
||||||
|
assertEquals(0, ((Number) em.createNativeQuery( "select count(*) from Event" ).getSingleResult()).intValue());
|
||||||
|
eventManager.endConversation();
|
||||||
|
assertEquals(1, ((Number) em.createNativeQuery( "select count(*) from Event" ).getSingleResult()).intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,41 @@
|
||||||
|
/*
|
||||||
|
* 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.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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* 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.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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
/*
|
||||||
|
* 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.cdi.extended;
|
||||||
|
|
||||||
|
import javax.ejb.EJB;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
import org.hibernate.testing.TestForIssue;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.Archive;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||||
|
import org.jboss.shrinkwrap.api.asset.StringAsset;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||||
|
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
|
||||||
|
import org.jboss.shrinkwrap.descriptor.api.persistence21.PersistenceDescriptor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class ManualFlushConversationalPersistenceContextExtendedTest {
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static Archive<?> buildDeployment() {
|
||||||
|
return ShrinkWrap.create( JavaArchive.class, "test.jar" )
|
||||||
|
.addClass( Event.class )
|
||||||
|
.addClass( ManualFlushConversationalEventManager.class )
|
||||||
|
.addAsManifestResource( EmptyAsset.INSTANCE, "beans.xml" )
|
||||||
|
.addAsManifestResource( new StringAsset( persistenceXml().exportAsString() ), "persistence.xml" );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PersistenceDescriptor persistenceXml() {
|
||||||
|
return Descriptors.create( PersistenceDescriptor.class )
|
||||||
|
.createPersistenceUnit().name( "pu-cdi-basic" )
|
||||||
|
.clazz( Event.class.getName() )
|
||||||
|
.excludeUnlistedClasses( true )
|
||||||
|
.nonJtaDataSource( "java:jboss/datasources/ExampleDS" )
|
||||||
|
.getOrCreateProperties().createProperty().name( "jboss.as.jpa.providerModule" ).value( "org.hibernate:5.2" ).up().up()
|
||||||
|
.getOrCreateProperties().createProperty().name( "hibernate.hbm2ddl.auto" ).value( "create-drop" ).up().up().up();
|
||||||
|
}
|
||||||
|
|
||||||
|
@EJB
|
||||||
|
private ManualFlushConversationalEventManager eventManager;
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@TestForIssue( jiraKey = "HHH-11019" )
|
||||||
|
public void testIt() throws Exception {
|
||||||
|
Event event = eventManager.saveEvent( "Untold" );
|
||||||
|
//This is going to be fixed by HHH-11019 since we could rely on FlushMode to implement long conversations properly
|
||||||
|
assertEquals(1, ((Number) em.createNativeQuery( "select count(*) from Event" ).getSingleResult()).intValue());
|
||||||
|
eventManager.endConversation();
|
||||||
|
assertEquals(1, ((Number) em.createNativeQuery( "select count(*) from Event" ).getSingleResult()).intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,35 @@
|
||||||
|
/*
|
||||||
|
* 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.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();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,67 @@
|
||||||
|
/*
|
||||||
|
* 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.cdi.extended;
|
||||||
|
|
||||||
|
import javax.ejb.EJB;
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceContext;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
|
||||||
|
import org.jboss.arquillian.container.test.api.Deployment;
|
||||||
|
import org.jboss.arquillian.junit.Arquillian;
|
||||||
|
import org.jboss.shrinkwrap.api.Archive;
|
||||||
|
import org.jboss.shrinkwrap.api.ShrinkWrap;
|
||||||
|
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
|
||||||
|
import org.jboss.shrinkwrap.api.asset.StringAsset;
|
||||||
|
import org.jboss.shrinkwrap.api.spec.JavaArchive;
|
||||||
|
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
|
||||||
|
import org.jboss.shrinkwrap.descriptor.api.persistence21.PersistenceDescriptor;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Vlad Mihalcea
|
||||||
|
*/
|
||||||
|
@RunWith(Arquillian.class)
|
||||||
|
public class NonConversationalPersistenceContextExtendedTest {
|
||||||
|
|
||||||
|
@Deployment
|
||||||
|
public static Archive<?> buildDeployment() {
|
||||||
|
return ShrinkWrap.create( JavaArchive.class, "test.jar" )
|
||||||
|
.addClass( Event.class )
|
||||||
|
.addClass( NonConversationalEventManager.class )
|
||||||
|
.addAsManifestResource( EmptyAsset.INSTANCE, "beans.xml" )
|
||||||
|
.addAsManifestResource( new StringAsset( persistenceXml().exportAsString() ), "persistence.xml" );
|
||||||
|
}
|
||||||
|
|
||||||
|
private static PersistenceDescriptor persistenceXml() {
|
||||||
|
return Descriptors.create( PersistenceDescriptor.class )
|
||||||
|
.createPersistenceUnit().name( "pu-cdi-basic" )
|
||||||
|
.clazz( Event.class.getName() )
|
||||||
|
.excludeUnlistedClasses( true )
|
||||||
|
.nonJtaDataSource( "java:jboss/datasources/ExampleDS" )
|
||||||
|
.getOrCreateProperties().createProperty().name( "jboss.as.jpa.providerModule" ).value( "org.hibernate:5.2" ).up().up()
|
||||||
|
.getOrCreateProperties().createProperty().name( "hibernate.hbm2ddl.auto" ).value( "create-drop" ).up().up().up();
|
||||||
|
}
|
||||||
|
|
||||||
|
@EJB
|
||||||
|
private NonConversationalEventManager eventManager;
|
||||||
|
|
||||||
|
@PersistenceContext
|
||||||
|
private EntityManager em;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIt() throws Exception {
|
||||||
|
Event event = eventManager.saveEvent( "Untold" );
|
||||||
|
assertEquals(1, ((Number) em.createNativeQuery( "select count(*) from Event" ).getSingleResult()).intValue());
|
||||||
|
eventManager.endConversation();
|
||||||
|
assertEquals(1, ((Number) em.createNativeQuery( "select count(*) from Event" ).getSingleResult()).intValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue