HHH-2801 : added tests for removing and adding equal element to many-to-many with association class

git-svn-id: https://svn.jboss.org/repos/hibernate/core/branches/Branch_3_2@14345 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2008-02-20 17:57:19 +00:00
parent 19c8fc3587
commit b434e67bb8
13 changed files with 1155 additions and 0 deletions

View File

@ -87,6 +87,7 @@ import org.hibernate.test.legacy.SQLLoaderTest;
import org.hibernate.test.legacy.StatisticsTest;
import org.hibernate.test.lob.LobSuite;
import org.hibernate.test.manytomany.ManyToManyTest;
import org.hibernate.test.manytomanyassociationclass.ManyToManyAssociationClassSuite;
import org.hibernate.test.map.MapIndexFormulaTest;
import org.hibernate.test.mapcompelem.MapCompositeElementTest;
import org.hibernate.test.mapelemformula.MapElementFormulaTest;
@ -230,6 +231,7 @@ public class AllTests {
suite.addTest( MixedTest.suite() );
suite.addTest( OneToManyTest.suite() );
suite.addTest( ManyToManyTest.suite() );
suite.addTest( ManyToManyAssociationClassSuite.suite() );
suite.addTest( OneToOneSuite.suite() );
suite.addTest( OptimisticLockTest.suite() );
suite.addTest( PropertyRefSuite.suite() );

View File

@ -0,0 +1,271 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass;
import java.util.HashSet;
import org.hibernate.Session;
import org.hibernate.junit.functional.FunctionalTestCase;
/**
* Abstract class for tests on many-to-many association using an association class.
*
* @author Gail Badner
*/
public abstract class AbstractManyToManyAssociationClassTest extends FunctionalTestCase {
private User user;
private Group group;
private Membership membership;
public AbstractManyToManyAssociationClassTest(String string) {
super( string );
}
public abstract String[] getMappings();
public abstract Membership createMembership(String name);
protected void prepareTest() {
Session s = openSession();
s.beginTransaction();
user = new User( "user" );
group = new Group( "group" );
s.save( user );
s.save( group );
membership = createMembership( "membership");
addMembership( user, group, membership );
s.getTransaction().commit();
s.close();
}
protected void cleanupTest() {
if ( getSessions() != null ) {
Session s = openSession();
s.beginTransaction();
s.createQuery( "delete from " + membership.getClass().getName() );
s.createQuery( "delete from User" );
s.createQuery( "delete from Group" );
s.getTransaction().commit();
s.close();
}
}
public User getUser() {
return user;
}
public Group getGroup() {
return group;
}
public Membership getMembership() {
return membership;
}
public void testRemoveAndAddSameElement() {
deleteMembership( user, group, membership );
addMembership( user, group, membership );
Session s = openSession();
s.beginTransaction();
s.merge( user );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
user = ( User ) s.get( User.class, user.getId() );
group = ( Group ) s.get( Group.class, group.getId() );
membership = ( Membership ) s.get( membership.getClass(), membership.getId() );
assertEquals( "user", user.getName() );
assertEquals( "group", group.getName() );
assertEquals( "membership", membership.getName() );
assertEquals( 1, user.getMemberships().size() );
assertEquals( 1, group.getMemberships().size() );
assertSame( membership, user.getMemberships().iterator().next() );
assertSame( membership, group.getMemberships().iterator().next() );
assertSame( user, membership.getUser() );
assertSame( group, membership.getGroup() );
s.getTransaction().commit();
s.close();
}
public void testRemoveAndAddEqualElement() {
deleteMembership( user, group, membership );
membership = createMembership( "membership" );
addMembership( user, group, membership );
Session s = openSession();
s.beginTransaction();
s.merge( user );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
user = ( User ) s.get( User.class, user.getId() );
group = ( Group ) s.get( Group.class, group.getId() );
membership = ( Membership ) s.get( membership.getClass(), membership.getId() );
assertEquals( "user", user.getName() );
assertEquals( "group", group.getName() );
assertEquals( "membership", membership.getName() );
assertEquals( 1, user.getMemberships().size() );
assertEquals( 1, group.getMemberships().size() );
assertSame( membership, user.getMemberships().iterator().next() );
assertSame( membership, group.getMemberships().iterator().next() );
assertSame( user, membership.getUser() );
assertSame( group, membership.getGroup() );
s.getTransaction().commit();
s.close();
}
public void testRemoveAndAddEqualCollection() {
deleteMembership( user, group, membership );
membership = createMembership( "membership" );
user.setMemberships( new HashSet() );
group.setMemberships( new HashSet() );
addMembership( user, group, membership );
Session s = openSession();
s.beginTransaction();
s.merge( user );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
user = ( User ) s.get( User.class, user.getId() );
group = ( Group ) s.get( Group.class, group.getId() );
membership = ( Membership ) s.get( membership.getClass(), membership.getId() );
assertEquals( "user", user.getName() );
assertEquals( "group", group.getName() );
assertEquals( "membership", membership.getName() );
assertEquals( 1, user.getMemberships().size() );
assertEquals( 1, group.getMemberships().size() );
assertSame( membership, user.getMemberships().iterator().next() );
assertSame( membership, group.getMemberships().iterator().next() );
assertSame( user, membership.getUser() );
assertSame( group, membership.getGroup() );
s.getTransaction().commit();
s.close();
}
public void testRemoveAndAddSameElementNonKeyModified() {
deleteMembership( user, group, membership );
addMembership( user, group, membership );
membership.setName( "membership1" );
Session s = openSession();
s.beginTransaction();
s.merge( user );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
user = ( User ) s.get( User.class, user.getId() );
group = ( Group ) s.get( Group.class, group.getId() );
membership = ( Membership ) s.get( membership.getClass(), membership.getId() );
assertEquals( "user", user.getName() );
assertEquals( "group", group.getName() );
assertEquals( "membership1", membership.getName() );
assertEquals( 1, user.getMemberships().size() );
assertEquals( 1, group.getMemberships().size() );
assertSame( membership, user.getMemberships().iterator().next() );
assertSame( membership, group.getMemberships().iterator().next() );
assertSame( user, membership.getUser() );
assertSame( group, membership.getGroup() );
s.getTransaction().commit();
s.close();
}
public void testRemoveAndAddEqualElementNonKeyModified() {
deleteMembership( user, group, membership );
membership = createMembership( "membership" );
addMembership( user, group, membership );
membership.setName( "membership1" );
Session s = openSession();
s.beginTransaction();
s.merge( user );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
user = ( User ) s.get( User.class, user.getId() );
group = ( Group ) s.get( Group.class, group.getId() );
membership = ( Membership ) s.get( membership.getClass(), membership.getId() );
assertEquals( "user", user.getName() );
assertEquals( "group", group.getName() );
assertEquals( "membership1", membership.getName() );
assertEquals( 1, user.getMemberships().size() );
assertEquals( 1, group.getMemberships().size() );
assertSame( membership, user.getMemberships().iterator().next() );
assertSame( membership, group.getMemberships().iterator().next() );
assertSame( user, membership.getUser() );
assertSame( group, membership.getGroup() );
s.getTransaction().commit();
s.close();
}
public void testDeleteDetached() {
Session s = openSession();
s.beginTransaction();
s.delete( user );
s.delete( group );
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
assertNull( s.get( User.class, user.getId() ) );
assertNull( s.get( Group.class , group.getId() ) );
assertNull( s.get( membership.getClass(), membership.getId() ) );
s.getTransaction().commit();
s.close();
}
public void deleteMembership(User u, Group g, Membership ug) {
if ( u == null || g == null ) {
throw new IllegalArgumentException();
}
u.getMemberships().remove( ug );
g.getMemberships().remove( ug );
ug.setUser( null );
ug.setGroup( null );
}
public void addMembership(User u, Group g, Membership ug) {
if ( u == null || g == null ) {
throw new IllegalArgumentException();
}
ug.setUser( u );
ug.setGroup( g );
u.getMemberships().add( ug );
g.getMemberships().add( ug );
}
}

View File

@ -0,0 +1,90 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass;
import java.util.HashSet;
import java.util.Set;
/**
* @author Gail Badner
*/
public class Group {
private Long id;
private String name;
private Set memberships = new HashSet();
public Group() {
}
public Group(String name) {
this.name = 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;
}
public Set getMemberships() {
return memberships;
}
public void setMemberships(Set memberships) {
this.memberships = memberships;
}
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj instanceof Group ) {
Group grp = ( Group ) obj;
if ( grp.getName() != null && name != null ) {
return grp.getName().equals( name );
}
else {
return super.equals( obj );
}
}
else {
return false;
}
}
public int hashCode() {
return ( name == null ? super.hashCode() : name.hashCode() );
}
}

