Author: epbernard
Date: 2006-12-19 06:38:26 -0500 (Tue, 19 Dec 2006)
New Revision: 11000
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizer.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizers.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Country.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Cuisine.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DataProxyHandler.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicComponentTuplizer.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicEntityTuplizer.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicInstantiator.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/EntityNameInterceptor.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/ProxyHelper.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/TuplizerTest.java
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/InheritanceState.java
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/TestCase.java
Log:
ANN-505 add @Tuplizer and allow interface annotations (not subclassed by real classes
though)
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizer.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizer.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizer.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,21 @@
+//$Id: $
+package org.hibernate.annotations;
+
+import java.lang.annotation.*;
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+import static java.lang.annotation.ElementType.TYPE;
+import static java.lang.annotation.ElementType.FIELD;
+import static java.lang.annotation.ElementType.METHOD;
+
+/**
+ * Define a tuplizer for an entity or a component
+ * @author Emmanuel Bernard
+ */
+(a)java.lang.annotation.Target( {TYPE, FIELD, METHOD} )
+@Retention( RUNTIME )
+public @interface Tuplizer {
+ /** tuplizer implementation */
+ Class impl();
+ /** either pojo, dynamic-map or domj4 */
+ String entityMode() default "pojo";
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizers.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizers.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/annotations/Tuplizers.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,16 @@
+//$Id: $
+package org.hibernate.annotations;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.RetentionPolicy;
+
+/**
+ * Define a set of tuplizer for an entity or a component
+ * @author Emmanuel Bernard
+ */
+(a)java.lang.annotation.Target( {ElementType.TYPE, ElementType.FIELD, ElementType.METHOD}
)
+@Retention( RetentionPolicy.RUNTIME )
+public @interface Tuplizers {
+ Tuplizer[] value();
+}
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationBinder.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -53,6 +53,7 @@
import org.hibernate.AssertionFailure;
import org.hibernate.FetchMode;
import org.hibernate.MappingException;
+import org.hibernate.EntityMode;
import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
@@ -89,6 +90,8 @@
import org.hibernate.annotations.Where;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.Target;
+import org.hibernate.annotations.Tuplizers;
+import org.hibernate.annotations.Tuplizer;
import org.hibernate.cfg.annotations.CollectionBinder;
import org.hibernate.cfg.annotations.EntityBinder;
import org.hibernate.cfg.annotations.Nullability;
@@ -840,7 +843,7 @@
superClass = superClass.getSuperclass();
superclassState = inheritanceStatePerClass.get( superClass );
}
- while ( !mappings.getReflectionManager().equals( superClass, Object.class ) &&
superclassState == null );
+ while ( superClass != null && !mappings.getReflectionManager().equals(
superClass, Object.class ) && superclassState == null );
currentClassInHierarchy = superClass;
}
@@ -1610,6 +1613,8 @@
isComponentEmbedded, isIdentifierMapper,
false, mappings
);
+ XProperty property = inferredData.getProperty();
+ setupComponentTuplizer( property, comp );
PropertyBinder binder = new PropertyBinder();
binder.setName( inferredData.getPropertyName() );
@@ -1710,6 +1715,9 @@
if ( componentId.getPropertySpan() == 0 ) {
throw new AnnotationException( componentId.getComponentClassName() + " has no
persistent id property" );
}
+ //tuplizers
+ XProperty property = inferredData.getProperty();
+ setupComponentTuplizer( property, componentId );
}
else {
for ( Ejb3Column column : columns ) {
@@ -1740,6 +1748,21 @@
}
}
+ private static void setupComponentTuplizer(XProperty property, Component component) {
+ if (property == null) return;
+ if (property.isAnnotationPresent( Tuplizers.class ) ) {
+ for ( Tuplizer tuplizer : property.getAnnotation( Tuplizers.class ).value() ) {
+ EntityMode mode = EntityMode.parse( tuplizer.entityMode() );
+ component.addTuplizer( mode, tuplizer.impl().getName() );
+ }
+ }
+ if ( property.isAnnotationPresent( Tuplizer.class ) ) {
+ Tuplizer tuplizer = property.getAnnotation( Tuplizer.class );
+ EntityMode mode = EntityMode.parse( tuplizer.entityMode() );
+ component.addTuplizer( mode, tuplizer.impl().getName() );
+ }
+ }
+
private static void bindManyToOne(
String cascadeStrategy, Ejb3JoinColumn[] columns, boolean optional,
boolean ignoreNotFound, boolean cascadeOnDelete,
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/AnnotationConfiguration.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -97,7 +97,7 @@
//for each class, copy all the relevent hierarchy
for ( XClass clazz : original ) {
XClass superClass = clazz.getSuperclass();
- while ( ! reflectionManager.equals( superClass, Object.class ) && !
copy.contains( superClass ) ) {
+ while ( superClass != null && ! reflectionManager.equals( superClass,
Object.class ) && ! copy.contains( superClass ) ) {
if ( superClass.isAnnotationPresent( Entity.class )
|| superClass.isAnnotationPresent( MappedSuperclass.class ) ) {
copy.add( superClass );
@@ -115,7 +115,7 @@
}
private void orderHierarchy(List<XClass> copy, List<XClass> newList,
List<XClass> original, XClass clazz) {
- if ( reflectionManager.equals( clazz, Object.class ) ) return;
+ if ( clazz == null || reflectionManager.equals( clazz, Object.class ) ) return;
//process superclass first
orderHierarchy( copy, newList, original, clazz.getSuperclass() );
if ( original.contains( clazz ) ) {
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/InheritanceState.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/InheritanceState.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/InheritanceState.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -70,7 +70,7 @@
InheritanceState currentState = states.get( superclass );
if ( currentState != null && ! currentState.isEmbeddableSuperclass ) return
currentState;
}
- while ( ! reflectionManager.equals( superclass, Object.class ) );
+ while ( superclass != null && ! reflectionManager.equals( superclass,
Object.class ) );
return null;
}
@@ -84,7 +84,7 @@
InheritanceState currentState = states.get( superclass );
if ( currentState != null ) return currentState;
}
- while ( ! reflectionManager.equals( superclass, Object.class ) );
+ while ( superclass != null && ! reflectionManager.equals( superclass,
Object.class ) );
return null;
}
}
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/java/org/hibernate/cfg/annotations/EntityBinder.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -19,6 +19,7 @@
import org.hibernate.AnnotationException;
import org.hibernate.AssertionFailure;
import org.hibernate.MappingException;
+import org.hibernate.EntityMode;
import org.hibernate.annotations.AccessType;
import org.hibernate.annotations.BatchSize;
import org.hibernate.annotations.Cache;
@@ -34,6 +35,8 @@
import org.hibernate.annotations.Tables;
import org.hibernate.annotations.Where;
import org.hibernate.annotations.SQLDeleteAll;
+import org.hibernate.annotations.Tuplizers;
+import org.hibernate.annotations.Tuplizer;
import org.hibernate.cache.CacheFactory;
import org.hibernate.cfg.AnnotationBinder;
import org.hibernate.cfg.BinderHelper;
@@ -225,6 +228,19 @@
persistentClass.setLoaderName( loader.namedQuery() );
}
+ //tuplizers
+ if ( annotatedClass.isAnnotationPresent( Tuplizers.class ) ) {
+ for ( Tuplizer tuplizer : annotatedClass.getAnnotation( Tuplizers.class ).value() ) {
+ EntityMode mode = EntityMode.parse( tuplizer.entityMode() );
+ persistentClass.addTuplizer( mode, tuplizer.impl().getName() );
+ }
+ }
+ if ( annotatedClass.isAnnotationPresent( Tuplizer.class ) ) {
+ Tuplizer tuplizer = annotatedClass.getAnnotation( Tuplizer.class );
+ EntityMode mode = EntityMode.parse( tuplizer.entityMode() );
+ persistentClass.addTuplizer( mode, tuplizer.impl().getName() );
+ }
+
if ( !inheritanceState.hasParents ) {
Iterator<Map.Entry<String, String>> iter = filters.entrySet().iterator();
while ( iter.hasNext() ) {
Modified:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/TestCase.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/TestCase.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/TestCase.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -6,6 +6,7 @@
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
+import org.hibernate.Interceptor;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
@@ -101,6 +102,11 @@
return session;
}
+ public Session openSession(Interceptor interceptor) throws HibernateException {
+ session = getSessions().openSession(interceptor);
+ return session;
+ }
+
protected abstract Class[] getMappings();
protected String[] getAnnotatedPackages() {
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Country.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Country.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Country.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,15 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import javax.persistence.Embeddable;
+import javax.persistence.Column;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Embeddable
+public interface Country {
+ @Column(name = "CountryName")
+ public String getName();
+ public void setName(String name);
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Cuisine.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Cuisine.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/Cuisine.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,29 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Entity;
+
+import org.hibernate.annotations.Tuplizer;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Tuplizer(impl = DynamicEntityTuplizer.class)
+public interface Cuisine {
+ @Id
+ @GeneratedValue
+ public Long getId();
+ public void setId(Long id);
+
+ public String getName();
+ public void setName(String name);
+
+ @Tuplizer(impl = DynamicComponentTuplizer.class)
+ public Country getCountry();
+ public void setCountry(Country country);
+
+
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DataProxyHandler.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DataProxyHandler.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DataProxyHandler.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,53 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.io.Serializable;
+
+/**
+ * A simple {@link java.lang.reflect.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, Serializable 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;
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicComponentTuplizer.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicComponentTuplizer.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicComponentTuplizer.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,29 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import org.hibernate.tuple.entity.PojoEntityTuplizer;
+import org.hibernate.tuple.entity.EntityMetamodel;
+import org.hibernate.tuple.Instantiator;
+import org.hibernate.tuple.component.PojoComponentTuplizer;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.mapping.Component;
+import org.hibernate.proxy.ProxyFactory;
+import org.hibernate.property.Getter;
+import org.hibernate.property.Setter;
+import org.hibernate.HibernateException;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class DynamicComponentTuplizer extends PojoComponentTuplizer {
+
+ public DynamicComponentTuplizer(Component component) {
+ super( component );
+ }
+
+
+ protected Instantiator buildInstantiator(Component component) {
+ return new DynamicInstantiator( component.getComponentClassName() ); //To change body
of overridden methods use File | Settings | File Templates.
+ }
+
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicEntityTuplizer.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicEntityTuplizer.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicEntityTuplizer.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,32 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import org.hibernate.tuple.entity.PojoEntityTuplizer;
+import org.hibernate.tuple.entity.EntityMetamodel;
+import org.hibernate.tuple.Instantiator;
+import org.hibernate.mapping.PersistentClass;
+import org.hibernate.proxy.ProxyFactory;
+import org.hibernate.property.Getter;
+import org.hibernate.property.Setter;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class DynamicEntityTuplizer extends PojoEntityTuplizer {
+
+ public DynamicEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass
mappedEntity) {
+ super( entityMetamodel, mappedEntity );
+ }
+
+ protected Instantiator buildInstantiator(PersistentClass persistentClass) {
+ return new DynamicInstantiator( persistentClass.getEntityName() );
+ }
+
+ 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 );
+ }
+ }
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicInstantiator.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicInstantiator.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/DynamicInstantiator.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,56 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import java.io.Serializable;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
+
+import org.hibernate.tuple.Instantiator;
+import org.hibernate.util.ReflectHelper;
+import org.hibernate.HibernateException;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class DynamicInstantiator implements Instantiator {
+ private final String entityName;
+
+ public DynamicInstantiator(String entityName) {
+ this.entityName = entityName;
+ }
+
+ public Object instantiate(Serializable id) {
+ if ( Cuisine.class.getName().equals( entityName ) ) {
+ return ProxyHelper.newCuisineProxy( id );
+ }
+ if ( Country.class.getName().equals( entityName ) ) {
+ return ProxyHelper.newCountryProxy( id );
+ }
+ else {
+ throw new IllegalArgumentException( "unknown entity for instantiation [" +
entityName + "]" );
+ }
+ }
+
+ public Object instantiate() {
+ return instantiate( null );
+ }
+
+ 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 );
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/EntityNameInterceptor.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/EntityNameInterceptor.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/EntityNameInterceptor.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,25 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import org.hibernate.EmptyInterceptor;
+
+/**
+ * @author Emmanuel Bernard
+ */
+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).
+ */
+ public String getEntityName(Object object) {
+ String entityName = ProxyHelper.extractEntityName( object );
+ if ( entityName == null ) {
+ entityName = super.getEntityName( object );
+ }
+ return entityName;
+ }
+}
+
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/ProxyHelper.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/ProxyHelper.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/ProxyHelper.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,50 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import java.io.Serializable;
+import java.lang.reflect.Proxy;
+import java.lang.reflect.InvocationHandler;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class ProxyHelper {
+
+ public static Country newPersonProxy() {
+ return newCountryProxy( null );
+ }
+
+ public static Country newCountryProxy(Serializable id) {
+ return ( Country ) Proxy.newProxyInstance(
+ Country.class.getClassLoader(),
+ new Class[] {Country.class},
+ new DataProxyHandler( Country.class.getName(), id )
+ );
+ }
+
+ public static Cuisine newCustomerProxy() {
+ return newCuisineProxy( null );
+ }
+
+ public static Cuisine newCuisineProxy(Serializable id) {
+ return ( Cuisine ) Proxy.newProxyInstance(
+ Cuisine.class.getClassLoader(),
+ new Class[] {Cuisine.class},
+ new DataProxyHandler( Cuisine.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;
+ }
+}
Added:
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/TuplizerTest.java
===================================================================
---
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/TuplizerTest.java 2006-12-18
20:58:17 UTC (rev 10999)
+++
branches/Branch_3_2/HibernateExt/metadata/src/test/org/hibernate/test/annotations/tuplizer/TuplizerTest.java 2006-12-19
11:38:26 UTC (rev 11000)
@@ -0,0 +1,34 @@
+//$Id: $
+package org.hibernate.test.annotations.tuplizer;
+
+import org.hibernate.test.annotations.TestCase;
+import org.hibernate.Session;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class TuplizerTest extends TestCase {
+ public void testEntityTuplizer() throws Exception {
+ Cuisine cuisine = ProxyHelper.newCuisineProxy( null );
+ cuisine.setName( "Fran�aise" );
+ Country country = ProxyHelper.newCountryProxy( null );
+ country.setName( "France" );
+ cuisine.setCountry( country );
+ Session s = openSession( new EntityNameInterceptor() );
+ s.getTransaction().begin();
+ s.persist( cuisine );
+ s.flush();
+ s.clear();
+ cuisine = (Cuisine) s.get(Cuisine.class, cuisine.getId() );
+ assertNotNull( cuisine );
+ assertEquals( "Fran�aise", cuisine.getName() );
+ assertEquals( "France", country.getName() );
+ s.getTransaction().rollback();
+ s.close();
+ }
+ protected Class[] getMappings() {
+ return new Class[] {
+ Cuisine.class
+ };
+ }
+}