Piotr Przybylski (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiM2NmNDQwMjNh...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-16751?atlOrigin=eyJpIjoiM2NmND...
) HHH-16751 (
https://hibernate.atlassian.net/browse/HHH-16751?atlOrigin=eyJpIjoiM2NmND...
) Unable to use SQL functions on UserType attribute (
https://hibernate.atlassian.net/browse/HHH-16751?atlOrigin=eyJpIjoiM2NmND...
)
Issue Type: Bug Affects Versions: 6.2.4 Assignee: Unassigned Attachments:
YearMonthUserTypeTest.java Components: query-hql Created: 05/Jun/2023 09:42 AM Priority:
Major Reporter: Piotr Przybylski (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%...
)
Similar to
https://hibernate.atlassian.net/browse/HHH-16241 , but concerns querying when
type conversion is done using UserType.
Given an attribute:
@Type(YearMonthUserType.class)
private YearMonth yearMonth;
and UserType :
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;
}
}
it’s impossible to eg. SELECT max(yearMonth) , because query creation fails with:
java.lang.IllegalArgumentException: org.hibernate.QueryException:
Parameter 1 of function max() has type COMPARABLE, but argument is of type
java.time.YearMonth
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.
(
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 )