From 63e4702629bf1258edc36ef307ac46cde395bded Mon Sep 17 00:00:00 2001 From: lrcuhuh Date: Thu, 16 Jul 2015 14:15:23 +0200 Subject: [PATCH] HHH-9933 - Uninitialized PersistentSet with lazy="extra" doesn't respect added elements --- .../collection/internal/PersistentSet.java | 4 +- .../ExtraLazyCollectionConsistencyTest.java | 130 ++++++++++++++++++ .../test/extralazy/ExtraLazyTest.java | 7 +- 3 files changed, 137 insertions(+), 4 deletions(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyCollectionConsistencyTest.java diff --git a/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentSet.java b/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentSet.java index f41d5a94c2..317c6f0dac 100644 --- a/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentSet.java +++ b/hibernate-core/src/main/java/org/hibernate/collection/internal/PersistentSet.java @@ -329,8 +329,8 @@ public class PersistentSet extends AbstractPersistentCollection implements java. public boolean endRead() { set.addAll( tempList ); tempList = null; - setInitialized(); - return true; + // ensure that operationQueue is considered + return super.endRead(); } @Override diff --git a/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyCollectionConsistencyTest.java b/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyCollectionConsistencyTest.java new file mode 100644 index 0000000000..4d992f32e5 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyCollectionConsistencyTest.java @@ -0,0 +1,130 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.extralazy; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * @author Vlad Mihalcea + */ +public class ExtraLazyCollectionConsistencyTest extends BaseCoreFunctionalTestCase { + + private User user; + + @Override + public String[] getMappings() { + return new String[] { "extralazy/UserGroup.hbm.xml","extralazy/Parent.hbm.xml","extralazy/Child.hbm.xml" }; + } + + @Override + protected void prepareTest() { + doInHibernate( this::sessionFactory, session -> { + user = new User("victor", "hugo"); + session.persist(user); + }); + } + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetSize() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + Document document = new Document("Les Miserables", "sad", _user); + assertEquals(1, _user.getDocuments().size()); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetIterator() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + Document document = new Document("Les Miserables", "sad", _user); + assertTrue(_user.getDocuments().iterator().hasNext()); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetIsEmpty() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + Document document = new Document("Les Miserables", "sad", _user); + assertFalse(_user.getDocuments().isEmpty()); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetContains() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + Document document = new Document("Les Miserables", "sad", _user); + assertTrue(_user.getDocuments().contains(document)); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetAdd() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + Document document = new Document(); + document.setTitle("Les Miserables"); + document.setContent("sad"); + document.setOwner(_user); + assertTrue("not added", _user.getDocuments().add(document)); + assertFalse("added", _user.getDocuments().add(document)); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetRemove() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + + Document document = new Document("Les Miserables", "sad", _user); + assertTrue("not removed", _user.getDocuments().remove(document)); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetToArray() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + + Document document = new Document("Les Miserables", "sad", _user); + assertEquals(1, _user.getDocuments().toArray().length); + }); + } + + @Test + @TestForIssue(jiraKey = "HHH-9933") + public void testSetToArrayTyped() { + doInHibernate( this::sessionFactory, session -> { + User _user = session.get(User.class, user.getName()); + + Document document = new Document("Les Miserables", "sad", _user); + assertEquals(1, _user.getDocuments().toArray(new Document[0]).length); + }); + } +} + diff --git a/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyTest.java b/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyTest.java index cd621bd067..258b99d75b 100755 --- a/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyTest.java +++ b/hibernate-core/src/test/java/org/hibernate/test/extralazy/ExtraLazyTest.java @@ -18,8 +18,11 @@ import org.hibernate.testing.FailureExpected; import org.hibernate.testing.RequiresDialectFeature; import org.hibernate.testing.TestForIssue; import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.hibernate.test.naturalid.inheritance.cache.ExtendedEntity; +import org.hibernate.test.naturalid.inheritance.cache.MyEntity; import org.junit.Test; +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -34,7 +37,7 @@ public class ExtraLazyTest extends BaseCoreFunctionalTestCase { public String[] getMappings() { return new String[] { "extralazy/UserGroup.hbm.xml","extralazy/Parent.hbm.xml","extralazy/Child.hbm.xml" }; } - + @Override protected Class[] getAnnotatedClasses() { return new Class[] { School.class, Student.class, Championship.class }; @@ -255,7 +258,7 @@ public class ExtraLazyTest extends BaseCoreFunctionalTestCase { assertNotNull(child2); session2.close(); } - + @Test @TestForIssue(jiraKey = "HHH-10874") public void testWhereClauseOnBidirectionalCollection() {