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
*/
//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();
private static final Logger log = Logger.getLogger(BitSetUserType.class);
@Override
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
public int getSqlType() {
return Types.VARCHAR;
}
@Override
public Class returnedClass() {
public Class<BitSet> returnedClass() {
return BitSet.class;
}
@Override
public boolean equals(Object x, Object y)
public boolean equals(BitSet x, BitSet y)
throws HibernateException {
return Objects.equals(x, y);
}
@Override
public int hashCode(Object x)
public int hashCode(BitSet x)
throws HibernateException {
return Objects.hashCode(x);
}
@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);
if (rs.wasNull()) {
columnValue = null;
@ -65,7 +65,7 @@ public class BitSetUserType implements UserType<Object> {
@Override
public void nullSafeSet(
PreparedStatement st, Object value, int index, SharedSessionContractImplementor session)
PreparedStatement st, BitSet value, int index, SharedSessionContractImplementor session)
throws HibernateException, SQLException {
if (value == null) {
log.debugv("Binding null to parameter {0} ",index);
@ -79,7 +79,7 @@ public class BitSetUserType implements UserType<Object> {
}
@Override
public Object deepCopy(Object value)
public BitSet deepCopy(BitSet value)
throws HibernateException {
return value == null ? null :
BitSet.valueOf(BitSet.class.cast(value).toLongArray());
@ -91,19 +91,19 @@ public class BitSetUserType implements UserType<Object> {
}
@Override
public Serializable disassemble(Object value)
public Serializable disassemble(BitSet value)
throws HibernateException {
return (BitSet) deepCopy(value);
return deepCopy(value);
}
@Override
public Object assemble(Serializable cached, Object owner)
public BitSet assemble(Serializable cached, Object owner)
throws HibernateException {
return deepCopy(cached);
return deepCopy( (BitSet) cached );
}
@Override
public Object replace(Object original, Object target, Object owner)
public BitSet replace(BitSet original, BitSet target, Object owner)
throws HibernateException {
return deepCopy(original);
}

View File

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

View File

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

View File

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

View File

@ -53,7 +53,7 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
@Override
public int getJdbcTypeCode() {
return userType.sqlTypes()[0];
return userType.getSqlType();
}
@Override
@ -142,10 +142,10 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
}
if ( extracted == null ) {
JdbcExtractingLogging.logNullExtracted( paramIndex, userType.sqlTypes()[0] );
JdbcExtractingLogging.logNullExtracted( paramIndex, userType.getSqlType() );
}
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 ) {
JdbcExtractingLogging.logNullExtracted( paramName, userType.sqlTypes()[0] );
JdbcExtractingLogging.logNullExtracted( paramName, userType.getSqlType() );
}
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 {
if ( JdbcBindingLogging.TRACE_ENABLED ) {
if ( value == null ) {
JdbcBindingLogging.logNullBinding( index, userType.sqlTypes()[ 0 ] );
JdbcBindingLogging.logNullBinding( index, userType.getSqlType() );
}
else {
JdbcBindingLogging.logBinding( index, userType.sqlTypes()[ 0 ], value );
JdbcBindingLogging.logBinding( index, userType.getSqlType(), value );
}
}
userType.nullSafeSet( st, value, index, options.getSession() );

View File

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

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

View File

@ -53,13 +53,13 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
public interface UserType<J> {
/**
* Return the SQL type codes for the columns mapped by this type. The
* codes are generally defined on {@code java.sql.Types}, but could
* Return the SQL type code for the column mapped by this type. The
* codes are generally defined on {@code org.hibernate.type.SqlTypes}, but could
* be database-specific codes
*
* @see java.sql.Types
* @see org.hibernate.type.SqlTypes
*/
int[] sqlTypes();
int getSqlType();
/**
* 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".
* 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"
*/
int hashCode(Object x);
int hashCode(J x);
/**
* 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
* @return Object a copy
*/
Object deepCopy(Object value);
J deepCopy(J value);
/**
* Are objects of this type mutable?
@ -118,7 +118,7 @@ public interface UserType<J> {
* @param value the object to be cached
* @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
@ -128,7 +128,7 @@ public interface UserType<J> {
* @param owner the owner of the cached object
* @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
@ -142,7 +142,7 @@ public interface UserType<J> {
*
* @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) {
return Size.DEFAULT_LENGTH;

View File

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

View File

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

View File

@ -76,14 +76,13 @@ public class EnumeratedSmokeTest extends BaseUnitTestCase {
assertThat( customType.getUserType(), instanceOf( org.hibernate.type.EnumType.class ) );
final org.hibernate.type.EnumType hibernateMappingEnumType = (org.hibernate.type.EnumType) customType.getUserType();
assertThat( hibernateMappingEnumType.isOrdinal(), is(expectedJpaEnumType==EnumType.ORDINAL) );
assertThat( hibernateMappingEnumType.sqlTypes().length, is(1) );
final int expectedJdbcTypeCode = jdbcRegistry.getDescriptor(
expectedJpaEnumType == EnumType.ORDINAL ?
Types.TINYINT :
Types.VARCHAR
).getJdbcTypeCode();
assertThat(
hibernateMappingEnumType.sqlTypes()[0],
hibernateMappingEnumType.getSqlType(),
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> {
@Override
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
public int getSqlType() {
return Types.VARCHAR;
}
@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)
throws HibernateException, SQLException {
if ( value == null ) {
st.setNull( index, sqlTypes()[0] );
st.setNull( index, getSqlType() );
}
else {
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> {
@Override
public int[] sqlTypes() {
return new int[] { Types.VARCHAR };
public int getSqlType() {
return Types.VARCHAR;
}
@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)
throws HibernateException, SQLException {
if ( value == null ) {
st.setNull( index, sqlTypes()[0] );
st.setNull( index, getSqlType() );
}
else {
String enumString = value.name();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -19,15 +19,15 @@ import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.ParameterizedType;
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() ) {
throw new IllegalArgumentException( "Parameter has to be an enum-class" );
}
EnumUserType that = new EnumUserType();
EnumUserType<T> that = new EnumUserType<>();
Properties p = new Properties();
p.setProperty( "enumClassName", clazz.getName() );
that.setParameterValues( p );
@ -41,7 +41,8 @@ public class EnumUserType implements UserType, ParameterizedType {
}
try {
this.clazz = Class.forName( enumClassName );
//noinspection unchecked
this.clazz = (Class<T>) Class.forName( enumClassName );
}
catch (ClassNotFoundException 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};
public int[] sqlTypes() {
return SQL_TYPES;
@Override
public int getSqlType() {
return Types.CHAR;
}
public Class returnedClass() {
public Class<T> returnedClass() {
return clazz;
}
@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 );
if ( rs.wasNull() ) {
return null;
@ -72,24 +72,23 @@ public class EnumUserType implements UserType, ParameterizedType {
public void nullSafeSet(PreparedStatement preparedStatement, Object value, int index)
throws HibernateException, SQLException {
if ( null == value ) {
preparedStatement.setNull( index, Types.VARCHAR );
}
else {
preparedStatement.setString( index, ( (Enum) value ).name() );
}
}
@Override
public void nullSafeSet(
PreparedStatement preparedStatement,
Object value,
T value,
int index,
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;
}
@ -97,23 +96,24 @@ public class EnumUserType implements UserType, ParameterizedType {
return false;
}
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
public T assemble(Serializable cached, Object owner) throws HibernateException {
//noinspection unchecked
return (T) cached;
}
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
public Serializable disassemble(T value) throws HibernateException {
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;
}
public int hashCode(Object x) throws HibernateException {
public int hashCode(T x) throws HibernateException {
return x.hashCode();
}
public boolean equals(Object x, Object y) throws HibernateException {
public boolean equals(T x, T y) throws HibernateException {
if ( x == y ) {
return true;
}

View File

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

View File

@ -10,6 +10,7 @@ import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.UUID;
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
public int[] sqlTypes() {
return new int[0];
public int getSqlType() {
return Types.VARCHAR;
}
@Override
public Class returnedClass() {
public Class<Object> returnedClass() {
return null;
}

View File

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

View File

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

View File

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

View File

@ -23,23 +23,21 @@ import org.hibernate.usertype.UserType;
*
* @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 int[] SQL_TYPES = {Types.TINYINT};
@Override
public int[] sqlTypes() {
return SQL_TYPES;
public int getSqlType() {
return Types.TINYINT;
}
@Override
public Class returnedClass() {
public Class<RevisionType> returnedClass() {
return RevisionType.class;
}
@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 );
if ( rs.wasNull() ) {
return null;
@ -48,7 +46,7 @@ public class RevisionTypeType implements UserType, Serializable {
}
@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 {
if ( value == null ) {
preparedStatement.setNull( index, Types.TINYINT );
@ -59,7 +57,7 @@ public class RevisionTypeType implements UserType, Serializable {
}
@Override
public Object deepCopy(Object value) throws HibernateException {
public RevisionType deepCopy(RevisionType value) throws HibernateException {
return value;
}
@ -69,27 +67,27 @@ public class RevisionTypeType implements UserType, Serializable {
}
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return cached;
public RevisionType assemble(Serializable cached, Object owner) throws HibernateException {
return (RevisionType) cached;
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
return (Serializable) value;
public Serializable disassemble(RevisionType value) throws HibernateException {
return value;
}
@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;
}
@Override
public int hashCode(Object x) throws HibernateException {
public int hashCode(RevisionType x) throws HibernateException {
return x.hashCode();
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
public boolean equals(RevisionType x, RevisionType y) throws HibernateException {
return Objects.equals( x, y );
}
}

View File

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

View File

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

View File

@ -16,13 +16,11 @@ import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
public class AgeType implements UserType {
public class AgeType implements UserType<Age> {
@Override
public int[] sqlTypes() {
return new int[] {
Types.INTEGER
};
public int getSqlType() {
return Types.INTEGER;
}
@Override
@ -31,29 +29,29 @@ public class AgeType implements UserType {
}
@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;
}
@Override
public int hashCode(Object x) throws HibernateException {
public int hashCode(Age x) throws HibernateException {
return x != null ? x.hashCode() : 1;
}
@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 ) );
}
@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 {
st.setInt( index, ( (Age) value ).getAgeInYears() );
st.setInt( index, value.getAgeInYears() );
}
@Override
public Object deepCopy(Object value) throws HibernateException {
return new Age( ( (Age) value ).getAgeInYears() );
public Age deepCopy(Age value) throws HibernateException {
return new Age( value.getAgeInYears() );
}
@Override
@ -62,17 +60,17 @@ public class AgeType implements UserType {
}
@Override
public Serializable disassemble(Object value) throws HibernateException {
public Serializable disassemble(Age value) throws HibernateException {
return null;
}
@Override
public Object assemble(Serializable cached, Object owner) throws HibernateException {
public Age assemble(Serializable cached, Object owner) throws HibernateException {
return null;
}
@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;
}
}

View File

@ -16,39 +16,40 @@ import org.hibernate.type.descriptor.java.MutableMutabilityPlan;
/**
* @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() {
super(
Map.class,
new MutableMutabilityPlan<Map>() {
@Override
protected Map deepCopyNotNull(Map value) {
return new HashMap( value );
}
}
);
}
public CommaDelimitedStringMapJavaType() {
//noinspection unchecked
super(
(Class<? extends Map<String, String>>) (Class<?>) Map.class,
new MutableMutabilityPlan<>() {
@Override
protected Map<String, String> deepCopyNotNull(Map<String, String> value) {
return new HashMap<>( value );
}
}
);
}
@Override
public String toString(Map value) {
return null;
}
@Override
public String toString(Map<String, String> value) {
return null;
}
@Override
public Map fromString(CharSequence string) {
return null;
}
@Override
public Map<String, String> fromString(CharSequence string) {
return null;
}
@Override
public <X> X unwrap(Map value, Class<X> type, WrapperOptions options) {
return (X) toString( value );
}
@Override
public <X> X unwrap(Map<String, String> value, Class<X> type, WrapperOptions options) {
return (X) toString( value );
}
@Override
public <X> Map wrap(X value, WrapperOptions options) {
return fromString( (String) value );
}
@Override
public <X> Map<String, String> wrap(X value, WrapperOptions options) {
return fromString( (String) value );
}
}