View File

@ -0,0 +1,48 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.hibernate.test.manytomanyassociationclass.compositeid.ManyToManyAssociationClassCompositeIdTest;
import org.hibernate.test.manytomanyassociationclass.surrogateid.assigned.ManyToManyAssociationClassAssignedIdTest;
import org.hibernate.test.manytomanyassociationclass.surrogateid.generated.ManyToManyAssociationClassGeneratedIdTest;
/**
* Tests on many-to-many association using an association class with a composite ID containing
* the IDs from the associated entities.
*
* @author Gail Badner
*/
public class ManyToManyAssociationClassSuite {
public static Test suite() {
TestSuite suite = new TestSuite( "Many-to-many with associaiton class tests" );
suite.addTest( ManyToManyAssociationClassCompositeIdTest.suite() );
suite.addTest( ManyToManyAssociationClassAssignedIdTest.suite() );
suite.addTest( ManyToManyAssociationClassGeneratedIdTest.suite() );
return suite;
}
}

View File

@ -0,0 +1,109 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass;
import java.io.Serializable;
/**
* Models a user's membership in a group.
*
* @author Gail Badner
*/
public class Membership {
private Serializable id;
private String name;
private User user;
private Group group;
public Membership() {
}
public Membership(Serializable id) {
this.id = id;
}
public Membership(String name) {
this.name = name;
}
public Membership(Serializable id, String name) {
this.id = id;
this.name = name;
}
public Serializable getId() {
return id;
}
public void setId(Serializable id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj instanceof Membership ) {
Membership mem = ( Membership ) obj;
if ( mem.getName() != null && name != null ) {
return mem.getName().equals( name );
}
else {
return super.equals( obj );
}
}
else {
return false;
}
}
public int hashCode() {
return ( name == null ? super.hashCode() : name.hashCode() );
}
}

