HHH-2485 : merge() and collections (test case)
git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@13986 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
parent
88d04bbc4c
commit
89a8d29baf
|
@ -0,0 +1,80 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
|
||||
*
|
||||
* 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, v. 2.1. This program is distributed in the
|
||||
* hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
|
||||
* distribution; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* Red Hat Author(s): Steve Ebersole
|
||||
*/
|
||||
package org.hibernate.test.collection.set;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Container implementation
|
||||
*
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class Container {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Set contents = new HashSet();
|
||||
|
||||
public Container() {
|
||||
}
|
||||
|
||||
public Container(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 getContents() {
|
||||
return contents;
|
||||
}
|
||||
|
||||
public void setContents(Set contents) {
|
||||
this.contents = contents;
|
||||
}
|
||||
|
||||
public static class Content {
|
||||
private String name;
|
||||
|
||||
public Content() {
|
||||
}
|
||||
|
||||
public Content(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -3,7 +3,21 @@
|
|||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
||||
|
||||
|
||||
<!--
|
||||
~ Copyright (c) 2007, Red Hat Middleware, LLC. All rights reserved.
|
||||
~
|
||||
~ 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, v. 2.1. This program is distributed in the
|
||||
~ hope that it will be useful, but WITHOUT A 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, v.2.1 along with this
|
||||
~ distribution; if not, write to the Free Software Foundation, Inc.,
|
||||
~ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
~
|
||||
~ Red Hat Author(s): Steve Ebersole
|
||||
-->
|
||||
<hibernate-mapping package="org.hibernate.test.collection.set">
|
||||
|
||||
<class name="Parent">
|
||||
|
@ -20,4 +34,17 @@
|
|||
<many-to-one name="parent" class="Parent" cascade="none" />
|
||||
</class>
|
||||
|
||||
<class name="Container">
|
||||
<id name="id" type="long">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="name" column="NAME" type="string"/>
|
||||
<set name="contents" table="CONTENTS">
|
||||
<key column="CONTAINER_ID"/>
|
||||
<composite-element class="Container$Content">
|
||||
<property name="name" column="NAME" type="string"/>
|
||||
</composite-element>
|
||||
</set>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
||||
|
|
|
@ -5,9 +5,12 @@ import java.util.HashSet;
|
|||
import junit.framework.Test;
|
||||
|
||||
import org.hibernate.Session;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.cfg.Environment;
|
||||
import org.hibernate.collection.PersistentSet;
|
||||
import org.hibernate.junit.functional.FunctionalTestCase;
|
||||
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
|
||||
import org.hibernate.stat.CollectionStatistics;
|
||||
|
||||
/**
|
||||
* todo: describe PersistentSetTest
|
||||
|
@ -27,6 +30,11 @@ public class PersistentSetTest extends FunctionalTestCase {
|
|||
return new FunctionalTestClassTestSuite( PersistentSetTest.class );
|
||||
}
|
||||
|
||||
public void configure(Configuration cfg) {
|
||||
super.configure( cfg );
|
||||
cfg.setProperty( Environment.GENERATE_STATISTICS, "true" );
|
||||
}
|
||||
|
||||
public void testWriteMethodDirtying() {
|
||||
Parent parent = new Parent( "p1" );
|
||||
Child child = new Child( "c1" );
|
||||
|
@ -77,4 +85,192 @@ public class PersistentSetTest extends FunctionalTestCase {
|
|||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void testCollectionMerging() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Parent parent = new Parent( "p1" );
|
||||
Child child = new Child( "c1" );
|
||||
parent.getChildren().add( child );
|
||||
child.setParent( parent );
|
||||
session.save( parent );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
CollectionStatistics stats = sfi().getStatistics().getCollectionStatistics( Parent.class.getName() + ".children" );
|
||||
long recreateCount = stats.getRecreateCount();
|
||||
long updateCount = stats.getUpdateCount();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
parent = ( Parent ) session.merge( parent );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
assertEquals( recreateCount, stats.getRecreateCount() );
|
||||
assertEquals( updateCount, stats.getUpdateCount() );
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, "p1" );
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
session.delete( parent );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void testCollectiondirtyChecking() {
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Parent parent = new Parent( "p1" );
|
||||
Child child = new Child( "c1" );
|
||||
parent.getChildren().add( child );
|
||||
child.setParent( parent );
|
||||
session.save( parent );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
CollectionStatistics stats = sfi().getStatistics().getCollectionStatistics( Parent.class.getName() + ".children" );
|
||||
long recreateCount = stats.getRecreateCount();
|
||||
long updateCount = stats.getUpdateCount();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
parent = ( Parent ) session.get( Parent.class, "p1" );
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
assertEquals( recreateCount, stats.getRecreateCount() );
|
||||
assertEquals( updateCount, stats.getUpdateCount() );
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
assertEquals( 1, parent.getChildren().size() );
|
||||
session.delete( parent );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void testCompositeElementWriteMethodDirtying() {
|
||||
Container container = new Container( "p1" );
|
||||
Container.Content c1 = new Container.Content( "c1" );
|
||||
container.getContents().add( c1 );
|
||||
Container.Content c2 = new Container.Content( "c2" );
|
||||
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
session.save( container );
|
||||
session.flush();
|
||||
// at this point, the set on container has now been replaced with a PersistentSet...
|
||||
PersistentSet children = ( PersistentSet ) container.getContents();
|
||||
|
||||
assertFalse( children.add( c1 ) );
|
||||
assertFalse( children.isDirty() );
|
||||
|
||||
assertFalse( children.remove( c2 ) );
|
||||
assertFalse( children.isDirty() );
|
||||
|
||||
HashSet otherSet = new HashSet();
|
||||
otherSet.add( c1 );
|
||||
assertFalse( children.addAll( otherSet ) );
|
||||
assertFalse( children.isDirty() );
|
||||
|
||||
assertFalse( children.retainAll( otherSet ) );
|
||||
assertFalse( children.isDirty() );
|
||||
|
||||
otherSet = new HashSet();
|
||||
otherSet.add( c2 );
|
||||
assertFalse( children.removeAll( otherSet ) );
|
||||
assertFalse( children.isDirty() );
|
||||
|
||||
assertTrue( children.retainAll( otherSet ));
|
||||
assertTrue( children.isDirty() );
|
||||
assertTrue( children.isEmpty() );
|
||||
|
||||
children.clear();
|
||||
assertTrue( children.isDirty() );
|
||||
|
||||
session.flush();
|
||||
|
||||
children.clear();
|
||||
assertFalse( children.isDirty() );
|
||||
|
||||
session.delete( container );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void testCompositeElementMergingFailureExpected() {
|
||||
// HHH-2485
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Container container = new Container( "p1" );
|
||||
Container.Content c1 = new Container.Content( "c1" );
|
||||
container.getContents().add( c1 );
|
||||
session.save( container );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
CollectionStatistics stats = sfi().getStatistics().getCollectionStatistics( Container.class.getName() + ".contents" );
|
||||
long recreateCount = stats.getRecreateCount();
|
||||
long updateCount = stats.getUpdateCount();
|
||||
|
||||
container.setName( "another name" );
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.merge( container );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
assertEquals( recreateCount, stats.getRecreateCount() );
|
||||
assertEquals( updateCount, stats.getUpdateCount() );
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.get( Container.class, container.getId() );
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
session.delete( container );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
|
||||
public void testCompositeElementCollectionDirtyCheckingFailureExpected() {
|
||||
// HHH-2485
|
||||
Session session = openSession();
|
||||
session.beginTransaction();
|
||||
Container container = new Container( "p1" );
|
||||
Container.Content c1 = new Container.Content( "c1" );
|
||||
container.getContents().add( c1 );
|
||||
session.save( container );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
CollectionStatistics stats = sfi().getStatistics().getCollectionStatistics( Container.class.getName() + ".contents" );
|
||||
long recreateCount = stats.getRecreateCount();
|
||||
long updateCount = stats.getUpdateCount();
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.get( Container.class, container.getId() );
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
assertEquals( recreateCount, stats.getRecreateCount() );
|
||||
assertEquals( updateCount, stats.getUpdateCount() );
|
||||
|
||||
session = openSession();
|
||||
session.beginTransaction();
|
||||
container = ( Container ) session.get( Container.class, container.getId() );
|
||||
assertEquals( 1, container.getContents().size() );
|
||||
session.delete( container );
|
||||
session.getTransaction().commit();
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue