Fully generify and simplify UserType contract

This commit is contained in:
Christian Beikov 2022-03-04 08:26:17 +01:00
parent e5c719b843
commit bdf8b2fc2e
32 changed files with 373 additions and 396 deletions

View File

@ -24,36 +24,36 @@ import org.jboss.logging.Logger;
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
//tag::basic-custom-type-BitSetUserType-example[] //tag::basic-custom-type-BitSetUserType-example[]
public class BitSetUserType implements UserType<Object> { public class BitSetUserType implements UserType<BitSet> {
public static final BitSetUserType INSTANCE = new BitSetUserType(); public static final BitSetUserType INSTANCE = new BitSetUserType();
private static final Logger log = Logger.getLogger(BitSetUserType.class); private static final Logger log = Logger.getLogger(BitSetUserType.class);
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.VARCHAR }; return Types.VARCHAR;
} }
@Override @Override
public Class returnedClass() { public Class<BitSet> returnedClass() {
return BitSet.class; return BitSet.class;
} }
@Override @Override
public boolean equals(Object x, Object y) public boolean equals(BitSet x, BitSet y)
throws HibernateException { throws HibernateException {
return Objects.equals(x, y); return Objects.equals(x, y);
} }
@Override @Override
public int hashCode(Object x) public int hashCode(BitSet x)
throws HibernateException { throws HibernateException {
return Objects.hashCode(x); return Objects.hashCode(x);
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public BitSet nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
String columnValue = (String) rs.getObject(position); String columnValue = (String) rs.getObject(position);
if (rs.wasNull()) { if (rs.wasNull()) {
columnValue = null; columnValue = null;
@ -65,7 +65,7 @@ public class BitSetUserType implements UserType<Object> {
@Override @Override
public void nullSafeSet( public void nullSafeSet(
PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) PreparedStatement st, BitSet value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException { throws HibernateException, SQLException {
if (value == null) { if (value == null) {
log.debugv("Binding null to parameter {0} ",index); log.debugv("Binding null to parameter {0} ",index);
@ -79,7 +79,7 @@ public class BitSetUserType implements UserType<Object> {
} }
@Override @Override
public Object deepCopy(Object value) public BitSet deepCopy(BitSet value)
throws HibernateException { throws HibernateException {
return value == null ? null : return value == null ? null :
BitSet.valueOf(BitSet.class.cast(value).toLongArray()); BitSet.valueOf(BitSet.class.cast(value).toLongArray());
@ -91,19 +91,19 @@ public class BitSetUserType implements UserType<Object> {
} }
@Override @Override
public Serializable disassemble(Object value) public Serializable disassemble(BitSet value)
throws HibernateException { throws HibernateException {
return (BitSet) deepCopy(value); return deepCopy(value);
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) public BitSet assemble(Serializable cached, Object owner)
throws HibernateException { throws HibernateException {
return deepCopy(cached); return deepCopy( (BitSet) cached );
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) public BitSet replace(BitSet original, BitSet target, Object owner)
throws HibernateException { throws HibernateException {
return deepCopy(original); return deepCopy(original);
} }

View File

@ -134,12 +134,12 @@ public class CustomType<J>
@Override @Override
public boolean isEqual(Object x, Object y) throws HibernateException { public boolean isEqual(Object x, Object y) throws HibernateException {
return getUserType().equals( x, y ); return getUserType().equals( (J) x, (J) y );
} }
@Override @Override
public int getHashCode(Object x) { public int getHashCode(Object x) {
return getUserType().hashCode( x); return getUserType().hashCode( (J) x );
} }
@Override @Override
@ -149,7 +149,7 @@ public class CustomType<J>
@Override @Override
public Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) { public Serializable disassemble(Object value, SharedSessionContractImplementor session, Object owner) {
return getUserType().disassemble( value); return getUserType().disassemble( (J) value );
} }
@Override @Override
@ -159,7 +159,7 @@ public class CustomType<J>
SharedSessionContractImplementor session, SharedSessionContractImplementor session,
Object owner, Object owner,
Map<Object, Object> copyCache) throws HibernateException { Map<Object, Object> copyCache) throws HibernateException {
return getUserType().replace( original, target, owner ); return getUserType().replace( (J) original, (J) target, owner );
} }
@Override @Override
@ -192,7 +192,7 @@ public class CustomType<J>
@Override @Override
public Object deepCopy(Object value, SessionFactoryImplementor factory) throws HibernateException { public Object deepCopy(Object value, SessionFactoryImplementor factory) throws HibernateException {
return getUserType().deepCopy( value); return getUserType().deepCopy( (J) value );
} }
@Override @Override

View File

@ -344,9 +344,9 @@ public class EnumType<T extends Enum<T>>
} }
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
verifyConfigured(); verifyConfigured();
return new int[] { enumValueConverter.getJdbcTypeCode() }; return enumValueConverter.getJdbcTypeCode();
} }
@Override @Override
@ -355,12 +355,12 @@ public class EnumType<T extends Enum<T>>
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(T x, T y) throws HibernateException {
return x == y; return x == y;
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(T x) throws HibernateException {
return x == null ? 0 : x.hashCode(); return x == null ? 0 : x.hashCode();
} }
@ -384,7 +384,7 @@ public class EnumType<T extends Enum<T>>
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public T deepCopy(T value) throws HibernateException {
return value; return value;
} }
@ -394,17 +394,17 @@ public class EnumType<T extends Enum<T>>
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(T value) throws HibernateException {
return ( Serializable ) value; return value;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public T assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (T) cached;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public T replace(T original, T target, Object owner) throws HibernateException {
return original; return original;
} }

View File

@ -12,7 +12,6 @@ import java.util.Comparator;
import org.hibernate.SharedSessionContract; import org.hibernate.SharedSessionContract;
import org.hibernate.annotations.Immutable; import org.hibernate.annotations.Immutable;
import org.hibernate.dialect.Dialect; import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaType; import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan; import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
@ -70,7 +69,7 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
if ( userType.equals( first, second ) ) { if ( userType.equals( first, second ) ) {
return 0; return 0;
} }
return Comparator.comparing( userType::hashCode ).compare( first, second ); return Comparator.<J, Integer>comparing( userType::hashCode ).compare( first, second );
} }
@Override @Override
@ -80,7 +79,7 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
@Override @Override
public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) { public JdbcType getRecommendedJdbcType(JdbcTypeIndicators context) {
return context.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor( userType.sqlTypes()[0] ); return context.getTypeConfiguration().getJdbcTypeRegistry().getDescriptor( userType.getSqlType() );
} }
@Override @Override
@ -153,8 +152,7 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
@Override @Override
public J deepCopy(J value) { public J deepCopy(J value) {
//noinspection unchecked return userType.deepCopy( value );
return (J) userType.deepCopy( value );
} }
@Override @Override
@ -164,8 +162,7 @@ public class UserTypeJavaTypeWrapper<J> implements BasicJavaType<J> {
@Override @Override
public J assemble(Serializable cached, SharedSessionContract session) { public J assemble(Serializable cached, SharedSessionContract session) {
//noinspection unchecked return userType.assemble( cached , session);
return (J) userType.disassemble( cached );
} }
} }
} }

View File

