From 27223c6b040d2dc2a952baf19b91b1d9e6694819 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Wed, 31 Oct 2007 00:14:49 +0000 Subject: [PATCH] HHH-2921 : added test cases that execute polymorphic queries when there is a superclass proxy for the resulting entity in the session cache git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@14160 1b8cb986-b30d-0410-93ca-fae66ebed9b2 --- .../test/discriminator/DiscriminatorTest.java | 82 ++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/testsuite/src/test/java/org/hibernate/test/discriminator/DiscriminatorTest.java b/testsuite/src/test/java/org/hibernate/test/discriminator/DiscriminatorTest.java index fcb6c10111..2baeb3c4b4 100755 --- a/testsuite/src/test/java/org/hibernate/test/discriminator/DiscriminatorTest.java +++ b/testsuite/src/test/java/org/hibernate/test/discriminator/DiscriminatorTest.java @@ -1,4 +1,4 @@ -//$Id: DiscriminatorTest.java 10977 2006-12-12 23:28:04Z steve.ebersole@jboss.com $ +//$Id: DiscriminatorTest.java 10976 2006-12-12 23:22:26Z steve.ebersole@jboss.com $ package org.hibernate.test.discriminator; import java.math.BigDecimal; @@ -10,7 +10,9 @@ import junit.framework.Test; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; +import org.hibernate.proxy.HibernateProxy; import org.hibernate.criterion.Property; +import org.hibernate.criterion.Restrictions; import org.hibernate.junit.functional.FunctionalTestCase; import org.hibernate.junit.functional.FunctionalTestClassTestSuite; @@ -171,6 +173,82 @@ public class DiscriminatorTest extends FunctionalTestCase { s.close(); } + public void testLoadSuperclassProxyPolymorphicAccess() { + Session s = openSession(); + s.beginTransaction(); + Employee e = new Employee(); + e.setName( "Steve" ); + e.setSex( 'M' ); + e.setTitle( "grand poobah" ); + s.save( e ); + s.getTransaction().commit(); + s.close(); + s = openSession(); + s.beginTransaction(); + // load the superclass proxy. + Person pLoad = ( Person ) s.load( Person.class, new Long( e.getId() ) ); + assertTrue( pLoad instanceof HibernateProxy); + Person pGet = ( Person ) s.get( Person.class, new Long( e.getId() )); + Person pQuery = ( Person ) s.createQuery( "from Person where id = :id" ) + .setLong( "id", e.getId() ) + .uniqueResult(); + Person pCriteria = ( Person ) s.createCriteria( Person.class ) + .add( Restrictions.idEq( new Long( e.getId() ) ) ) + .uniqueResult(); + // assert that executing the queries polymorphically returns the same proxy + assertSame( pLoad, pGet ); + assertSame( pLoad, pQuery ); + assertSame( pLoad, pCriteria ); + + // assert that the proxy is not an instance of Employee + assertFalse( pLoad instanceof Employee ); + + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + s.delete( e ); + s.getTransaction().commit(); + s.close(); + } + + public void testLoadSuperclassProxyEvictPolymorphicAccess() { + Session s = openSession(); + s.beginTransaction(); + Employee e = new Employee(); + e.setName( "Steve" ); + e.setSex( 'M' ); + e.setTitle( "grand poobah" ); + s.save( e ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + // load the superclass proxy. + Person pLoad = ( Person ) s.load( Person.class, new Long( e.getId() ) ); + assertTrue( pLoad instanceof HibernateProxy); + // evict the proxy + s.evict( pLoad ); + Employee pGet = ( Employee ) s.get( Person.class, new Long( e.getId() )); + Employee pQuery = ( Employee ) s.createQuery( "from Person where id = :id" ) + .setLong( "id", e.getId() ) + .uniqueResult(); + Employee pCriteria = ( Employee ) s.createCriteria( Person.class ) + .add( Restrictions.idEq( new Long( e.getId() ) ) ) + .uniqueResult(); + // assert that executing the queries polymorphically returns the same Employee instance + assertSame( pGet, pQuery ); + assertSame( pGet, pCriteria ); + s.getTransaction().commit(); + s.close(); + + s = openSession(); + s.beginTransaction(); + s.delete( e ); + s.getTransaction().commit(); + s.close(); + } } -