HHH-5182 - Inject SessionFactory into "non-basic" Types

git-svn-id: https://svn.jboss.org/repos/hibernate/core/trunk@19376 1b8cb986-b30d-0410-93ca-fae66ebed9b2
This commit is contained in:
Steve Ebersole 2010-05-06 02:36:12 +00:00
parent 190f2c99d5
commit 7c27fc9f2a
2 changed files with 53 additions and 9 deletions

View File

@ -34,6 +34,7 @@ import java.util.Properties;
import org.hibernate.collection.PersistentCollection;
import org.hibernate.engine.HibernateIterator;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.engine.jdbc.NonContextualLobCreator;
import org.hibernate.engine.jdbc.LobCreationContext;
@ -41,8 +42,10 @@ import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.jdbc.StreamUtils;
import org.hibernate.intercept.FieldInterceptionHelper;
import org.hibernate.intercept.FieldInterceptor;
import org.hibernate.mapping.ManyToOne;
import org.hibernate.proxy.HibernateProxy;
import org.hibernate.proxy.LazyInitializer;
import org.hibernate.type.AnyType;
import org.hibernate.type.BigDecimalType;
import org.hibernate.type.BigIntegerType;
import org.hibernate.type.BinaryType;
@ -61,6 +64,7 @@ import org.hibernate.type.FloatType;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LocaleType;
import org.hibernate.type.LongType;
import org.hibernate.type.ManyToOneType;
import org.hibernate.type.ObjectType;
import org.hibernate.type.SerializableType;
import org.hibernate.type.ShortType;
@ -71,6 +75,7 @@ import org.hibernate.type.TimeZoneType;
import org.hibernate.type.TimestampType;
import org.hibernate.type.TrueFalseType;
import org.hibernate.type.Type;
import org.hibernate.type.TypeFactory;
import org.hibernate.type.YesNoType;
import org.hibernate.type.CharArrayType;
import org.hibernate.type.WrapperBinaryType;
@ -78,6 +83,7 @@ import org.hibernate.type.CharacterArrayType;
import org.hibernate.type.MaterializedBlobType;
import org.hibernate.type.ImageType;
import org.hibernate.type.MaterializedClobType;
import org.hibernate.usertype.CompositeUserType;
/**
* <ul>
@ -293,7 +299,7 @@ public final class Hibernate {
*/
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration" })
public static Type any(Type metaType, Type identifierType) {
throw new HibernateException( "Not supported" );
return new AnyType( metaType, identifierType );
}
/**
@ -301,9 +307,17 @@ public final class Hibernate {
*
* @deprecated Use {@link TypeHelper#entity} instead; see http://opensource.atlassian.com/projects/hibernate/browse/HHH-5182
*/
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration" })
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration", "deprecation" })
public static Type entity(Class persistentClass) {
throw new HibernateException( "Not supported" );
return entity( persistentClass.getName() );
}
private static class NoScope implements TypeFactory.TypeScope {
public static final NoScope INSTANCE = new NoScope();
public SessionFactoryImplementor resolveFactory() {
throw new HibernateException( "Cannot access SessionFactory from here" );
}
}
/**
@ -313,7 +327,7 @@ public final class Hibernate {
*/
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration" })
public static Type entity(String entityName) {
throw new HibernateException( "Not supported" );
return new ManyToOneType( NoScope.INSTANCE, entityName );
}
/**
@ -323,7 +337,7 @@ public final class Hibernate {
*/
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration" })
public static Type custom(Class userTypeClass) {
throw new HibernateException( "Not supported" );
return custom( userTypeClass, null );
}
/**
@ -333,7 +347,17 @@ public final class Hibernate {
*/
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration" })
public static Type custom(Class userTypeClass, String[] parameterNames, String[] parameterValues) {
throw new HibernateException( "Not supported" );
return custom( userTypeClass, toProperties( parameterNames, parameterValues ) ); }
private static Properties toProperties(String[] parameterNames, String[] parameterValues) {
if ( parameterNames == null || parameterNames.length == 0 ) {
return null;
}
Properties parameters = new Properties();
for ( int i = 0; i < parameterNames.length; i ++ ) {
parameters.put( parameterNames[i], parameterValues[i] );
}
return parameters;
}
/**
@ -341,9 +365,14 @@ public final class Hibernate {
*
* @deprecated Use {@link TypeHelper#custom} instead; see http://opensource.atlassian.com/projects/hibernate/browse/HHH-5182
*/
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration" })
@SuppressWarnings({ "JavaDoc", "UnusedDeclaration", "unchecked" })
public static Type custom(Class userTypeClass, Properties parameters) {
throw new HibernateException( "Not supported" );
if ( CompositeUserType.class.isAssignableFrom( userTypeClass ) ) {
return TypeFactory.customComponent( userTypeClass, parameters, NoScope.INSTANCE );
}
else {
return TypeFactory.custom( userTypeClass, parameters, NoScope.INSTANCE );
}
}
/**

View File

@ -122,7 +122,7 @@ public final class TypeFactory implements Serializable {
}
}
public void injectParameters(Object type, Properties parameters) {
public static void injectParameters(Object type, Properties parameters) {
if ( ParameterizedType.class.isInstance( type ) ) {
( (ParameterizedType) type ).setParameterValues(parameters);
}
@ -132,6 +132,14 @@ public final class TypeFactory implements Serializable {
}
public CompositeCustomType customComponent(Class<CompositeUserType> typeClass, Properties parameters) {
return customComponent( typeClass, parameters, typeScope );
}
/**
* @deprecated Only for use temporary use by {@link org.hibernate.Hibernate}
*/
@SuppressWarnings({ "JavaDoc" })
public static CompositeCustomType customComponent(Class<CompositeUserType> typeClass, Properties parameters, TypeScope scope) {
try {
CompositeUserType userType = typeClass.newInstance();
injectParameters( userType, parameters );
@ -163,6 +171,13 @@ public final class TypeFactory implements Serializable {
}
public CustomType custom(Class<UserType> typeClass, Properties parameters) {
return custom( typeClass, parameters, typeScope );
}
/**
* @deprecated Only for use temporary use by {@link org.hibernate.Hibernate}
*/
public static CustomType custom(Class<UserType> typeClass, Properties parameters, TypeScope scope) {
try {
UserType userType = typeClass.newInstance();
injectParameters( userType, parameters );