@ -53,7 +53,7 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
@Override @Override
public int getJdbcTypeCode() { public int getJdbcTypeCode() {
return userType.sqlTypes()[0]; return userType.getSqlType();
} }
@Override @Override
@ -142,10 +142,10 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
} }
if ( extracted == null ) { if ( extracted == null ) {
JdbcExtractingLogging.logNullExtracted( paramIndex, userType.sqlTypes()[0] ); JdbcExtractingLogging.logNullExtracted( paramIndex, userType.getSqlType() );
} }
else { else {
JdbcExtractingLogging.logExtracted( paramIndex, userType.sqlTypes()[0], extracted ); JdbcExtractingLogging.logExtracted( paramIndex, userType.getSqlType(), extracted );
} }
} }
@ -155,10 +155,10 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
} }
if ( extracted == null ) { if ( extracted == null ) {
JdbcExtractingLogging.logNullExtracted( paramName, userType.sqlTypes()[0] ); JdbcExtractingLogging.logNullExtracted( paramName, userType.getSqlType() );
} }
else { else {
JdbcExtractingLogging.logExtracted( paramName, userType.sqlTypes()[0], extracted ); JdbcExtractingLogging.logExtracted( paramName, userType.getSqlType(), extracted );
} }
} }
} }
@ -174,10 +174,10 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
public void bind(PreparedStatement st, J value, int index, WrapperOptions options) throws SQLException { public void bind(PreparedStatement st, J value, int index, WrapperOptions options) throws SQLException {
if ( JdbcBindingLogging.TRACE_ENABLED ) { if ( JdbcBindingLogging.TRACE_ENABLED ) {
if ( value == null ) { if ( value == null ) {
JdbcBindingLogging.logNullBinding( index, userType.sqlTypes()[ 0 ] ); JdbcBindingLogging.logNullBinding( index, userType.getSqlType() );
} }
else { else {
JdbcBindingLogging.logBinding( index, userType.sqlTypes()[ 0 ], value ); JdbcBindingLogging.logBinding( index, userType.getSqlType(), value );
} }
} }
userType.nullSafeSet( st, value, index, options.getSession() ); userType.nullSafeSet( st, value, index, options.getSession() );

View File

@ -28,8 +28,6 @@ public abstract class BaseUserTypeSupport<T> implements UserType<T> {
private boolean resolved; private boolean resolved;
// cached array wrapping our single type-code
private int[] sqlTypes;
// cached JDBC extractor and binder // cached JDBC extractor and binder
private ValueExtractor<T> jdbcValueExtractor; private ValueExtractor<T> jdbcValueExtractor;
private ValueBinder<T> jdbcValueBinder; private ValueBinder<T> jdbcValueBinder;
@ -45,8 +43,6 @@ public abstract class BaseUserTypeSupport<T> implements UserType<T> {
this.javaType = javaType; this.javaType = javaType;
this.jdbcType = jdbcType; this.jdbcType = jdbcType;
sqlTypes = new int[] { jdbcType.getJdbcTypeCode() };
jdbcValueExtractor = jdbcType.getExtractor( javaType ); jdbcValueExtractor = jdbcType.getExtractor( javaType );
jdbcValueBinder = jdbcType.getBinder( javaType ); jdbcValueBinder = jdbcType.getBinder( javaType );
@ -65,9 +61,9 @@ public abstract class BaseUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
ensureResolved(); ensureResolved();
return sqlTypes; return jdbcType.getDefaultSqlTypeCode();
} }
@Override @Override
@ -76,15 +72,13 @@ public abstract class BaseUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(T x, T y) throws HibernateException {
//noinspection unchecked return javaType().areEqual( x, y );
return javaType().areEqual( (T) x, (T) y );
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(T x) throws HibernateException {
//noinspection unchecked return javaType().extractHashCode( x );
return javaType().extractHashCode( (T) x );
} }
@Override @Override
@ -100,9 +94,8 @@ public abstract class BaseUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public T deepCopy(T value) throws HibernateException {
//noinspection unchecked return javaType().getMutabilityPlan().deepCopy( value );
return javaType().getMutabilityPlan().deepCopy( (T) value );
} }
@Override @Override
@ -111,18 +104,17 @@ public abstract class BaseUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(T value) throws HibernateException {
//noinspection unchecked return javaType().getMutabilityPlan().disassemble( value, null );
return javaType().getMutabilityPlan().disassemble( (T) value, null );
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public T assemble(Serializable cached, Object owner) throws HibernateException {
return javaType().getMutabilityPlan().assemble( cached, null ); return javaType().getMutabilityPlan().assemble( cached, null );
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public T replace(T original, T target, Object owner) throws HibernateException {
return deepCopy( original ); return deepCopy( original );
} }
} }

View File

