From bb53193a75a058f5def2ec0797d4e5248b999ede Mon Sep 17 00:00:00 2001 From: Brett Meyer Date: Wed, 27 Nov 2013 18:06:06 -0500 Subject: [PATCH] HHH-8749 Error flushing with a many-to-many Map defined with unique="true" and cascading orphan delete --- .../org/hibernate/engine/spi/EntityEntry.java | 2 +- .../test/orphan/manytomany/Group.java | 54 ++++++++++ .../manytomany/ManyToManyOrphanTest.java | 98 +++++++++++++++++++ .../test/orphan/manytomany/User.java | 47 +++++++++ .../test/orphan/manytomany/UserGroup.hbm.xml | 25 +++++ 5 files changed, 225 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml diff --git a/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityEntry.java b/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityEntry.java index 5c8843d20e..9b24418bb3 100644 --- a/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityEntry.java +++ b/hibernate-core/src/main/java/org/hibernate/engine/spi/EntityEntry.java @@ -282,7 +282,7 @@ public final class EntityEntry implements Serializable { } public Object getLoadedValue(String propertyName) { - if ( loadedState == null ) { + if ( loadedState == null || propertyName == null ) { return null; } else { diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java new file mode 100644 index 0000000000..e735ac0110 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/Group.java @@ -0,0 +1,54 @@ +package org.hibernate.test.orphan.manytomany; + +import java.io.Serializable; + +public class Group implements Serializable { + + private String org; + + private String name; + + private String description; + + private Integer groupType; + + public Group(String name, String org) { + this.org = org; + this.name = name; + } + + public Group() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOrg() { + return org; + } + + public void setOrg(String org) { + this.org = org; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getGroupType() { + return groupType; + } + + public void setGroupType(Integer groupType) { + this.groupType = groupType; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java new file mode 100644 index 0000000000..692cbf066f --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/ManyToManyOrphanTest.java @@ -0,0 +1,98 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2006-2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.orphan.manytomany; + +import static org.junit.Assert.assertEquals; + +import java.util.List; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +public class ManyToManyOrphanTest extends BaseCoreFunctionalTestCase { + + @Override + protected String[] getMappings() { + return new String[] { "orphan/manytomany/UserGroup.hbm.xml" }; + } + + @Test + @TestForIssue(jiraKey = "HHH-8749") + public void testManyToManyWithCascadeDeleteOrphan() { + Session s = openSession(); + Transaction t = s.beginTransaction(); + User bob = new User( "bob", "jboss" ); + Group seam = new Group( "seam", "jboss" ); + seam.setGroupType( 1 ); + Group hb = new Group( "hibernate", "jboss" ); + hb.setGroupType( 2 ); + bob.getGroups().put( seam.getGroupType(), seam ); + bob.getGroups().put( hb.getGroupType(), hb ); + s.persist( bob ); + s.persist( seam ); + s.persist( hb ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + bob = (User) s.get( User.class, "bob" ); + assertEquals( 2, bob.getGroups().size() ); + seam = (Group) s.get( Group.class, "seam" ); + assertEquals( (Integer) 1, seam.getGroupType() ); + hb = (Group) s.get( Group.class, "hibernate" ); + assertEquals( (Integer) 2, hb.getGroupType() ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + bob = (User) s.get( User.class, "bob" ); + assertEquals( 2, bob.getGroups().size() ); + hb = (Group) s.get( Group.class, "hibernate" ); + bob.getGroups().remove( hb.getGroupType() ); + assertEquals( 1, bob.getGroups().size() ); + t.commit(); + s.close(); + + s = openSession(); + t = s.beginTransaction(); + bob = (User) s.get( User.class, "bob" ); + assertEquals( 1, bob.getGroups().size() ); + t.commit(); + s.close(); + + // Verify orphan group was deleted + s = openSession(); + t = s.beginTransaction(); + List groups = s.createCriteria( Group.class ).list(); + assertEquals( 1, groups.size() ); + assertEquals( "seam", groups.get( 0 ).getName() ); + t.commit(); + s.close(); + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java new file mode 100644 index 0000000000..3898b435eb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/User.java @@ -0,0 +1,47 @@ +package org.hibernate.test.orphan.manytomany; + +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +public class User implements Serializable { + + private String org; + + private String name; + + private Map groups = new HashMap(); + + public User(String name, String org) { + this.org = org; + this.name = name; + } + + public User() { + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getOrg() { + return org; + } + + public void setOrg(String org) { + this.org = org; + } + + public Map getGroups() { + return groups; + } + + public void setGroups(Map groups) { + this.groups = groups; + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml new file mode 100644 index 0000000000..be2e9c7cc6 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/orphan/manytomany/UserGroup.hbm.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + + + + + + +