clean up remaining test suite failures
This commit is contained in:
parent
7437a96b12
commit
1dcdec9c15
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.dynamicentity;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
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);
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.dynamicentity;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
public interface Company {
|
||||
public Long getId();
|
||||
public void setId(Long id);
|
||||
public String getName();
|
||||
public void setName(String name);
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.dynamicentity;
|
||||
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
public interface Customer extends Person {
|
||||
public Company getCompany();
|
||||
public void setCompany(Company company);
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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.
|
||||
* <p/>
|
||||
* This is a trivial impl which simply keeps the property values into
|
||||
* a Map.
|
||||
*
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.dynamicentity;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
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);
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.dynamicentity;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Proxy;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -1,35 +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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<html>
|
||||
<head></head>
|
||||
|
||||
<body>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
<p>
|
||||
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.
|
||||
</p>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -1,60 +0,0 @@
|
|||
<?xml version="1.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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.dynamicentity">
|
||||
|
||||
<!--
|
||||
Mapping the Customer and Company interfaces. Our custom Interceptor
|
||||
will be responsible for: a) creating instances representing these
|
||||
interfaces; b) determining the appropriate entity-name (i.e., which
|
||||
entity mapping to use) given an instance of one of these proxies.
|
||||
-->
|
||||
<class name="Person" table="t_person" discriminator-value="person" abstract="false">
|
||||
<tuplizer class="org.hibernate.test.dynamicentity.tuplizer.MyEntityTuplizer" entity-mode="pojo"/>
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<discriminator column="t_person_dis" force="false"/>
|
||||
<property name="name"/>
|
||||
|
||||
<many-to-one name="address" cascade="all" column="addr_id"/>
|
||||
|
||||
<set name="family" lazy="true" cascade="all">
|
||||
<key column="pers_id"/>
|
||||
<one-to-many class="Person"/>
|
||||
</set>
|
||||
|
||||
<subclass name="Customer" discriminator-value="customer" abstract="false">
|
||||
<tuplizer class="org.hibernate.test.dynamicentity.tuplizer.MyEntityTuplizer" entity-mode="pojo"/>
|
||||
<many-to-one name="company" cascade="none" column="comp_id"/>
|
||||
</subclass>
|
||||
</class>
|
||||
|
||||
<!-- Company interface mapping -->
|
||||
<class name="Company" table="t_company" abstract="false">
|
||||
<tuplizer class="org.hibernate.test.dynamicentity.tuplizer.MyEntityTuplizer" entity-mode="pojo"/>
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="name"/>
|
||||
</class>
|
||||
|
||||
<class name="Address" table="t_address" abstract="false">
|
||||
<tuplizer class="org.hibernate.test.dynamicentity.tuplizer.MyEntityTuplizer" entity-mode="pojo"/>
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="street"/>
|
||||
<property name="city"/>
|
||||
<property name="postalCode"/>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
package org.hibernate.test.dynamicentity.tuplizer;
|
||||
import org.hibernate.EmptyInterceptor;
|
||||
import org.hibernate.test.dynamicentity.ProxyHelper;
|
||||
|
||||
/**
|
||||
* @author <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 <a href="mailto:steve@hibernate.org">Steve Ebersole </a>
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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.
|
||||
* <p/>
|
||||
* 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();
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
<?xml version="1.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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
-->
|
||||
<!DOCTYPE hibernate-mapping PUBLIC
|
||||
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
||||
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
|
||||
|
||||
<hibernate-mapping package="org.hibernate.test.dynamicentity">
|
||||
|
||||
<class name="Person" table="t_person" discriminator-value="person" abstract="false">
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<discriminator column="t_person_dis" force="false"/>
|
||||
<property name="name"/>
|
||||
|
||||
<many-to-one name="address" cascade="all" column="addr_id"/>
|
||||
|
||||
<set name="family" lazy="true" cascade="all">
|
||||
<key column="pers_id"/>
|
||||
<one-to-many class="Person"/>
|
||||
</set>
|
||||
|
||||
<subclass name="Customer" discriminator-value="customer" abstract="false">
|
||||
<many-to-one name="company" cascade="none" column="comp_id"/>
|
||||
</subclass>
|
||||
</class>
|
||||
|
||||
<class name="Company" table="t_company" abstract="false">
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="name"/>
|
||||
</class>
|
||||
|
||||
<class name="Address" table="t_address" abstract="false">
|
||||
<id name="id">
|
||||
<generator class="increment"/>
|
||||
</id>
|
||||
<property name="street"/>
|
||||
<property name="city"/>
|
||||
<property name="postalCode"/>
|
||||
</class>
|
||||
|
||||
</hibernate-mapping>
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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.
|
||||
* <p/>
|
||||
* 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();
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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 );
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue