HHH-7777 - Deprecate XmlRepresentableType

(cherry picked from commit 5ef8a667ff)
This commit is contained in:
Steve Ebersole 2012-11-12 12:13:52 -06:00
parent 83e122773d
commit 26a42cb365
2 changed files with 58 additions and 24 deletions

View File

@ -54,7 +54,10 @@ import org.hibernate.usertype.UserVersionType;
* @author Gavin King
* @author Steve Ebersole
*/
public class CustomType extends AbstractType implements IdentifierType, DiscriminatorType, VersionType, BasicType {
public class CustomType
extends AbstractType
implements IdentifierType, DiscriminatorType, VersionType, BasicType, StringRepresentableType {
private final UserType userType;
private final String name;
private final int[] types;
@ -163,20 +166,12 @@ public class CustomType extends AbstractType implements IdentifierType, Discrimi
@SuppressWarnings({ "UnusedDeclaration" })
public String toXMLString(Object value, SessionFactoryImplementor factory) {
if ( value == null ) {
return null;
}
if ( userType instanceof EnhancedUserType ) {
return ( (EnhancedUserType) userType ).toXMLString( value );
}
else {
return value.toString();
}
return toString( value );
}
@SuppressWarnings({ "UnusedDeclaration" })
public Object fromXMLString(String xml, Mapping factory) {
return ( (EnhancedUserType) userType ).fromXMLString(xml);
return fromStringValue( xml );
}
public String getName() {
@ -193,7 +188,7 @@ public class CustomType extends AbstractType implements IdentifierType, Discrimi
}
public Object stringToObject(String xml) {
return ( (EnhancedUserType) userType ).fromXMLString(xml);
return fromStringValue( xml );
}
public String objectToSQLString(Object value, Dialect dialect) throws Exception {
@ -246,4 +241,39 @@ public class CustomType extends AbstractType implements IdentifierType, Discrimi
throws HibernateException {
return checkable[0] && isDirty(old, current, session);
}
@Override
@SuppressWarnings("unchecked")
public String toString(Object value) throws HibernateException {
if ( StringRepresentableType.class.isInstance( userType ) ) {
return ( (StringRepresentableType) userType ).toString( value );
}
if ( value == null ) {
return null;
}
if ( EnhancedUserType.class.isInstance( userType ) ) {
//noinspection deprecation
return ( (EnhancedUserType) userType ).toXMLString( value );
}
return value.toString();
}
@Override
public Object fromStringValue(String string) throws HibernateException {
if ( StringRepresentableType.class.isInstance( userType ) ) {
return ( (StringRepresentableType) userType ).fromStringValue( string );
}
if ( EnhancedUserType.class.isInstance( userType ) ) {
//noinspection deprecation
return ( (EnhancedUserType) userType ).fromXMLString( string );
}
throw new HibernateException(
String.format(
"Could not process #fromStringValue, UserType class [%s] did not implement %s or %s",
name,
StringRepresentableType.class.getName(),
EnhancedUserType.class.getName()
)
);
}
}

View File

@ -1,10 +1,10 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008, Red Hat Middleware LLC or third-party contributors as
* Copyright (c) 2008, 2012, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Middleware LLC.
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
@ -20,15 +20,11 @@
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*
*/
package org.hibernate.usertype;
/**
* A custom type that may function as an identifier or
* discriminator type, or may be marshalled to and from
* an XML document
* A custom type that may function as an identifier or discriminator type
*
* @author Gavin King
*/
@ -39,13 +35,21 @@ public interface EnhancedUserType extends UserType {
public String objectToSQLString(Object value);
/**
* Return a string representation of this value, as it
* should appear in an XML document
* Return a string representation of this value, as it should appear in an XML document
*
* @deprecated To be removed in 5. Implement {@link org.hibernate.type.StringRepresentableType#toString(Object)}
* instead. See <a href="https://hibernate.onjira.com/browse/HHH-7776">HHH-7776</a> for details
*/
@Deprecated
public String toXMLString(Object value);
/**
* Parse a string representation of this value, as it
* appears in an XML document
* Parse a string representation of this value, as it appears in an XML document
*
* @deprecated To be removed in 5. Implement
* {@link org.hibernate.type.StringRepresentableType#fromStringValue(String)} instead.
* See <a href="https://hibernate.onjira.com/browse/HHH-7776">HHH-7776</a> for details
*/
@Deprecated
public Object fromXMLString(String xmlValue);
}