Author: gbadner
Date: 2010-05-10 20:49:20 -0400 (Mon, 10 May 2010)
New Revision: 19461
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/MappingsNonLazy.hbm.xml
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetNonLazyTest.java
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Child.java
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Mappings.hbm.xml
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java
Log:
HHH-3799 : Added test for PersistentSet does not honor hashcode/equals contract when
loaded eagerly
Modified: core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Child.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Child.java 2010-05-11
00:41:09 UTC (rev 19460)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Child.java 2010-05-11
00:49:20 UTC (rev 19461)
@@ -8,6 +8,7 @@
public class Child {
private String name;
private Parent parent;
+ private String description;
public Child() {
}
@@ -31,4 +32,38 @@
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;
+ }
}
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Mappings.hbm.xml
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Mappings.hbm.xml 2010-05-11
00:41:09 UTC (rev 19460)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/Mappings.hbm.xml 2010-05-11
00:49:20 UTC (rev 19461)
@@ -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">
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/MappingsNonLazy.hbm.xml
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/MappingsNonLazy.hbm.xml
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/MappingsNonLazy.hbm.xml 2010-05-11
00:49:20 UTC (rev 19461)
@@ -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>
Added:
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetNonLazyTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetNonLazyTest.java
(rev 0)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetNonLazyTest.java 2010-05-11
00:49:20 UTC (rev 19461)
@@ -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();
+ }
+}
\ No newline at end of file
Modified:
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java
===================================================================
---
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java 2010-05-11
00:41:09 UTC (rev 19460)
+++
core/trunk/testsuite/src/test/java/org/hibernate/test/collection/set/PersistentSetTest.java 2010-05-11
00:49:20 UTC (rev 19461)
@@ -4,10 +4,12 @@
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 @@
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();
+ }
}