Similar to [https://hibernate.atlassian.net/browse/HHH-16241|https://hibernate.atlassian.net/browse/HHH-16241|smart-link] , but concerns querying when type conversion is done using UserType.
Given an attribute:
{code:java}@Type(YearMonthUserType.class) private YearMonth yearMonth;{code}
and {{UserType}}:
{noformat} public static class YearMonthUserType implements UserType<YearMonth> { @Override public int getSqlType() { return SqlTypes.INTEGER; }
@Override public Class<YearMonth> returnedClass() { return YearMonth.class; }
@Override public boolean equals(YearMonth x, YearMonth y) { return Objects.equals(x, y); }
@Override public int hashCode(YearMonth x) { return Objects.hashCode(x); }
@Override public YearMonth nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { int intValue = rs.getInt( position ); if ( rs.wasNull() ) { return null; } return YearMonth.of( intValue / 100, intValue % 100 ); }
@Override public void nullSafeSet(PreparedStatement st, YearMonth value, int index, SharedSessionContractImplementor session) throws SQLException { if ( value == null ) { st.setNull( index, Types.INTEGER ); } else { st.setInt( index, ( value.getYear() * 100 ) + value.getMonth().getValue() ); } }
@Override public YearMonth deepCopy(YearMonth value) { return value; }
@Override public boolean isMutable() { return false; }
@Override public Serializable disassemble(YearMonth value) { return value; }
@Override public YearMonth assemble(Serializable cached, Object owner) { return cached instanceof YearMonth ? (YearMonth) cached : null; } }{noformat}
it’s impossible to eg. {{SELECT max(yearMonth)}}, because query creation fails with:
{quote}java.lang.IllegalArgumentException: org.hibernate.QueryException: Parameter 1 of function max() has type COMPARABLE, but argument is of type java.time.YearMonth{quote}
Attached test case was made as a copy of [https://github.com/hibernate/hibernate-orm/blob/6.2.4/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/YearMonthConverterTest.java|https://github.com/hibernate/hibernate-orm/blob/6.2.4/hibernate-core/src/test/java/org/hibernate/orm/test/mapping/converted/converter/YearMonthConverterTest.java|smart-link], but uses {{@Type}} instead of {{AttributeConverter}}.
{{@JavaType}} also doesn’t work (test case in JavaType.zip, copied from [https://hibernate.atlassian.net/browse/HHH-16671|https://hibernate.atlassian.net/browse/HHH-16671|smart-link]). |
|