@ -17,6 +17,7 @@ import org.hibernate.metamodel.model.convert.spi.BasicValueConverter;
import org.hibernate.type.descriptor.ValueBinder; import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.java.BasicJavaType; import org.hibernate.type.descriptor.java.BasicJavaType;
import org.hibernate.type.descriptor.java.JavaType;
import org.hibernate.type.descriptor.java.MutabilityPlan; import org.hibernate.type.descriptor.java.MutabilityPlan;
import org.hibernate.type.descriptor.jdbc.JdbcType; import org.hibernate.type.descriptor.jdbc.JdbcType;
@ -24,46 +25,49 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
* @author Steve Ebersole * @author Steve Ebersole
*/ */
public class StaticUserTypeSupport<T> implements UserType<T> { public class StaticUserTypeSupport<T> implements UserType<T> {
private final BasicJavaType javaType; private final BasicJavaType<T> javaType;
private final JdbcType jdbcType; private final JdbcType jdbcType;
private final MutabilityPlan mutabilityPlan; private final MutabilityPlan<T> mutabilityPlan;
private final BasicValueConverter valueConverter; private final BasicValueConverter<T, Object> valueConverter;
private final int[] sqlTypes; private final ValueExtractor<Object> jdbcValueExtractor;
private ValueExtractor jdbcValueExtractor; private final ValueBinder<Object> jdbcValueBinder;
private ValueBinder jdbcValueBinder;
public StaticUserTypeSupport(BasicJavaType javaType, JdbcType jdbcType) { public StaticUserTypeSupport(BasicJavaType<T> javaType, JdbcType jdbcType) {
this( javaType, jdbcType, javaType.getMutabilityPlan() ); this( javaType, jdbcType, javaType.getMutabilityPlan() );
} }
public StaticUserTypeSupport( public StaticUserTypeSupport(
BasicJavaType javaType, BasicJavaType<T> javaType,
JdbcType jdbcType, JdbcType jdbcType,
MutabilityPlan mutabilityPlan) { MutabilityPlan<T> mutabilityPlan) {
this( javaType, jdbcType, mutabilityPlan, null ); this( javaType, jdbcType, mutabilityPlan, null );
} }
public StaticUserTypeSupport( public StaticUserTypeSupport(
BasicJavaType javaType, BasicJavaType<T> javaType,
JdbcType jdbcType, JdbcType jdbcType,
BasicValueConverter valueConverter) { BasicValueConverter<T, Object> valueConverter) {
this( javaType, jdbcType, javaType.getMutabilityPlan(), valueConverter ); this( javaType, jdbcType, javaType.getMutabilityPlan(), valueConverter );
} }
public StaticUserTypeSupport(BasicJavaType javaType, JdbcType jdbcType, MutabilityPlan mutabilityPlan, BasicValueConverter valueConverter) { public StaticUserTypeSupport(
BasicJavaType<T> javaType,
JdbcType jdbcType,
MutabilityPlan<T> mutabilityPlan,
BasicValueConverter<T, Object> valueConverter) {
this.javaType = javaType; this.javaType = javaType;
this.jdbcType = jdbcType; this.jdbcType = jdbcType;
this.mutabilityPlan = mutabilityPlan; this.mutabilityPlan = mutabilityPlan;
this.valueConverter = valueConverter; this.valueConverter = valueConverter;
this.sqlTypes = new int[] { jdbcType.getJdbcTypeCode() }; //noinspection unchecked
this.jdbcValueExtractor = jdbcType.getExtractor( (JavaType<Object>) javaType );
this.jdbcValueExtractor = jdbcType.getExtractor( javaType ); //noinspection unchecked
this.jdbcValueBinder = jdbcType.getBinder( javaType ); this.jdbcValueBinder = jdbcType.getBinder( (JavaType<Object>) javaType );
} }
public BasicJavaType getJavaType() { public BasicJavaType<T> getJavaType() {
return javaType; return javaType;
} }
@ -71,25 +75,25 @@ public class StaticUserTypeSupport<T> implements UserType<T> {
return jdbcType; return jdbcType;
} }
public MutabilityPlan getMutabilityPlan() { public MutabilityPlan<T> getMutabilityPlan() {
return mutabilityPlan; return mutabilityPlan;
} }
public BasicValueConverter getValueConverter() { public BasicValueConverter<T, Object> getValueConverter() {
return valueConverter; return valueConverter;
} }
public ValueExtractor getJdbcValueExtractor() { public ValueExtractor<Object> getJdbcValueExtractor() {
return jdbcValueExtractor; return jdbcValueExtractor;
} }
public ValueBinder getJdbcValueBinder() { public ValueBinder<Object> getJdbcValueBinder() {
return jdbcValueBinder; return jdbcValueBinder;
} }
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return sqlTypes; return jdbcType.getDefaultSqlTypeCode();
} }
@Override @Override
@ -98,34 +102,32 @@ public class StaticUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(T x, T y) throws HibernateException {
//noinspection unchecked return javaType.areEqual( x, y );
return javaType.areEqual( (T) x, (T) y );
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(T x) throws HibernateException {
//noinspection unchecked return javaType.extractHashCode( x );
return javaType.extractHashCode( (T) x );
} }
@Override @Override
@SuppressWarnings("unchecked")
public T nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public T nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
final Object extracted = jdbcValueExtractor.extract( rs, position, session ); final Object extracted = jdbcValueExtractor.extract( rs, position, session );
if ( valueConverter != null ) { if ( valueConverter != null ) {
return (T) valueConverter.toDomainValue( extracted ); return valueConverter.toDomainValue( extracted );
} }
//noinspection unchecked
return (T) extracted; return (T) extracted;
} }
@Override @Override
public void nullSafeSet(PreparedStatement st, T value, int index, SharedSessionContractImplementor session) throws SQLException { public void nullSafeSet(PreparedStatement st, T value, int index, SharedSessionContractImplementor session) throws SQLException {
final T valueToBind; final Object valueToBind;
if ( valueConverter != null ) { if ( valueConverter != null ) {
valueToBind = (T) valueConverter.toRelationalValue( value ); valueToBind = valueConverter.toRelationalValue( value );
} }
else { else {
valueToBind = value; valueToBind = value;
@ -135,9 +137,8 @@ public class StaticUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public T deepCopy(T value) throws HibernateException {
//noinspection unchecked return javaType.getMutabilityPlan().deepCopy( value );
return javaType.getMutabilityPlan().deepCopy( (T) value );
} }
@Override @Override
@ -146,18 +147,17 @@ public class StaticUserTypeSupport<T> implements UserType<T> {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(T value) throws HibernateException {
//noinspection unchecked return javaType.getMutabilityPlan().disassemble( value, null );
return javaType.getMutabilityPlan().disassemble( (T) value, null );
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public T assemble(Serializable cached, Object owner) throws HibernateException {
return javaType.getMutabilityPlan().assemble( cached, null ); return javaType.getMutabilityPlan().assemble( cached, null );
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public T replace(T original, T target, Object owner) throws HibernateException {
return deepCopy( original ); return deepCopy( original );
} }

View File

@ -53,13 +53,13 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
public interface UserType<J> { public interface UserType<J> {
/** /**
* Return the SQL type codes for the columns mapped by this type. The * Return the SQL type code for the column mapped by this type. The
* codes are generally defined on {@code java.sql.Types}, but could * codes are generally defined on {@code org.hibernate.type.SqlTypes}, but could
* be database-specific codes * be database-specific codes
* *
* @see java.sql.Types * @see org.hibernate.type.SqlTypes
*/ */
int[] sqlTypes(); int getSqlType();
/** /**
* The class returned by {@code nullSafeGet()}. * The class returned by {@code nullSafeGet()}.
@ -72,12 +72,12 @@ public interface UserType<J> {
* Compare two instances of the class mapped by this type for persistence "equality". * Compare two instances of the class mapped by this type for persistence "equality".
* Equality of the persistent state. * Equality of the persistent state.
*/ */
boolean equals(Object x, Object y); boolean equals(J x, J y);
/** /**
* Get a hashcode for the instance, consistent with persistence "equality" * Get a hashcode for the instance, consistent with persistence "equality"
*/ */
int hashCode(Object x); int hashCode(J x);
/** /**
* Retrieve an instance of the mapped class from a JDBC resultset. Implementors * Retrieve an instance of the mapped class from a JDBC resultset. Implementors
@ -100,7 +100,7 @@ public interface UserType<J> {
* @param value the object to be cloned, which may be null * @param value the object to be cloned, which may be null
* @return Object a copy * @return Object a copy
*/ */
Object deepCopy(Object value); J deepCopy(J value);
/** /**
* Are objects of this type mutable? * Are objects of this type mutable?
@ -118,7 +118,7 @@ public interface UserType<J> {
* @param value the object to be cached * @param value the object to be cached
* @return a cacheable representation of the object * @return a cacheable representation of the object
*/ */
Serializable disassemble(Object value); Serializable disassemble(J value);
/** /**
* Reconstruct an object from the cacheable representation. At the very least this * Reconstruct an object from the cacheable representation. At the very least this
@ -128,7 +128,7 @@ public interface UserType<J> {
* @param owner the owner of the cached object * @param owner the owner of the cached object
* @return a reconstructed object from the cacheable representation * @return a reconstructed object from the cacheable representation
*/ */
Object assemble(Serializable cached, Object owner); J assemble(Serializable cached, Object owner);
/** /**
* During merge, replace the existing (target) value in the entity we are merging to * During merge, replace the existing (target) value in the entity we are merging to
@ -142,7 +142,7 @@ public interface UserType<J> {
* *
* @return the value to be merged * @return the value to be merged
*/ */
Object replace(Object detached, Object managed, Object owner); J replace(J detached, J managed, Object owner);
default long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) { default long getDefaultSqlLength(Dialect dialect, JdbcType jdbcType) {
return Size.DEFAULT_LENGTH; return Size.DEFAULT_LENGTH;

View File

@ -19,11 +19,11 @@ import org.hibernate.usertype.UserType;
/** /**
* @author Chris Pheby * @author Chris Pheby
*/ */
public class DollarValueUserType implements UserType { public class DollarValueUserType implements UserType<DollarValue> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] {Types.BIGINT}; return Types.BIGINT;
} }
@Override @Override
@ -32,37 +32,31 @@ public class DollarValueUserType implements UserType {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(DollarValue x, DollarValue y) throws HibernateException {
if (!(x instanceof DollarValue) || !(y instanceof DollarValue)) { return x.getAmount().equals(y.getAmount());
throw new HibernateException("Expected DollarValue");
}
return ((DollarValue)x).getAmount().equals(((DollarValue)y).getAmount());
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(DollarValue x) throws HibernateException {
if (!(x instanceof DollarValue)) { return x.getAmount().hashCode();
throw new HibernateException("Expected DollarValue");
}
return ((DollarValue)x).getAmount().hashCode();
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public DollarValue nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
return new DollarValue( rs.getBigDecimal( position ) ); return new DollarValue( rs.getBigDecimal( position ) );
} }
@Override @Override
public void nullSafeSet( public void nullSafeSet(
PreparedStatement st, PreparedStatement st,
Object value, DollarValue value,
int index, int index,
SharedSessionContractImplementor session) throws HibernateException, SQLException { SharedSessionContractImplementor session) throws HibernateException, SQLException {
st.setBigDecimal(index, ((DollarValue)value).getAmount()); st.setBigDecimal(index, value.getAmount());
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public DollarValue deepCopy(DollarValue value) throws HibernateException {
return new DollarValue(); return new DollarValue();
} }
@ -72,18 +66,18 @@ public class DollarValueUserType implements UserType {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(DollarValue value) throws HibernateException {
return null; return null;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) public DollarValue assemble(Serializable cached, Object owner)
throws HibernateException { throws HibernateException {
return null; return null;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) public DollarValue replace(DollarValue original, DollarValue target, Object owner)
throws HibernateException { throws HibernateException {
return null; return null;
} }

View File

@ -19,11 +19,11 @@ import org.hibernate.usertype.UserType;
/** /**
* @author Chris Pheby * @author Chris Pheby
*/ */
public class MyDateUserType implements UserType { public class MyDateUserType implements UserType<MyDate> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] {Types.DATE}; return Types.DATE;
} }
@Override @Override
@ -32,37 +32,31 @@ public class MyDateUserType implements UserType {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(MyDate x, MyDate y) throws HibernateException {
if (!(x instanceof MyDate) || !(y instanceof MyDate)) { return x.getDate().equals(y.getDate());
throw new HibernateException("Expected MyDate");
}
return ((MyDate)x).getDate().equals(((MyDate)y).getDate());
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(MyDate x) throws HibernateException {
if (!(x instanceof MyDate)) { return x.getDate().hashCode();
throw new HibernateException("Expected MyDate");
}
return ((MyDate)x).getDate().hashCode();
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public MyDate nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
return new MyDate( rs.getDate( position ) ); return new MyDate( rs.getDate( position ) );
} }
@Override @Override
public void nullSafeSet( public void nullSafeSet(
PreparedStatement st, PreparedStatement st,
Object value, MyDate value,
int index, int index,
SharedSessionContractImplementor session) throws HibernateException, SQLException { SharedSessionContractImplementor session) throws HibernateException, SQLException {
st.setDate(index, new java.sql.Date(((MyDate)value).getDate().getTime())); st.setDate(index, new java.sql.Date(value.getDate().getTime()));
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public MyDate deepCopy(MyDate value) throws HibernateException {
MyDate result = new MyDate(); MyDate result = new MyDate();
return result; return result;
@ -74,18 +68,18 @@ public class MyDateUserType implements UserType {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(MyDate value) throws HibernateException {
return null; return null;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) public MyDate assemble(Serializable cached, Object owner)
throws HibernateException { throws HibernateException {
return null; return null;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) public MyDate replace(MyDate original, MyDate target, Object owner)
throws HibernateException { throws HibernateException {
return null; return null;
} }

View File

@ -76,14 +76,13 @@ public class EnumeratedSmokeTest extends BaseUnitTestCase {
assertThat( customType.getUserType(), instanceOf( org.hibernate.type.EnumType.class ) ); assertThat( customType.getUserType(), instanceOf( org.hibernate.type.EnumType.class ) );
final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType(); final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType();
assertThat( hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType==EnumType.ORDINAL) ); assertThat( hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType==EnumType.ORDINAL) );
assertThat( hibernateMappingEnumType.sqlTypes().length, is(1) );
final int expectedJdbcTypeCode = jdbcRegistry.getDescriptor( final int expectedJdbcTypeCode = jdbcRegistry.getDescriptor(
expectedJpaEnumType == EnumType.ORDINAL ? expectedJpaEnumType == EnumType.ORDINAL ?
Types.TINYINT : Types.TINYINT :
Types.VARCHAR Types.VARCHAR
).getJdbcTypeCode(); ).getJdbcTypeCode();
assertThat( assertThat(
hibernateMappingEnumType.sqlTypes()[0], hibernateMappingEnumType.getSqlType(),
is( expectedJdbcTypeCode ) is( expectedJdbcTypeCode )
); );
} }

View File

@ -21,8 +21,8 @@ import org.hibernate.orm.test.annotations.enumerated.enums.FirstLetter;
public class FirstLetterType extends org.hibernate.type.EnumType<FirstLetter> { public class FirstLetterType extends org.hibernate.type.EnumType<FirstLetter> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.VARCHAR }; return Types.VARCHAR;
} }
@Override @Override
@ -38,7 +38,7 @@ public class FirstLetterType extends org.hibernate.type.EnumType<FirstLetter> {
public void nullSafeSet(PreparedStatement st, FirstLetter value, int index, SharedSessionContractImplementor session) public void nullSafeSet(PreparedStatement st, FirstLetter value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException { throws HibernateException, SQLException {
if ( value == null ) { if ( value == null ) {
st.setNull( index, sqlTypes()[0] ); st.setNull( index, getSqlType() );
} }
else { else {
String enumString = value.name(); String enumString = value.name();

View File

@ -21,8 +21,8 @@ import org.hibernate.orm.test.annotations.enumerated.enums.LastNumber;
public class LastNumberType extends org.hibernate.type.EnumType<LastNumber> { public class LastNumberType extends org.hibernate.type.EnumType<LastNumber> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.VARCHAR }; return Types.VARCHAR;
} }
@Override @Override
@ -38,7 +38,7 @@ public class LastNumberType extends org.hibernate.type.EnumType<LastNumber> {
public void nullSafeSet(PreparedStatement st, LastNumber value, int index, SharedSessionContractImplementor session) public void nullSafeSet(PreparedStatement st, LastNumber value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException { throws HibernateException, SQLException {
if ( value == null ) { if ( value == null ) {
st.setNull( index, sqlTypes()[0] ); st.setNull( index, getSqlType() );
} }
else { else {
String enumString = value.name(); String enumString = value.name();

View File

@ -73,14 +73,14 @@ public class EnumeratedWithMappedSuperclassTest extends BaseUnitTestCase {
final Property natureProperty = addressLevelBinding.getProperty( "nature" ); final Property natureProperty = addressLevelBinding.getProperty( "nature" );
CustomType<Object> customType = assertTyping( CustomType.class, natureProperty.getType() ); CustomType<Object> customType = assertTyping( CustomType.class, natureProperty.getType() );
EnumType enumType = assertTyping( EnumType.class, customType.getUserType() ); EnumType enumType = assertTyping( EnumType.class, customType.getUserType() );
assertEquals( Types.VARCHAR, enumType.sqlTypes()[0] ); assertEquals( Types.VARCHAR, enumType.getSqlType() );
SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory(); SessionFactoryImplementor sf = (SessionFactoryImplementor) metadata.buildSessionFactory();
try { try {
EntityPersister p = sf.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(AddressLevel.class.getName()); EntityPersister p = sf.getRuntimeMetamodels().getMappingMetamodel().getEntityDescriptor(AddressLevel.class.getName());
CustomType<Object> runtimeType = assertTyping( CustomType.class, p.getPropertyType( "nature" ) ); CustomType<Object> runtimeType = assertTyping( CustomType.class, p.getPropertyType( "nature" ) );
EnumType runtimeEnumType = assertTyping( EnumType.class, runtimeType.getUserType() ); EnumType runtimeEnumType = assertTyping( EnumType.class, runtimeType.getUserType() );
assertEquals( Types.VARCHAR, runtimeEnumType.sqlTypes()[0] ); assertEquals( Types.VARCHAR, runtimeEnumType.getSqlType() );
} }
finally { finally {
sf.close(); sf.close();

View File

@ -22,21 +22,21 @@ import org.hibernate.usertype.UserType;
* @author Emmanuel Bernard * @author Emmanuel Bernard
*/ */
public class StateType implements UserType<State> { public class StateType implements UserType<State> {
public int[] sqlTypes() {
return new int[] { @Override
Types.INTEGER public int getSqlType() {
}; return Types.INTEGER;
} }
public Class returnedClass() { public Class<State> returnedClass() {
return State.class; return State.class;
} }
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(State x, State y) throws HibernateException {
return x == y; return x == y;
} }
public int hashCode(Object x) throws HibernateException { public int hashCode(State x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
@ -57,7 +57,7 @@ public class StateType implements UserType<State> {
} }
} }
public Object deepCopy(Object value) throws HibernateException { public State deepCopy(State value) throws HibernateException {
return value; return value;
} }
@ -65,15 +65,15 @@ public class StateType implements UserType<State> {
return false; return false;
} }
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(State value) throws HibernateException {
return (Serializable) value; return value;
} }
public Object assemble(Serializable cached, Object owner) throws HibernateException { public State assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (State) cached;
} }
public Object replace(Object original, Object target, Object owner) throws HibernateException { public State replace(State original, State target, Object owner) throws HibernateException {
return original; return original;
} }
} }

View File

@ -106,8 +106,8 @@ public class MyStringType implements UserType<String>, DynamicParameterizedType
} }
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.VARCHAR }; return Types.VARCHAR;
} }
@Override @Override
@ -116,17 +116,17 @@ public class MyStringType implements UserType<String>, DynamicParameterizedType
} }
@Override @Override
public boolean equals(Object x, Object y) { public boolean equals(String x, String y) {
return ( x == null && y == null ) || ( x != null && y != null && x.equals( y ) ); return ( x == null && y == null ) || ( x != null && y != null && x.equals( y ) );
} }
@Override @Override
public int hashCode(Object x) { public int hashCode(String x) {
return x.hashCode(); return x.hashCode();
} }
@Override @Override
public Object deepCopy(Object value) { public String deepCopy(String value) {
return value; return value;
} }
@ -136,17 +136,17 @@ public class MyStringType implements UserType<String>, DynamicParameterizedType
} }
@Override @Override
public Serializable disassemble(Object value) { public Serializable disassemble(String value) {
return (Integer) value; return value;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) { public String assemble(Serializable cached, Object owner) {
return cached; return (String) cached;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) { public String replace(String original, String target, Object owner) {
return original; return original;
} }
} }

View File

@ -15,7 +15,6 @@ import java.sql.Types;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.descriptor.ValueExtractor; import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions; import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.usertype.EnhancedUserType; import org.hibernate.usertype.EnhancedUserType;
@ -32,17 +31,17 @@ import org.hibernate.usertype.EnhancedUserType;
public class ClassificationType implements EnhancedUserType<Classification>, ValueExtractor<Classification> { public class ClassificationType implements EnhancedUserType<Classification>, ValueExtractor<Classification> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.TINYINT }; return Types.TINYINT;
} }
@Override @Override
public Class returnedClass() { public Class<Classification> returnedClass() {
return Classification.class; return Classification.class;
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(Classification x, Classification y) throws HibernateException {
if ( x == null && y == null ) { if ( x == null && y == null ) {
return false; return false;
} }
@ -55,7 +54,7 @@ public class ClassificationType implements EnhancedUserType<Classification>, Val
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(Classification x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
@ -79,7 +78,7 @@ public class ClassificationType implements EnhancedUserType<Classification>, Val
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public Classification deepCopy(Classification value) throws HibernateException {
return value; return value;
} }
@ -89,17 +88,17 @@ public class ClassificationType implements EnhancedUserType<Classification>, Val
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Classification value) throws HibernateException {
return ( Classification ) value; return ( Classification ) value;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public Classification assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (Classification) cached;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public Classification replace(Classification original, Classification target, Object owner) throws HibernateException {
return original; return original;
} }

View File

@ -125,8 +125,8 @@ public class UserTypeComparableIdTest {
public static class CustomIdType implements UserType<CustomId>, Comparator<CustomId> { public static class CustomIdType implements UserType<CustomId>, Comparator<CustomId> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.BIGINT }; return Types.BIGINT;
} }
@Override @Override
@ -162,17 +162,17 @@ public class UserTypeComparableIdTest {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(CustomId x, CustomId y) throws HibernateException {
return x.equals( y ); return x.equals( y );
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(CustomId x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public CustomId deepCopy(CustomId value) throws HibernateException {
return value; return value;
} }
@ -182,17 +182,17 @@ public class UserTypeComparableIdTest {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(CustomId value) throws HibernateException {
return (Serializable) value; return value;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public CustomId assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (CustomId) cached;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public CustomId replace(CustomId original, CustomId target, Object owner) throws HibernateException {
return original; return original;
} }
} }

View File

@ -112,8 +112,8 @@ public class UserTypeNonComparableIdTest {
public static class CustomIdType implements UserType<CustomId> { public static class CustomIdType implements UserType<CustomId> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.BIGINT }; return Types.BIGINT;
} }
@Override @Override
@ -139,22 +139,22 @@ public class UserTypeNonComparableIdTest {
} }
@Override @Override
public Class returnedClass() { public Class<CustomId> returnedClass() {
return CustomId.class; return CustomId.class;
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(CustomId x, CustomId y) throws HibernateException {
return x.equals( y ); return x.equals( y );
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(CustomId x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public CustomId deepCopy(CustomId value) throws HibernateException {
return value; return value;
} }
@ -164,17 +164,17 @@ public class UserTypeNonComparableIdTest {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(CustomId value) throws HibernateException {
return (Serializable) value; return value;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public CustomId assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (CustomId) cached;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public CustomId replace(CustomId original, CustomId target, Object owner) throws HibernateException {
return original; return original;
} }
} }

View File

@ -113,8 +113,8 @@ public class QueryParametersValidationTest extends BaseEntityManagerFunctionalTe
public static class BooleanUserType implements UserType<Boolean> { public static class BooleanUserType implements UserType<Boolean> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.CHAR }; return Types.CHAR;
} }
@Override @Override
@ -123,12 +123,12 @@ public class QueryParametersValidationTest extends BaseEntityManagerFunctionalTe
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(Boolean x, Boolean y) throws HibernateException {
return Objects.equals( x, y); return Objects.equals( x, y);
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(Boolean x) throws HibernateException {
return Objects.hashCode(x); return Objects.hashCode(x);
} }
@ -147,7 +147,7 @@ public class QueryParametersValidationTest extends BaseEntityManagerFunctionalTe
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public Boolean deepCopy(Boolean value) throws HibernateException {
return value; return value;
} }
@ -157,17 +157,17 @@ public class QueryParametersValidationTest extends BaseEntityManagerFunctionalTe
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Boolean value) throws HibernateException {
return null; return null;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public Boolean assemble(Serializable cached, Object owner) throws HibernateException {
return null; return null;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public Boolean replace(Boolean original, Boolean target, Object owner) throws HibernateException {
return null; return null;
} }
} }

View File

@ -10,6 +10,7 @@ import java.io.Serializable;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -20,6 +21,7 @@ import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.query.TypedParameterValue; import org.hibernate.query.TypedParameterValue;
import org.hibernate.type.CustomType; import org.hibernate.type.CustomType;
import org.hibernate.type.SqlTypes;
import org.hibernate.usertype.UserType; import org.hibernate.usertype.UserType;
import org.hibernate.testing.orm.junit.EntityManagerFactoryScope; import org.hibernate.testing.orm.junit.EntityManagerFactoryScope;
@ -139,12 +141,10 @@ public class TypedValueParametersTest {
public static class TagUserType implements UserType<List<String>> { public static class TagUserType implements UserType<List<String>> {
public static final TagUserType INSTANCE = new TagUserType(); public static final TagUserType INSTANCE = new TagUserType();
private final int SQLTYPE = java.sql.Types.VARCHAR;
@Override @Override
public void nullSafeSet(PreparedStatement statement, List<String> list, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { public void nullSafeSet(PreparedStatement statement, List<String> list, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
if ( list == null ) { if ( list == null ) {
statement.setNull(index, SQLTYPE); statement.setNull( index, SqlTypes.VARCHAR );
} }
else { else {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -183,8 +183,10 @@ public class TypedValueParametersTest {
return list; return list;
} }
public int[] sqlTypes() {
return new int[]{SQLTYPE}; @Override
public int getSqlType() {
return Types.VARCHAR;
} }
@Override @Override
@ -194,28 +196,28 @@ public class TypedValueParametersTest {
} }
@Override @Override
public Object assemble(final Serializable cached, final Object owner) throws HibernateException { public List<String> assemble(final Serializable cached, final Object owner) throws HibernateException {
return cached; return (List<String>) cached;
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public Object deepCopy(final Object o) throws HibernateException { public List<String> deepCopy(final List<String> o) throws HibernateException {
return o == null ? null : new ArrayList<>((List<String>) o); return o == null ? null : new ArrayList<>( o );
} }
@Override @Override
public Serializable disassemble(final Object o) throws HibernateException { public Serializable disassemble(final List<String> o) throws HibernateException {
return (Serializable) o; return (Serializable) o;
} }
@Override @Override
public boolean equals(final Object x, final Object y) throws HibernateException { public boolean equals(final List<String> x, final List<String> y) throws HibernateException {
return x == null ? y == null : x.equals(y); return x == null ? y == null : x.equals(y);
} }
@Override @Override
public int hashCode(final Object o) throws HibernateException { public int hashCode(final List<String> o) throws HibernateException {
return o == null ? 0 : o.hashCode(); return o == null ? 0 : o.hashCode();
} }
@ -225,7 +227,7 @@ public class TypedValueParametersTest {
} }
@Override @Override
public Object replace(final Object original, final Object target, final Object owner) throws HibernateException { public List<String> replace(final List<String> original, final List<String> target, final Object owner) throws HibernateException {
return original; return original;
} }

View File

@ -19,15 +19,15 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.ParameterizedType; import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType; import org.hibernate.usertype.UserType;
public class EnumUserType implements UserType, ParameterizedType { public class EnumUserType<T extends Enum<T>> implements UserType<T>, ParameterizedType {
private Class clazz = null; private Class<T> clazz = null;
public static EnumUserType createInstance(Class clazz) { public static <T extends Enum<T>> EnumUserType<T> createInstance(Class<T> clazz) {
if ( !clazz.isEnum() ) { if ( !clazz.isEnum() ) {
throw new IllegalArgumentException( "Parameter has to be an enum-class" ); throw new IllegalArgumentException( "Parameter has to be an enum-class" );
} }
EnumUserType that = new EnumUserType(); EnumUserType<T> that = new EnumUserType<>();
Properties p = new Properties(); Properties p = new Properties();
p.setProperty( "enumClassName", clazz.getName() ); p.setProperty( "enumClassName", clazz.getName() );
that.setParameterValues( p ); that.setParameterValues( p );
@ -41,7 +41,8 @@ public class EnumUserType implements UserType, ParameterizedType {
} }
try { try {
this.clazz = Class.forName( enumClassName ); //noinspection unchecked
this.clazz = (Class<T>) Class.forName( enumClassName );
} }
catch (ClassNotFoundException e) { catch (ClassNotFoundException e) {
throw new MappingException( "enumClass " + enumClassName + " not found", e ); throw new MappingException( "enumClass " + enumClassName + " not found", e );
@ -51,18 +52,17 @@ public class EnumUserType implements UserType, ParameterizedType {
} }
} }
private static final int[] SQL_TYPES = {Types.CHAR}; @Override
public int getSqlType() {
public int[] sqlTypes() { return Types.CHAR;
return SQL_TYPES;
} }
public Class returnedClass() { public Class<T> returnedClass() {
return clazz; return clazz;
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public T nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
final String name = rs.getString( position ); final String name = rs.getString( position );
if ( rs.wasNull() ) { if ( rs.wasNull() ) {
return null; return null;
@ -72,24 +72,23 @@ public class EnumUserType implements UserType, ParameterizedType {
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index) public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException { throws HibernateException, SQLException {
if ( null == value ) {
preparedStatement.setNull( index, Types.VARCHAR );
}
else {
preparedStatement.setString( index, ( (Enum) value ).name() );
}
} }
@Override @Override
public void nullSafeSet( public void nullSafeSet(
PreparedStatement preparedStatement, PreparedStatement preparedStatement,
Object value, T value,
int index, int index,
SharedSessionContractImplementor session) throws HibernateException, SQLException { SharedSessionContractImplementor session) throws HibernateException, SQLException {
nullSafeSet( preparedStatement, value, index ); if ( null == value ) {
preparedStatement.setNull( index, Types.VARCHAR );
}
else {
preparedStatement.setString( index, value.name() );
}
} }
public Object deepCopy(Object value) throws HibernateException { public T deepCopy(T value) throws HibernateException {
return value; return value;
} }
@ -97,23 +96,24 @@ public class EnumUserType implements UserType, ParameterizedType {
return false; return false;
} }
public Object assemble(Serializable cached, Object owner) throws HibernateException { public T assemble(Serializable cached, Object owner) throws HibernateException {
return cached; //noinspection unchecked
return (T) cached;
} }
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(T value) throws HibernateException {
return (Serializable) value; return value;
} }
public Object replace(Object original, Object target, Object owner) throws HibernateException { public T replace(T original, T target, Object owner) throws HibernateException {
return original; return original;
} }
public int hashCode(Object x) throws HibernateException { public int hashCode(T x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(T x, T y) throws HibernateException {
if ( x == y ) { if ( x == y ) {
return true; return true;
} }

View File

@ -21,13 +21,14 @@ import org.hibernate.usertype.UserType;
/** /**
* @author Gavin King * @author Gavin King
*/ */
public class RowIdType implements UserType { public class RowIdType implements UserType<Object>{
public int[] sqlTypes() { @Override
return new int[] { Types.JAVA_OBJECT }; public int getSqlType() {
return Types.JAVA_OBJECT;
} }
public Class returnedClass() { public Class<Object> returnedClass() {
return Object.class; return Object.class;
} }

View File

@ -10,6 +10,7 @@ import java.io.Serializable;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID; import java.util.UUID;
import org.hibernate.HibernateException; import org.hibernate.HibernateException;
@ -117,15 +118,15 @@ public class BasicTypeRegistryTest extends BaseUnitTestCase {
} }
} }
public static class TotallyIrrelevantUserType implements UserType { public static class TotallyIrrelevantUserType implements UserType<Object> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[0]; return Types.VARCHAR;
} }
@Override @Override
public Class returnedClass() { public Class<Object> returnedClass() {
return null; return null;
} }

View File

@ -37,8 +37,8 @@ public class ArrayType implements UserType<Array>, BindableType<Array> {
} }
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { jdbcType.getJdbcTypeCode() }; return jdbcType.getJdbcTypeCode();
} }
@Override @Override
@ -47,13 +47,13 @@ public class ArrayType implements UserType<Array>, BindableType<Array> {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(Array x, Array y) throws HibernateException {
return javaType.areEqual( (Array) x, (Array) y ); return javaType.areEqual( x, y );
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(Array x) throws HibernateException {
return javaType.extractHashCode( (Array) x ); return javaType.extractHashCode( x );
} }
@Override @Override
@ -68,8 +68,8 @@ public class ArrayType implements UserType<Array>, BindableType<Array> {
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public Array deepCopy(Array value) throws HibernateException {
return ArrayMutabilityPlan.INSTANCE.deepCopy( (Array) value ); return ArrayMutabilityPlan.INSTANCE.deepCopy( value );
} }
@Override @Override
@ -78,17 +78,17 @@ public class ArrayType implements UserType<Array>, BindableType<Array> {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Array value) throws HibernateException {
return ArrayMutabilityPlan.INSTANCE.disassemble( (Array) value, null ); return ArrayMutabilityPlan.INSTANCE.disassemble( value, null );
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public Array assemble(Serializable cached, Object owner) throws HibernateException {
return ArrayMutabilityPlan.INSTANCE.assemble( cached, null ); return ArrayMutabilityPlan.INSTANCE.assemble( cached, null );
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public Array replace(Array original, Array target, Object owner) throws HibernateException {
return null; return null;
} }
} }

View File

@ -29,8 +29,8 @@ public class StringWrapperUserType implements UserType<StringWrapper> {
private static final Logger log = Logger.getLogger( StringWrapperUserType.class ); private static final Logger log = Logger.getLogger( StringWrapperUserType.class );
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { Types.VARCHAR }; return Types.VARCHAR;
} }
@Override @Override
@ -39,13 +39,13 @@ public class StringWrapperUserType implements UserType<StringWrapper> {
} }
@Override @Override
public boolean equals(Object x, Object y) public boolean equals(StringWrapper x, StringWrapper y)
throws HibernateException { throws HibernateException {
return Objects.equals( x, y ); return Objects.equals( x, y );
} }
@Override @Override
public int hashCode(Object x) public int hashCode(StringWrapper x)
throws HibernateException { throws HibernateException {
return Objects.hashCode( x ); return Objects.hashCode( x );
} }
@ -66,7 +66,7 @@ public class StringWrapperUserType implements UserType<StringWrapper> {
st.setNull( index, Types.VARCHAR ); st.setNull( index, Types.VARCHAR );
} }
else { else {
String stringValue = toString( (StringWrapper) value ); String stringValue = toString( value );
log.debugv("Binding {0} to parameter {1} ", stringValue, index); log.debugv("Binding {0} to parameter {1} ", stringValue, index);
st.setString( index, stringValue ); st.setString( index, stringValue );
} }
@ -83,7 +83,7 @@ public class StringWrapperUserType implements UserType<StringWrapper> {
} }
@Override @Override
public Object deepCopy(Object value) public StringWrapper deepCopy(StringWrapper value)
throws HibernateException { throws HibernateException {
return value; return value;
} }
@ -94,19 +94,19 @@ public class StringWrapperUserType implements UserType<StringWrapper> {
} }
@Override @Override
public Serializable disassemble(Object value) public Serializable disassemble(StringWrapper value)
throws HibernateException { throws HibernateException {
return (StringWrapper) deepCopy( value ); return deepCopy( value );
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) public StringWrapper assemble(Serializable cached, Object owner)
throws HibernateException { throws HibernateException {
return deepCopy( cached ); return deepCopy( (StringWrapper) cached );
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) public StringWrapper replace(StringWrapper original, StringWrapper target, Object owner)
throws HibernateException { throws HibernateException {
return deepCopy( original ); return deepCopy( original );
} }

View File

@ -24,20 +24,21 @@ import org.jboss.logging.Logger;
/** /**
* @author Michi * @author Michi
*/ */
public class DefaultValueIntegerType implements UserType, ParameterizedType, Serializable { public class DefaultValueIntegerType implements UserType<Integer>, ParameterizedType, Serializable {
private static final Logger log = Logger.getLogger( DefaultValueIntegerType.class ); private static final Logger log = Logger.getLogger( DefaultValueIntegerType.class );
private Integer defaultValue; private Integer defaultValue;
public int[] sqlTypes() { @Override
return new int[] {Types.INTEGER}; public int getSqlType() {
return Types.INTEGER;
} }
public Class returnedClass() { public Class returnedClass() {
return int.class; return int.class;
} }
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(Integer x, Integer y) throws HibernateException {
if ( x == y ) { if ( x == y ) {
return true; return true;
} }
@ -48,13 +49,13 @@ public class DefaultValueIntegerType implements UserType, ParameterizedType, Ser
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public Integer nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
Number result = (Number) rs.getObject( position ); Number result = (Number) rs.getObject( position );
return result == null ? defaultValue : Integer.valueOf( result.intValue() ); return result == null ? defaultValue : Integer.valueOf( result.intValue() );
} }
@Override @Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) public void nullSafeSet(PreparedStatement st, Integer value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException { throws HibernateException, SQLException {
if ( value == null || defaultValue.equals( value ) ) { if ( value == null || defaultValue.equals( value ) ) {
log.trace( "binding null to parameter: " + index ); log.trace( "binding null to parameter: " + index );
@ -62,32 +63,32 @@ public class DefaultValueIntegerType implements UserType, ParameterizedType, Ser
} }
else { else {
log.trace( "binding " + value + " to parameter: " + index ); log.trace( "binding " + value + " to parameter: " + index );
st.setInt( index, ( (Integer) value ).intValue() ); st.setInt( index, value );
} }
} }
public Object deepCopy(Object value) throws HibernateException { public Integer deepCopy(Integer value) throws HibernateException {
return new Integer( ( (Integer) value ).intValue() ); return value;
} }
public boolean isMutable() { public boolean isMutable() {
return false; return false;
} }
public int hashCode(Object x) throws HibernateException { public int hashCode(Integer x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
public Object assemble(Serializable cached, Object owner) public Integer assemble(Serializable cached, Object owner)
throws HibernateException { throws HibernateException {
return cached; return (Integer) cached;
} }
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Integer value) throws HibernateException {
return (Serializable) value; return value;
} }
public Object replace(Object original, Object target, Object owner) public Integer replace(Integer original, Integer target, Object owner)
throws HibernateException { throws HibernateException {
return original; return original;
} }

View File

@ -23,23 +23,21 @@ import org.hibernate.usertype.UserType;
* *
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
*/ */
public class RevisionTypeType implements UserType, Serializable { public class RevisionTypeType implements UserType<RevisionType>, Serializable {
private static final long serialVersionUID = -1053201518229282688L; private static final long serialVersionUID = -1053201518229282688L;
private static final int[] SQL_TYPES = {Types.TINYINT};
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return SQL_TYPES; return Types.TINYINT;
} }
@Override @Override
public Class returnedClass() { public Class<RevisionType> returnedClass() {
return RevisionType.class; return RevisionType.class;
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public RevisionType nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
byte byteValue = rs.getByte( position ); byte byteValue = rs.getByte( position );
if ( rs.wasNull() ) { if ( rs.wasNull() ) {
return null; return null;
@ -48,7 +46,7 @@ public class RevisionTypeType implements UserType, Serializable {
} }
@Override @Override
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) public void nullSafeSet(PreparedStatement preparedStatement, RevisionType value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException { throws HibernateException, SQLException {
if ( value == null ) { if ( value == null ) {
preparedStatement.setNull( index, Types.TINYINT ); preparedStatement.setNull( index, Types.TINYINT );
@ -59,7 +57,7 @@ public class RevisionTypeType implements UserType, Serializable {
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public RevisionType deepCopy(RevisionType value) throws HibernateException {
return value; return value;
} }
@ -69,27 +67,27 @@ public class RevisionTypeType implements UserType, Serializable {
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public RevisionType assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (RevisionType) cached;
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(RevisionType value) throws HibernateException {
return (Serializable) value; return value;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public RevisionType replace(RevisionType original, RevisionType target, Object owner) throws HibernateException {
return original; return original;
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(RevisionType x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(RevisionType x, RevisionType y) throws HibernateException {
return Objects.equals( x, y ); return Objects.equals( x, y );
} }
} }

View File

@ -24,7 +24,6 @@ import org.hibernate.usertype.UserType;
* @author Adam Warski (adam at warski dot org) * @author Adam Warski (adam at warski dot org)
*/ */
public class ParametrizedTestUserType implements UserType<String>, ParameterizedType { public class ParametrizedTestUserType implements UserType<String>, ParameterizedType {
private static final int[] TYPES = new int[] {Types.VARCHAR};
private String param1; private String param1;
private String param2; private String param2;
@ -58,23 +57,24 @@ public class ParametrizedTestUserType implements UserType<String>, Parameterized
.bind( st, value, index, session ); .bind( st, value, index, session );
} }
public int[] sqlTypes() { @Override
return TYPES; public int getSqlType() {
return Types.VARCHAR;
} }
public Object assemble(Serializable cached, Object owner) throws HibernateException { public String assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (String) cached;
} }
public Object deepCopy(Object value) throws HibernateException { public String deepCopy(String value) throws HibernateException {
return value; return value;
} }
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(String value) throws HibernateException {
return (Serializable) value; return value;
} }
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(String x, String y) throws HibernateException {
//noinspection ObjectEquality //noinspection ObjectEquality
if ( x == y ) { if ( x == y ) {
return true; return true;
@ -87,7 +87,7 @@ public class ParametrizedTestUserType implements UserType<String>, Parameterized
return x.equals( y ); return x.equals( y );
} }
public int hashCode(Object x) throws HibernateException { public int hashCode(String x) throws HibernateException {
return x.hashCode(); return x.hashCode();
} }
@ -95,7 +95,7 @@ public class ParametrizedTestUserType implements UserType<String>, Parameterized
return false; return false;
} }
public Object replace(Object original, Object target, Object owner) throws HibernateException { public String replace(String original, String target, Object owner) throws HibernateException {
return original; return original;
} }
} }

View File

@ -20,17 +20,17 @@ import org.hibernate.usertype.UserType;
* @author Slawek Garwol (slawekgarwol at gmail dot com) * @author Slawek Garwol (slawekgarwol at gmail dot com)
*/ */
public class CustomEnumUserType implements UserType<CustomEnum> { public class CustomEnumUserType implements UserType<CustomEnum> {
private static final int[] SQL_TYPES = {Types.VARCHAR};
public int[] sqlTypes() { @Override
return SQL_TYPES; public int getSqlType() {
return Types.VARCHAR;
} }
public Class returnedClass() { public Class<CustomEnum> returnedClass() {
return CustomEnum.class; return CustomEnum.class;
} }
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(CustomEnum x, CustomEnum y) throws HibernateException {
if ( x == y ) { if ( x == y ) {
return true; return true;
} }
@ -40,7 +40,7 @@ public class CustomEnumUserType implements UserType<CustomEnum> {
return x.equals( y ); return x.equals( y );
} }
public int hashCode(Object x) throws HibernateException { public int hashCode(CustomEnum x) throws HibernateException {
return (x == null) ? 0 : x.hashCode(); return (x == null) ? 0 : x.hashCode();
} }
@ -63,7 +63,7 @@ public class CustomEnumUserType implements UserType<CustomEnum> {
} }
} }
public Object deepCopy(Object value) throws HibernateException { public CustomEnum deepCopy(CustomEnum value) throws HibernateException {
return value; return value;
} }
@ -71,15 +71,15 @@ public class CustomEnumUserType implements UserType<CustomEnum> {
return false; return false;
} }
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(CustomEnum value) throws HibernateException {
return (Serializable) value; return value;
} }
public Object assemble(Serializable cached, Object owner) throws HibernateException { public CustomEnum assemble(Serializable cached, Object owner) throws HibernateException {
return cached; return (CustomEnum) cached;
} }
public Object replace(Object original, Object target, Object owner) throws HibernateException { public CustomEnum replace(CustomEnum original, CustomEnum target, Object owner) throws HibernateException {
return original; return original;
} }
} }

View File

@ -16,13 +16,11 @@ import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor; import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType; import org.hibernate.usertype.UserType;
public class AgeType implements UserType { public class AgeType implements UserType<Age> {
@Override @Override
public int[] sqlTypes() { public int getSqlType() {
return new int[] { return Types.INTEGER;
Types.INTEGER
};
} }
@Override @Override
@ -31,29 +29,29 @@ public class AgeType implements UserType {
} }
@Override @Override
public boolean equals(Object x, Object y) throws HibernateException { public boolean equals(Age x, Age y) throws HibernateException {
return x != null ? x.equals( y ) : y == null; return x != null ? x.equals( y ) : y == null;
} }
@Override @Override
public int hashCode(Object x) throws HibernateException { public int hashCode(Age x) throws HibernateException {
return x != null ? x.hashCode() : 1; return x != null ? x.hashCode() : 1;
} }
@Override @Override
public Object nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { public Age nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException {
return new Age( rs.getInt( position ) ); return new Age( rs.getInt( position ) );
} }
@Override @Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) public void nullSafeSet(PreparedStatement st, Age value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException { throws HibernateException, SQLException {
st.setInt( index, ( (Age) value ).getAgeInYears() ); st.setInt( index, value.getAgeInYears() );
} }
@Override @Override
public Object deepCopy(Object value) throws HibernateException { public Age deepCopy(Age value) throws HibernateException {
return new Age( ( (Age) value ).getAgeInYears() ); return new Age( value.getAgeInYears() );
} }
@Override @Override
@ -62,17 +60,17 @@ public class AgeType implements UserType {
} }
@Override @Override
public Serializable disassemble(Object value) throws HibernateException { public Serializable disassemble(Age value) throws HibernateException {
return null; return null;
} }
@Override @Override
public Object assemble(Serializable cached, Object owner) throws HibernateException { public Age assemble(Serializable cached, Object owner) throws HibernateException {
return null; return null;
} }
@Override @Override
public Object replace(Object original, Object target, Object owner) throws HibernateException { public Age replace(Age original, Age target, Object owner) throws HibernateException {
return null; return null;
} }
} }

View File

@ -16,39 +16,40 @@ import org.hibernate.type.descriptor.java.MutableMutabilityPlan;
/** /**
* @author Vlad Mihalcea * @author Vlad Mihalcea
*/ */
public class CommaDelimitedStringMapJavaType extends AbstractClassJavaType<Map> { public class CommaDelimitedStringMapJavaType extends AbstractClassJavaType<Map<String, String>> {
public static final String DELIMITER = ","; public static final String DELIMITER = ",";
public CommaDelimitedStringMapJavaType() { public CommaDelimitedStringMapJavaType() {
//noinspection unchecked
super( super(
Map.class, (Class<? extends Map<String, String>>) (Class<?>) Map.class,
new MutableMutabilityPlan<Map>() { new MutableMutabilityPlan<>() {
@Override @Override
protected Map deepCopyNotNull(Map value) { protected Map<String, String> deepCopyNotNull(Map<String, String> value) {
return new HashMap( value ); return new HashMap<>( value );
} }
} }
); );
} }
@Override @Override
public String toString(Map value) { public String toString(Map<String, String> value) {
return null; return null;
} }
@Override @Override
public Map fromString(CharSequence string) { public Map<String, String> fromString(CharSequence string) {
return null; return null;
} }
@Override @Override
public <X> X unwrap(Map value, Class<X> type, WrapperOptions options) { public <X> X unwrap(Map<String, String> value, Class<X> type, WrapperOptions options) {
return (X) toString( value ); return (X) toString( value );
} }
@Override @Override
public <X> Map wrap(X value, WrapperOptions options) { public <X> Map<String, String> wrap(X value, WrapperOptions options) {
return fromString( (String) value ); return fromString( (String) value );
} }
} }