From 43402ea51c319cd037c94d858711bf2082dc2e24 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Thu, 17 Oct 2019 13:52:35 +0100 Subject: [PATCH] HHH-13672 Add test for issue --- ...StatelessSessionPersistentContextTest.java | 187 ++++++++++++++++++ .../test/stateless/StatelessSessionTest.java | 12 +- 2 files changed, 193 insertions(+), 6 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionPersistentContextTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionPersistentContextTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionPersistentContextTest.java new file mode 100644 index 0000000000..ed007f2ad8 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionPersistentContextTest.java @@ -0,0 +1,187 @@ +/* + * 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.test.stateless; + +import java.util.function.Consumer; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.Table; + +import org.hibernate.StatelessSession; +import org.hibernate.Transaction; +import org.hibernate.engine.spi.PersistenceContext; +import org.hibernate.engine.spi.SharedSessionContractImplementor; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertTrue; + +/** + * @author Andrea Boriero + */ +public class StatelessSessionPersistentContextTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { TestEntity.class, OtherEntity.class }; + } + + @Test + @TestForIssue(jiraKey = "HHH-13672") + public void testStatelessSessionPersistenceContextIsCleared() { + TestEntity testEntity = new TestEntity(); + consumeAndCheckPersistenceContextIsClosed( + statelessSession -> { + testEntity.setName( "Fab" ); + OtherEntity otherEntity = new OtherEntity(); + otherEntity.setName( "other" ); + testEntity.setOtherEntity( otherEntity ); + statelessSession.insert( otherEntity ); + statelessSession.insert( testEntity ); + } + ); + + consumeAndCheckPersistenceContextIsClosed( + statelessSession -> { + statelessSession.get( TestEntity.class, testEntity.getId() ); + } + ); + + consumeAndCheckPersistenceContextIsClosed( + statelessSession -> { + TestEntity p2 = (TestEntity) statelessSession.get( TestEntity.class, testEntity.getId() ); + p2.setName( "Fabulous" ); + statelessSession.update( p2 ); + } + ); + + consumeAndCheckPersistenceContextIsClosed( + statelessSession -> { + TestEntity testEntity1 = (TestEntity) statelessSession.createQuery( + "select p from TestEntity p where id = :id" ) + .setParameter( "id", testEntity.getId() ) + .uniqueResult(); + testEntity1.getOtherEntity(); + } + ); + + consumeAndCheckPersistenceContextIsClosed( + statelessSession -> { + statelessSession.refresh( testEntity ); + + } + ); + + consumeAndCheckPersistenceContextIsClosed( + statelessSession -> { + statelessSession.delete( testEntity ); + + } + ); + } + + private void consumeAndCheckPersistenceContextIsClosed(Consumer consumer) { + Transaction transaction = null; + StatelessSession statelessSession = sessionFactory().openStatelessSession(); + try { + transaction = statelessSession.beginTransaction(); + consumer.accept( statelessSession ); + transaction.commit(); + } + catch (Exception e) { + if ( transaction != null && transaction.isActive() ) { + transaction.rollback(); + } + throw e; + } + finally { + statelessSession.close(); + } + assertThatPersistenContextIsCleared( statelessSession ); + } + + private void assertThatPersistenContextIsCleared(StatelessSession ss) { + PersistenceContext persistenceContextInternal = ( (SharedSessionContractImplementor) ss ).getPersistenceContextInternal(); + assertTrue( + "StatelessSession: PersistenceContext has not been cleared", + persistenceContextInternal.getEntitiesByKey().isEmpty() + ); + assertTrue( + "StatelessSession: PersistenceContext has not been cleared", + persistenceContextInternal.getCollectionsByKey().isEmpty() + ); + } + + @Entity(name = "TestEntity") + @Table(name = "TestEntity") + public static class TestEntity { + @Id + @GeneratedValue + private Long id; + + private String name; + + @ManyToOne + @JoinColumn + private OtherEntity otherEntity; + + 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; + } + + public OtherEntity getOtherEntity() { + return otherEntity; + } + + public void setOtherEntity(OtherEntity otherEntity) { + this.otherEntity = otherEntity; + } + } + + @Entity(name = "OtherEntity") + @Table(name = "OtherEntity") + public static class OtherEntity { + @Id + @GeneratedValue + 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; + } + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java index 7f555eb4ab..d9a18cb2b1 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/stateless/StatelessSessionTest.java @@ -50,15 +50,15 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase { doc.setText("blah blah blah .... blah blay"); ss.update(doc); tx.commit(); - + Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs"); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); - + doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); - + ScrollableResults sr = ss.createQuery("from Document where text is not null") .scroll(ScrollMode.FORWARD_ONLY); sr.next(); @@ -66,17 +66,17 @@ public class StatelessSessionTest extends BaseCoreFunctionalTestCase { sr.close(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); - + doc2 = (Document) ss.createSQLQuery("select * from Document") .addEntity(Document.class) .uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); - + doc2 = (Document) ss.createCriteria(Document.class).uniqueResult(); assertEquals("Blahs", doc2.getName()); assertEquals(doc.getText(), doc2.getText()); - + sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY); sr.next(); doc2 = (Document) sr.get(0);