diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Address.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Address.java deleted file mode 100644 index 478fbd3c35..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Address.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity; - - -/** - * @author Steve Ebersole - */ -public interface Address { - public Long getId(); - public void setId(Long id); - - public String getStreet(); - public void setStreet(String street); - - public String getCity(); - public void setCity(String city); - - public String getPostalCode(); - public void setPostalCode(String postalCode); -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Company.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Company.java deleted file mode 100644 index 10abc3d45a..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Company.java +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity; - - -/** - * @author Steve Ebersole - */ -public interface Company { - public Long getId(); - public void setId(Long id); - public String getName(); - public void setName(String name); -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Customer.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Customer.java deleted file mode 100644 index a2eb2cdd73..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Customer.java +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity; - - -/** - * @author Steve Ebersole - */ -public interface Customer extends Person { - public Company getCompany(); - public void setCompany(Company company); -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/DataProxyHandler.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/DataProxyHandler.java deleted file mode 100644 index ee03da385f..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/DataProxyHandler.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Method; -import java.util.HashMap; - -/** - * A simple {@link InvocationHandler} to act as the handler for our generated - * {@link java.lang.reflect.Proxy}-based entity instances. - *

- * This is a trivial impl which simply keeps the property values into - * a Map. - * - * @author Steve Ebersole - */ -public final class DataProxyHandler implements InvocationHandler { - private String entityName; - private HashMap data = new HashMap(); - - public DataProxyHandler(String entityName, Object id) { - this.entityName = entityName; - data.put( "Id", id ); - } - - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - String methodName = method.getName(); - if ( methodName.startsWith( "set" ) ) { - String propertyName = methodName.substring( 3 ); - data.put( propertyName, args[0] ); - } - else if ( methodName.startsWith( "get" ) ) { - String propertyName = methodName.substring( 3 ); - return data.get( propertyName ); - } - else if ( "toString".equals( methodName ) ) { - return entityName + "#" + data.get( "Id" ); - } - else if ( "hashCode".equals( methodName ) ) { - return new Integer( this.hashCode() ); - } - return null; - } - - public String getEntityName() { - return entityName; - } - - public HashMap getData() { - return data; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Person.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Person.java deleted file mode 100644 index 86c866c2d8..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/Person.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity; -import java.util.Set; - -/** - * @author Steve Ebersole - */ -public interface Person { - public Long getId(); - public void setId(Long id); - public String getName(); - public void setName(String name); - public Address getAddress(); - public void setAddress(Address address); - public Set getFamily(); - public void setFamily(Set family); -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/ProxyHelper.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/ProxyHelper.java deleted file mode 100644 index a6b2461683..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/ProxyHelper.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; - -/** - * @author Steve Ebersole - */ -public class ProxyHelper { - - public static Person newPersonProxy() { - return newPersonProxy( null ); - } - - public static Person newPersonProxy(Object id) { - return ( Person ) Proxy.newProxyInstance( - Person.class.getClassLoader(), - new Class[] {Person.class}, - new DataProxyHandler( Person.class.getName(), id ) - ); - } - - public static Customer newCustomerProxy() { - return newCustomerProxy( null ); - } - - public static Customer newCustomerProxy(Object id) { - return ( Customer ) Proxy.newProxyInstance( - Customer.class.getClassLoader(), - new Class[] {Customer.class}, - new DataProxyHandler( Customer.class.getName(), id ) - ); - } - - public static Company newCompanyProxy() { - return newCompanyProxy( null ); - } - - public static Company newCompanyProxy(Object id) { - return ( Company ) Proxy.newProxyInstance( - Company.class.getClassLoader(), - new Class[] {Company.class}, - new DataProxyHandler( Company.class.getName(), id ) - ); - } - - public static Address newAddressProxy() { - return newAddressProxy( null ); - } - - public static Address newAddressProxy(Object id) { - return ( Address ) Proxy.newProxyInstance( - Address.class.getClassLoader(), - new Class[] {Address.class}, - new DataProxyHandler( Address.class.getName(), id ) - ); - } - - public static String extractEntityName(Object object) { - // Our custom java.lang.reflect.Proxy instances actually bundle - // their appropriate entity name, so we simply extract it from there - // if this represents one of our proxies; otherwise, we return null - if ( Proxy.isProxyClass( object.getClass() ) ) { - InvocationHandler handler = Proxy.getInvocationHandler( object ); - if ( DataProxyHandler.class.isAssignableFrom( handler.getClass() ) ) { - DataProxyHandler myHandler = ( DataProxyHandler ) handler; - return myHandler.getEntityName(); - } - } - return null; - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/package.html b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/package.html deleted file mode 100644 index 73bd8531bb..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/package.html +++ /dev/null @@ -1,35 +0,0 @@ - - - - - -

- Demonstration of different ways to use Hibernate to represent your domain - model as a series of JDK dynamic proxies. We map the interfaces and then - use one of two approaches to get Hibernate to recognize these proxies as - domain entities. Really this is demonstrating various "entity representation" - capabilities built in to Hibernate3. -

-

- First we use an interceptor-based approach where we use a custom Interceptor - implementation for interpret incoming proxies (and resolve them to the correct - mappings) and to help Hibernate instantiate these proxy instances. This is the - quick-and-dirty approach. It is fully expected that this approach will - encounter certain limitations. -

-

- Next we explore the notion of a Tuplizer and plug in custom Tuplizers to - help achieve the same results. Currently, Tuplizers do not have a chance - to influence the resolution of entity-name given a potential entity, so we - also use an Interceptor here and supply its getEntityName() impl. This is - simply so we do not need to supply the entity name explicitly to the - Hibernate Session calls. -

- - - \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/Customer.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/Customer.hbm.xml deleted file mode 100644 index f5944985e7..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/Customer.hbm.xml +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java deleted file mode 100644 index 7e555f93a6..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/EntityNameInterceptor.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer; -import org.hibernate.EmptyInterceptor; -import org.hibernate.test.dynamicentity.ProxyHelper; - -/** - * @author Steve Ebersole - */ -public class EntityNameInterceptor extends EmptyInterceptor { - /** - * The callback from Hibernate to determine the entity name given - * a presumed entity instance. - * - * @param object The presumed entity instance. - * @return The entity name (pointing to the proper entity mapping). - */ - @Override - public String getEntityName(Object object) { - String entityName = ProxyHelper.extractEntityName( object ); - if ( entityName == null ) { - entityName = super.getEntityName( object ); - } - return entityName; - } - - -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/MyEntityInstantiator.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/MyEntityInstantiator.java deleted file mode 100644 index e0f122dd25..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/MyEntityInstantiator.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer; - -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.Proxy; - -import org.hibernate.HibernateException; -import org.hibernate.internal.util.ReflectHelper; -import org.hibernate.tuple.Instantiator; - -import org.hibernate.test.dynamicentity.Address; -import org.hibernate.test.dynamicentity.Company; -import org.hibernate.test.dynamicentity.Customer; -import org.hibernate.test.dynamicentity.DataProxyHandler; -import org.hibernate.test.dynamicentity.Person; -import org.hibernate.test.dynamicentity.ProxyHelper; - -/** - * @author Steve Ebersole - */ -public class MyEntityInstantiator implements Instantiator { - private final String entityName; - - public MyEntityInstantiator(String entityName) { - this.entityName = entityName; - } - - @Override - public Object instantiate(Object id) { - if ( Person.class.getName().equals( entityName ) ) { - return ProxyHelper.newPersonProxy( id ); - } - if ( Customer.class.getName().equals( entityName ) ) { - return ProxyHelper.newCustomerProxy( id ); - } - else if ( Company.class.getName().equals( entityName ) ) { - return ProxyHelper.newCompanyProxy( id ); - } - else if ( Address.class.getName().equals( entityName ) ) { - return ProxyHelper.newAddressProxy( id ); - } - else { - throw new IllegalArgumentException( "unknown entity for instantiation [" + entityName + "]" ); - } - } - - @Override - public Object instantiate() { - return instantiate( null ); - } - - @Override - public boolean isInstance(Object object) { - String resolvedEntityName = null; - if ( Proxy.isProxyClass( object.getClass() ) ) { - InvocationHandler handler = Proxy.getInvocationHandler( object ); - if ( DataProxyHandler.class.isAssignableFrom( handler.getClass() ) ) { - DataProxyHandler myHandler = ( DataProxyHandler ) handler; - resolvedEntityName = myHandler.getEntityName(); - } - } - try { - return ReflectHelper.classForName( entityName ).isInstance( object ); - } - catch( Throwable t ) { - throw new HibernateException( "could not get handle to entity-name as interface : " + t ); - } - -// return entityName.equals( resolvedEntityName ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/MyEntityTuplizer.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/MyEntityTuplizer.java deleted file mode 100644 index e79b97a6b4..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/MyEntityTuplizer.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer; -import org.hibernate.mapping.PersistentClass; -import org.hibernate.property.access.spi.Getter; -import org.hibernate.property.access.spi.Setter; -import org.hibernate.proxy.ProxyFactory; -import org.hibernate.tuple.entity.EntityMetamodel; -import org.hibernate.tuple.entity.PojoEntityTuplizer; - -/** - * @author Steve Ebersole - */ -public class MyEntityTuplizer extends PojoEntityTuplizer { - - public MyEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) { - super( entityMetamodel, mappedEntity ); - } - - @Override - protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) { - // allows defining a custom proxy factory, which is responsible for - // generating lazy proxies for a given entity. - // - // Here we simply use the default... - return super.buildProxyFactory( persistentClass, idGetter, idSetter ); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/TuplizerDynamicEntityTest.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/TuplizerDynamicEntityTest.java deleted file mode 100644 index 86410babfb..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer/TuplizerDynamicEntityTest.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer; - -import java.util.HashSet; - -import org.junit.Test; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.cfg.Configuration; -import org.hibernate.test.dynamicentity.Address; -import org.hibernate.test.dynamicentity.Company; -import org.hibernate.test.dynamicentity.Customer; -import org.hibernate.test.dynamicentity.Person; -import org.hibernate.test.dynamicentity.ProxyHelper; -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - - -/** - * Demonstrates use of Tuplizers to allow the use of JDK - * {@link java.lang.reflect.Proxy dynamic proxies} as our - * domain model. - *

- * Here we plug a custom Interceptor into the session simply to - * allow us to not have to explicitly supply the appropriate entity - * name to the Session calls. - * - * @author Steve Ebersole - */ -public class TuplizerDynamicEntityTest extends BaseCoreFunctionalTestCase { - @Override - public String[] getMappings() { - return new String[] { "dynamicentity/tuplizer/Customer.hbm.xml" }; - } - - @Override - protected String getBaseForMappings() { - return "org/hibernate/test/"; - } - - @Override - public void configure(Configuration cfg) { - super.configure( cfg ); - cfg.setInterceptor( new EntityNameInterceptor() ); - } - - @Test - public void testIt() { - // Test saving these dyna-proxies - Session session = openSession(); - session.beginTransaction(); - Company company = ProxyHelper.newCompanyProxy(); - company.setName( "acme" ); - session.save( company ); - Customer customer = ProxyHelper.newCustomerProxy(); - customer.setName( "Steve" ); - customer.setCompany( company ); - Address address = ProxyHelper.newAddressProxy(); - address.setStreet( "somewhere over the rainbow" ); - address.setCity( "lawerence, kansas" ); - address.setPostalCode( "toto"); - customer.setAddress( address ); - customer.setFamily( new HashSet() ); - Person son = ProxyHelper.newPersonProxy(); - son.setName( "son" ); - customer.getFamily().add( son ); - Person wife = ProxyHelper.newPersonProxy(); - wife.setName( "wife" ); - customer.getFamily().add( wife ); - session.save( customer ); - session.getTransaction().commit(); - session.close(); - - assertNotNull( "company id not assigned", company.getId() ); - assertNotNull( "customer id not assigned", customer.getId() ); - assertNotNull( "address id not assigned", address.getId() ); - assertNotNull( "son:Person id not assigned", son.getId() ); - assertNotNull( "wife:Person id not assigned", wife.getId() ); - - // Test loading these dyna-proxies, along with flush processing - session = openSession(); - session.beginTransaction(); - customer = ( Customer ) session.load( Customer.class, customer.getId() ); - assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer ) ); - - customer.setName( "other" ); - session.flush(); - assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer.getCompany() ) ); - - session.refresh( customer ); - assertEquals( "name not updated", "other", customer.getName() ); - assertEquals( "company association not correct", "acme", customer.getCompany().getName() ); - - session.getTransaction().commit(); - session.close(); - - // Test detached entity re-attachment with these dyna-proxies - customer.setName( "Steve" ); - session = openSession(); - session.beginTransaction(); - session.update( customer ); - session.flush(); - session.refresh( customer ); - assertEquals( "name not updated", "Steve", customer.getName() ); - session.getTransaction().commit(); - session.close(); - - // Test querying - session = openSession(); - session.beginTransaction(); - int count = session.createQuery( "from Customer" ).list().size(); - assertEquals( "querying dynamic entity", 1, count ); - session.clear(); - count = session.createQuery( "from Person" ).list().size(); - assertEquals( "querying dynamic entity", 3, count ); - session.getTransaction().commit(); - session.close(); - - // test deleteing - session = openSession(); - session.beginTransaction(); - session.delete( company ); - session.delete( customer ); - session.getTransaction().commit(); - session.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/Customer.hbm.xml b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/Customer.hbm.xml deleted file mode 100644 index 623d8dc6df..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/Customer.hbm.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/ImprovedTuplizerDynamicEntityTest.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/ImprovedTuplizerDynamicEntityTest.java deleted file mode 100644 index a1fb69f947..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/ImprovedTuplizerDynamicEntityTest.java +++ /dev/null @@ -1,135 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer2; - -import java.util.HashSet; - -import org.hibernate.Hibernate; -import org.hibernate.Session; -import org.hibernate.cfg.Configuration; -import org.hibernate.metamodel.RepresentationMode; - -import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; -import org.hibernate.test.dynamicentity.Address; -import org.hibernate.test.dynamicentity.Company; -import org.hibernate.test.dynamicentity.Customer; -import org.hibernate.test.dynamicentity.Person; -import org.hibernate.test.dynamicentity.ProxyHelper; -import org.junit.Test; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; - -/** - * Demonstrates use of Tuplizers to allow the use of JDK - * {@link java.lang.reflect.Proxy dynamic proxies} as our - * domain model. - *

- * Here we plug a custom Interceptor into the session simply to - * allow us to not have to explicitly supply the appropriate entity - * name to the Session calls. - * - * @author Steve Ebersole - */ -public class ImprovedTuplizerDynamicEntityTest extends BaseCoreFunctionalTestCase { - public String[] getMappings() { - return new String[] { "dynamicentity/tuplizer2/Customer.hbm.xml" }; - } - - @Override - protected String getBaseForMappings() { - return "org/hibernate/test/"; - } - - public void configure(Configuration cfg) { - super.configure( cfg ); - cfg.getEntityTuplizerFactory().registerDefaultTuplizerClass( RepresentationMode.POJO, MyEntityTuplizer.class ); - } - - @Test - @SuppressWarnings( {"unchecked"}) - public void testIt() { - // Test saving these dyna-proxies - Session session = openSession(); - session.beginTransaction(); - Company company = ProxyHelper.newCompanyProxy(); - company.setName( "acme" ); - session.save( company ); - Customer customer = ProxyHelper.newCustomerProxy(); - customer.setName( "Steve" ); - customer.setCompany( company ); - Address address = ProxyHelper.newAddressProxy(); - address.setStreet( "somewhere over the rainbow" ); - address.setCity( "lawerence, kansas" ); - address.setPostalCode( "toto"); - customer.setAddress( address ); - customer.setFamily( new HashSet() ); - Person son = ProxyHelper.newPersonProxy(); - son.setName( "son" ); - customer.getFamily().add( son ); - Person wife = ProxyHelper.newPersonProxy(); - wife.setName( "wife" ); - customer.getFamily().add( wife ); - session.save( customer ); - session.getTransaction().commit(); - session.close(); - - assertNotNull( "company id not assigned", company.getId() ); - assertNotNull( "customer id not assigned", customer.getId() ); - assertNotNull( "address id not assigned", address.getId() ); - assertNotNull( "son:Person id not assigned", son.getId() ); - assertNotNull( "wife:Person id not assigned", wife.getId() ); - - // Test loading these dyna-proxies, along with flush processing - session = openSession(); - session.beginTransaction(); - customer = ( Customer ) session.load( Customer.class, customer.getId() ); - assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer ) ); - - customer.setName( "other" ); - session.flush(); - assertFalse( "should-be-proxy was initialized", Hibernate.isInitialized( customer.getCompany() ) ); - - session.refresh( customer ); - assertEquals( "name not updated", "other", customer.getName() ); - assertEquals( "company association not correct", "acme", customer.getCompany().getName() ); - - session.getTransaction().commit(); - session.close(); - - // Test detached entity re-attachment with these dyna-proxies - customer.setName( "Steve" ); - session = openSession(); - session.beginTransaction(); - session.update( customer ); - session.flush(); - session.refresh( customer ); - assertEquals( "name not updated", "Steve", customer.getName() ); - session.getTransaction().commit(); - session.close(); - - // Test querying - session = openSession(); - session.beginTransaction(); - int count = session.createQuery( "from Customer" ).list().size(); - assertEquals( "querying dynamic entity", 1, count ); - session.clear(); - count = session.createQuery( "from Person" ).list().size(); - assertEquals( "querying dynamic entity", 3, count ); - session.getTransaction().commit(); - session.close(); - - // test deleteing - session = openSession(); - session.beginTransaction(); - session.delete( company ); - session.delete( customer ); - session.getTransaction().commit(); - session.close(); - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/MyEntityInstantiator.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/MyEntityInstantiator.java deleted file mode 100644 index 1e9110deaf..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/MyEntityInstantiator.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer2; - -import org.hibernate.HibernateException; -import org.hibernate.internal.util.ReflectHelper; -import org.hibernate.test.dynamicentity.Address; -import org.hibernate.test.dynamicentity.Company; -import org.hibernate.test.dynamicentity.Customer; -import org.hibernate.test.dynamicentity.Person; -import org.hibernate.test.dynamicentity.ProxyHelper; -import org.hibernate.tuple.Instantiator; - -/** - * @author Steve Ebersole - */ -public class MyEntityInstantiator implements Instantiator { - private final String entityName; - - public MyEntityInstantiator(String entityName) { - this.entityName = entityName; - } - - @Override - public Object instantiate(Object id) { - if ( Person.class.getName().equals( entityName ) ) { - return ProxyHelper.newPersonProxy( id ); - } - if ( Customer.class.getName().equals( entityName ) ) { - return ProxyHelper.newCustomerProxy( id ); - } - else if ( Company.class.getName().equals( entityName ) ) { - return ProxyHelper.newCompanyProxy( id ); - } - else if ( Address.class.getName().equals( entityName ) ) { - return ProxyHelper.newAddressProxy( id ); - } - else { - throw new IllegalArgumentException( "unknown entity for instantiation [" + entityName + "]" ); - } - } - - @Override - public Object instantiate() { - return instantiate( null ); - } - - @Override - public boolean isInstance(Object object) { - try { - return ReflectHelper.classForName( entityName ).isInstance( object ); - } - catch( Throwable t ) { - throw new HibernateException( "could not get handle to entity-name as interface : " + t ); - } - } -} diff --git a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/MyEntityTuplizer.java b/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/MyEntityTuplizer.java deleted file mode 100644 index f4fd5d7802..0000000000 --- a/hibernate-core/src/test/java/org/hibernate/test/dynamicentity/tuplizer2/MyEntityTuplizer.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Hibernate, Relational Persistence for Idiomatic Java - * - * License: GNU Lesser General Public License (LGPL), version 2.1 or later. - * See the lgpl.txt file in the root directory or . - */ -package org.hibernate.test.dynamicentity.tuplizer2; -import org.hibernate.EntityNameResolver; -import org.hibernate.engine.spi.SessionFactoryImplementor; -import org.hibernate.mapping.PersistentClass; -import org.hibernate.property.access.spi.Getter; -import org.hibernate.property.access.spi.Setter; -import org.hibernate.proxy.ProxyFactory; -import org.hibernate.test.dynamicentity.ProxyHelper; - -import org.hibernate.tuple.entity.EntityMetamodel; -import org.hibernate.tuple.entity.PojoEntityTuplizer; - -/** - * @author Steve Ebersole - */ -public class MyEntityTuplizer extends PojoEntityTuplizer { - - public MyEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) { - super( entityMetamodel, mappedEntity ); - } - - @Override - public EntityNameResolver[] getEntityNameResolvers() { - return new EntityNameResolver[] { MyEntityNameResolver.INSTANCE }; - } - - @Override - public String determineConcreteSubclassEntityName(Object entityInstance, SessionFactoryImplementor factory) { - String entityName = ProxyHelper.extractEntityName( entityInstance ); - if ( entityName == null ) { - entityName = super.determineConcreteSubclassEntityName( entityInstance, factory ); - } - return entityName; - } - - @Override - protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) { - // allows defining a custom proxy factory, which is responsible for - // generating lazy proxies for a given entity. - // - // Here we simply use the default... - return super.buildProxyFactory( persistentClass, idGetter, idSetter ); - } - - public static class MyEntityNameResolver implements EntityNameResolver { - public static final MyEntityNameResolver INSTANCE = new MyEntityNameResolver(); - - public String resolveEntityName(Object entity) { - return ProxyHelper.extractEntityName( entity ); - } - - public boolean equals(Object obj) { - return getClass().equals( obj.getClass() ); - } - - public int hashCode() { - return getClass().hashCode(); - } - } -}