在 Hibernate 中,您可以通过创建自己的 org.hibernate.usertype.UserType
.
public class SpecialStringUserType implements UserType {
private static final int[] SQL_TYPES = { Types.VARCHAR };
public int[] sqlTypes() {
return SQL_TYPES;
}
public Class returnedClass() {
return String.class;
}
public boolean equals(Object x, Object y)
throws HibernateException {
if (x == y) {
return true;
} else if (x == null || y == null) {
return false;
} else {
return x.equals(y);
}
}
public Object nullSafeGet(ResultSet resultSet,
String[] names, Object owner)
throws HibernateException, SQLException {
String result = resultSet.getString(names[0]);
if ("SPECIAL_UNICODE_NULL".equals( result )) {
return null;
}
return result;
}
public void nullSafeSet(PreparedStatement statement,
Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
statement.setString(index, "SPECIAL_UNICODE_NULL");
} else {
statement.setString(index, value);
}
}
public Object deepCopy(Object value) throws HibernateException {
return value;
}
public boolean isMutable() {
return false;
}
}
不要使用您自己的@LegacyString
注释,而是执行以下操作:
@Column(name="A_LEGACY_COLUMN")
@Type( type = "SpecialStringUserType" )
public String oneOfThenLegacyColumn;