HHH-3799 : Added test for PersistentSet does not honor hashcode/equals contract when loaded eagerly

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19461 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Gail Badner 2010-05-11 00:49:20 +00:00
parent 36a2ef2699
commit 8749ae600a
5 changed files with 260 additions and 0 deletions

View File

@ -8,6 +8,7 @@ package org.hibernate.test.collection.set;
public class Child {
private String name;
private Parent parent;
private String description;
public Child() {
}
@ -31,4 +32,38 @@ public class Child {
public void setParent(Parent parent) {
this.parent = parent;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public boolean equals(Object o) {
if ( this == o ) {
return true;
}
if ( o == null || getClass() != o.getClass() ) {
return false;
}
Child child = ( Child ) o;
if ( description != null ? ! description.equals( child.description ) : child.description != null ) {
return false;
}
if ( ! name.equals( child.name ) ) {
return false;
}
return true;
}
public int hashCode() {
int result = name.hashCode();
result = 31 * result + ( description != null ? description.hashCode() : 0 );
return result;
}
}

View File

@ -32,6 +32,7 @@
<class name="Child">
<id name="name" column="NAME" type="string"/>
<many-to-one name="parent" class="Parent" cascade="none" />
<property name="description" type="string"/>
</class>
<class name="Container">

View File

@ -0,0 +1,51 @@
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//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">
<id name="name" column="NAME" type="string" />
<set name="children" inverse="true" cascade="all" lazy="false">
<key column="PARENT" />
<one-to-many class="Child" />
</set>
</class>
<class name="Child">
<id name="name" column="NAME" type="string"/>
<many-to-one name="parent" class="Parent" cascade="none" lazy="false" />
<property name="description" type="string"/>
</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" lazy="false">
<key column="CONTAINER_ID"/>
<composite-element class="Container$Content">
<property name="name" column="NAME" type="string"/>
</composite-element>
</set>
</class>
</hibernate-mapping>

View File

@ -0,0 +1,65 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2010, 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.collection.set;
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;
/**
*
* @author Gail Badner
*/
public class PersistentSetNonLazyTest extends PersistentSetTest {
public PersistentSetNonLazyTest(String name) {
super( name );
}
public String[] getMappings() {
return new String[] { "collection/set/MappingsNonLazy.hbm.xml" };
}
public static Test suite() {
return new FunctionalTestClassTestSuite( PersistentSetNonLazyTest.class );
}
public void testLoadChildCheckParentContainsChildCache() {
reportSkip( "known to fail with non-lazy collection using query cache",
"support for non-lazy collections using query cache"
);
}
public void testLoadChildCheckParentContainsChildCacheFailureExpected() {
super.testLoadChildCheckParentContainsChildCache();
}
}

View File

@ -4,10 +4,12 @@ import java.util.HashSet;
import junit.framework.Test;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.collection.PersistentSet;
import org.hibernate.criterion.Restrictions;
import org.hibernate.junit.functional.FunctionalTestCase;
import org.hibernate.junit.functional.FunctionalTestClassTestSuite;
import org.hibernate.stat.CollectionStatistics;
@ -273,4 +275,110 @@ public class PersistentSetTest extends FunctionalTestCase {
session.getTransaction().commit();
session.close();
}
public void testLoadChildCheckParentContainsChildCache() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
child.setDescription( "desc1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
otherChild.setDescription( "desc2" );
parent.getChildren().add( otherChild );
otherChild.setParent( parent );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
parent = ( Parent ) session.get( Parent.class, parent.getName() );
assertTrue( parent.getChildren().contains( child ) );
assertTrue( parent.getChildren().contains( otherChild ) );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
child = ( Child ) session.get( Child.class, child.getName() );
assertTrue( child.getParent().getChildren().contains( child ) );
session.clear();
child = ( Child ) session.createCriteria( Child.class, child.getName() )
.setCacheable( true )
.add( Restrictions.idEq( "c1" ) )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = ( Child ) session.createCriteria( Child.class, child.getName() )
.setCacheable( true )
.add( Restrictions.idEq( "c1" ) )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = ( Child ) session.createQuery( "from Child where name = 'c1'" )
.setCacheable( true )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
child = ( Child ) session.createQuery( "from Child where name = 'c1'" )
.setCacheable( true )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
session.delete( child.getParent() );
session.getTransaction().commit();
session.close();
}
public void testLoadChildCheckParentContainsChildNoCache() {
Parent parent = new Parent( "p1" );
Child child = new Child( "c1" );
parent.getChildren().add( child );
child.setParent( parent );
Child otherChild = new Child( "c2" );
parent.getChildren().add( otherChild );
otherChild.setParent( parent );
Session session = openSession();
session.beginTransaction();
session.save( parent );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
session.setCacheMode( CacheMode.IGNORE );
parent = ( Parent ) session.get( Parent.class, parent.getName() );
assertTrue( parent.getChildren().contains( child ) );
assertTrue( parent.getChildren().contains( otherChild ) );
session.getTransaction().commit();
session = openSession();
session.beginTransaction();
session.setCacheMode( CacheMode.IGNORE );
child = ( Child ) session.get( Child.class, child.getName() );
assertTrue( child.getParent().getChildren().contains( child ) );
session.clear();
child = ( Child ) session.createCriteria( Child.class, child.getName() )
.add( Restrictions.idEq( "c1" ) )
.uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
assertTrue( child.getParent().getChildren().contains( otherChild ) );
session.clear();
child = ( Child ) session.createQuery( "from Child where name = 'c1'" ).uniqueResult();
assertTrue( child.getParent().getChildren().contains( child ) );
session.delete( child.getParent() );
session.getTransaction().commit();
session.close();
}
}