View File

@ -0,0 +1,95 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass;
import java.util.HashSet;
import java.util.Set;
/**
* @author Gail Badner
*/
public class User {
private Long id;
private String name;
private Set memberships = new HashSet();
public User() {
}
public User(String name) {
this.name = 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;
}
public Set getMemberships() {
return memberships;
}
public void setMemberships(Set memberships) {
this.memberships = memberships;
}
public void addGroupMembership(Group group) {
if ( group == null ) {
throw new IllegalArgumentException( "group cannot be null" );
}
}
public boolean equals(Object obj) {
if ( this == obj ) {
return true;
}
if ( obj instanceof User ) {
User user = ( User ) obj;
if ( user.getName() != null && name != null ) {
return user.getName().equals( name );
}
else {
return super.equals( obj );
}
}
else {
return false;
}
}
public int hashCode() {
return ( name == null ? super.hashCode() : name.hashCode() );
}
}

View File

@ -0,0 +1,55 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass.compositeid;
import junit.framework.Test;
import org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest;
import org.hibernate.test.manytomanyassociationclass.Membership;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
/**
* Tests on many-to-many association using an association class with a composite ID containing
* the IDs from the associated entities.
*
* @author Gail Badner
*/
public class ManyToManyAssociationClassCompositeIdTest extends AbstractManyToManyAssociationClassTest {
public ManyToManyAssociationClassCompositeIdTest(String string) {
super( string );
}
public String[] getMappings() {
return new String[] { "manytomanyassociationclass/compositeid/Mappings.hbm.xml" };
}
public static Test suite() {
return new FunctionalTestClassTestSuite( ManyToManyAssociationClassCompositeIdTest.class );
}
public Membership createMembership( String name ) {
return new MembershipWithCompositeId( name );
}
}

View File

@ -0,0 +1,68 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
~
~ 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
~
-->
<hibernate-mapping package="org.hibernate.test.manytomanyassociationclass.compositeid">
<class name="org.hibernate.test.manytomanyassociationclass.User" table="HB_USER">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<set name="memberships" cascade="all, delete-orphan" inverse="true" lazy="true">
<key column="USER_ID"/>
<one-to-many class="MembershipWithCompositeId"/>
</set>
</class>
<class name="org.hibernate.test.manytomanyassociationclass.Group" table="HB_GROUP">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<set name="memberships" cascade="all, delete-orphan" inverse="true" lazy="true">
<key column="GROUP_ID"/>
<one-to-many class="MembershipWithCompositeId"/>
</set>
</class>
<class name="MembershipWithCompositeId" table="HB_MEMBERSHIP">
<composite-id name="id"
class="MembershipWithCompositeId$Id">
<key-property name="userId" type="long" column="USER_ID"/>
<key-property name="groupId" type="long" column="GROUP_ID"/>
</composite-id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<many-to-one column="USER_ID" name="user" class="org.hibernate.test.manytomanyassociationclass.User"
not-null="true" unique-key="UK_MEMBERSHIP"
insert="false" update="false"/>
<many-to-one column="GROUP_ID" name="group" class="org.hibernate.test.manytomanyassociationclass.Group"
not-null="true" unique-key="UK_MEMBERSHIP"
insert="false" update="false"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,101 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass.compositeid;
import java.io.Serializable;
import org.hibernate.test.manytomanyassociationclass.Group;
import org.hibernate.test.manytomanyassociationclass.Membership;
import org.hibernate.test.manytomanyassociationclass.User;
/**
* Models a user's membership in a group.
*
* @author Gail Badner
*/
public class MembershipWithCompositeId extends Membership {
public static class Id implements Serializable {
private Long userId;
private Long groupId;
public Id() {
}
public Id(Long userId, Long groupId) {
this.userId = userId;
this.groupId = groupId;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public boolean equals(Object o) {
if ( o != null && o instanceof Id ) {
Id that = ( Id ) o;
return this.userId.equals( that.userId ) &&
this.groupId.equals( that.groupId );
}
else {
return false;
}
}
public int hashCode() {
return userId.hashCode() + groupId.hashCode();
}
}
public MembershipWithCompositeId() {
super( new Id() );
}
public MembershipWithCompositeId(String name) {
super( new Id(), name );
}
public void setGroup(Group group) {
( (Id) getId() ).setGroupId( ( group == null ? null : group.getId() ) );
super.setGroup( group );
}
public void setUser(User user) {
( (Id) getId() ).setUserId( user == null ? null : user.getId() );
super.setUser( user );
}
}

View File

@ -0,0 +1,54 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass.surrogateid.assigned;
import junit.framework.Test;
import org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest;
import org.hibernate.test.manytomanyassociationclass.Membership;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
/**
* Tests on many-to-many association using an association class with a surrogate ID that is assigned.
*
* @author Gail Badner
*/
public class ManyToManyAssociationClassAssignedIdTest extends AbstractManyToManyAssociationClassTest {
public ManyToManyAssociationClassAssignedIdTest(String string) {
super( string );
}
public String[] getMappings() {
return new String[] { "manytomanyassociationclass/surrogateid/assigned/Mappings.hbm.xml" };
}
public static Test suite() {
return new FunctionalTestClassTestSuite( ManyToManyAssociationClassAssignedIdTest.class );
}
public Membership createMembership(String name) {
return new Membership( new Long( 1000 ), name );
}
}

View File

@ -0,0 +1,62 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
~
~ 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
~
-->
<hibernate-mapping package="org.hibernate.test.manytomanyassociationclass">
<class name="User" table="HB_USER">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<set name="memberships" cascade="all, delete-orphan" inverse="true" lazy="true">
<key column="USER_ID"/>
<one-to-many class="Membership"/>
</set>
</class>
<class name="Group" table="HB_GROUP">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<set name="memberships" cascade="all, delete-orphan" inverse="true" lazy="true">
<key column="GROUP_ID"/>
<one-to-many class="Membership"/>
</set>
</class>
<class name="Membership" table="HB_MEMBERSHIP">
<id name="id" column="ID" type="long">
<generator class="assigned"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<many-to-one column="USER_ID" name="user" class="User" not-null="true" unique-key="UK_MEMBERSHIP"/>
<many-to-one column="GROUP_ID" name="group" class="Group" not-null="true" unique-key="UK_MEMBERSHIP"/>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,134 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
*
* 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.manytomanyassociationclass.surrogateid.generated;
import java.util.HashSet;
import junit.framework.Test;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest;
import org.hibernate.test.manytomanyassociationclass.Membership;
import org.hibernate.Session;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
/**
* Tests on many-to-many association using an association class with a surrogate ID that is generated.
*
* @author Gail Badner
*/
public class ManyToManyAssociationClassGeneratedIdTest extends AbstractManyToManyAssociationClassTest {
public ManyToManyAssociationClassGeneratedIdTest(String string) {
super( string );
}
public String[] getMappings() {
return new String[] { "manytomanyassociationclass/surrogateid/generated/Mappings.hbm.xml" };
}
public static Test suite() {
return new FunctionalTestClassTestSuite( ManyToManyAssociationClassGeneratedIdTest.class );
}
public Membership createMembership(String name) {
return new Membership( name );
}
public void testRemoveAndAddEqualElement() {
deleteMembership( getUser(), getGroup(), getMembership() );
addMembership( getUser(), getGroup(), createMembership( "membership" ) );
Session s = openSession();
s.beginTransaction();
try {
// The new membership is transient (it has a null surrogate ID), so
// Hibernate assumes that it should be added to the collection.
// Inserts are done before deletes, so a ConstraintViolationException
// will be thrown on the insert because the unique constraint on the
// user and group IDs in the join table is violated. See HHH-2801.
s.merge( getUser() );
fail( "should have failed because inserts are before deletes");
}
catch( ConstraintViolationException ex ) {
// expected
s.getTransaction().rollback();
}
finally {
s.close();
}
}
public void testRemoveAndAddEqualCollection() {
deleteMembership( getUser(), getGroup(), getMembership() );
getUser().setMemberships( new HashSet() );
getGroup().setMemberships( new HashSet() );
addMembership( getUser(), getGroup(), createMembership( "membership" ) );
Session s = openSession();
s.beginTransaction();
try {
// The new membership is transient (it has a null surrogate ID), so
// Hibernate assumes that it should be added to the collection.
// Inserts are done before deletes, so a ConstraintViolationException
// will be thrown on the insert because the unique constraint on the
// user and group IDs in the join table is violated. See HHH-2801.
s.merge( getUser() );
fail( "should have failed because inserts are before deletes");
}
catch( ConstraintViolationException ex ) {
// expected
s.getTransaction().rollback();
}
finally {
s.close();
}
}
public void testRemoveAndAddEqualElementNonKeyModified() {
deleteMembership( getUser(), getGroup(), getMembership() );
Membership membershipNew = createMembership( "membership" );
addMembership( getUser(), getGroup(), membershipNew );
membershipNew.setName( "membership1" );
Session s = openSession();
s.beginTransaction();
try {
// The new membership is transient (it has a null surrogate ID), so
// Hibernate assumes that it should be added to the collection.
// Inserts are done before deletes, so a ConstraintViolationException
// will be thrown on the insert because the unique constraint on the
// user and group IDs in the join table is violated. See HHH-2801.
s.merge( getUser() );
fail( "should have failed because inserts are before deletes");
}
catch( ConstraintViolationException ex ) {
// expected
s.getTransaction().rollback();
}
finally {
s.close();
}
}
}

View File

@ -0,0 +1,66 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ Copyright (c) 2008, Red Hat Middleware LLC 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 Middleware LLC.
~
~ 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
~
-->
<hibernate-mapping package="org.hibernate.test.manytomanyassociationclass">
<class name="User" table="HB_USER">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<set name="memberships" cascade="all, delete-orphan" inverse="true" lazy="true">
<key column="USER_ID"/>
<one-to-many class="Membership"/>
</set>
</class>
<class name="Group" table="HB_GROUP">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<set name="memberships" cascade="all, delete-orphan" inverse="true" lazy="true">
<key column="GROUP_ID"/>
<one-to-many class="Membership"/>
</set>
</class>
<class name="Membership" table="HB_MEMBERSHIP">
<id name="id" column="ID" type="long">
<generator class="native"/>
</id>
<property name="name" column="NAME" type="string" length="40" not-null="true"/>
<many-to-one column="USER_ID" name="user"
class="User"
not-null="true" unique-key="UK_MEMBERSHIP"/>
<many-to-one column="GROUP_ID" name="group"
class="Group"
not-null="true" unique-key="UK_MEMBERSHIP"/>
</class>
</hibernate-mapping>