Piotr Przybylski (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiYzI4NmE2ZWU1...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16751?atlOrigin=eyJpIjoiYzI4Nm...
) HHH-16751 (
https://hibernate.atlassian.net/browse/HHH-16751?atlOrigin=eyJpIjoiYzI4Nm...
) Unable to use SQL functions on UserType attribute (
https://hibernate.atlassian.net/browse/HHH-16751?atlOrigin=eyJpIjoiYzI4Nm...
)
Change By: Piotr Przybylski (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
Similar to
[
https://hibernate.atlassian.net/browse/HHH-16241|https://hibernate.atlass...]
, 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/...],
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.atlass...]).
(
https://hibernate.atlassian.net/browse/HHH-16751#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-16751#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:4a1ccf9 )