HHH-8749 Error flushing with a many-to-many Map defined with

unique="true" and cascading orphan delete
This commit is contained in:
Brett Meyer 2013-11-27 18:06:06 -05:00
parent 73f0e7b29d
commit bb53193a75
5 changed files with 225 additions and 1 deletions

View File

@ -282,7 +282,7 @@ public final class EntityEntry implements Serializable {
} }
public Object getLoadedValue(String propertyName) { public Object getLoadedValue(String propertyName) {
if ( loadedState == null ) { if ( loadedState == null || propertyName == null ) {
return null; return null;
} }
else { else {

View File

@ -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;
}
}

View File

@ -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<Group> groups = s.createCriteria( Group.class ).list();
assertEquals( 1, groups.size() );
assertEquals( "seam", groups.get( 0 ).getName() );
t.commit();
s.close();
}
}

View File

@ -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<Integer, Group> groups = new HashMap<Integer, Group>();
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<Integer, Group> getGroups() {
return groups;
}
public void setGroups(Map<Integer, Group> groups) {
this.groups = groups;
}
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.hibernate.test.orphan.manytomany">
<class name="User" table="`User`">
<id name="name" length="32" />
<map name="groups" table="UserGroup" cascade="all,delete-orphan">
<key column="name" />
<map-key column="groupType" type="integer" />
<many-to-many class="Group" unique="true">
<column name="groupName" />
</many-to-many>
</map>
</class>
<class name="Group" table="`Group`">
<id name="name" length="32" />
<property name="description" />
<property name="groupType" type="integer" />
</class>
</hibernate-mapping>