The model has {{java.time.YearMonth}} attributes being mapped to integers on the DB (MariaDB)
Here is how it was used:
{code:java}@Convert(converter = YearMonthConverter.class) @Column(name = "month", nullable = false) private YearMonth month;{code}
And here is the converter class (based on Jakarta now):
{code:java}public class YearMonthConverter implements AttributeConverter<YearMonth, Integer> { @Override public Integer convertToDatabaseColumn(YearMonth attribute) { return attribute == null ? null : (attribute.getYear() * 100) + attribute.getMonth().getValue(); }
@Override public YearMonth convertToEntityAttribute(Integer dbData) { return dbData == null ? null : YearMonth.of(dbData / 100, dbData % 100); } }{code}
I’ve got Adding a MAX demo project to the ticket with 2 errors:
* Persisting the entity gives {{PersistenceException}}: ** message: {{ ( conn=9 ) query on this column which fails You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'year_month, id) values (202212, 1)' at line 1}} * Searching the entity with {{=}} gives a {{PersistenceException}}: ** query: {{SELECT de FROM DemoEntity de WHERE de.yearMonth = :yearMonth}} ** message: {{Converting org.hibernate.exception.SQLGrammarException to JPA PersistenceException : JDBC exception executing SQL [select d1_0.id,d1_0.year_month from demo_table d1_0 where d1_0.year_month=?]}} * Searching the following error entity with {{MAX}} gives an {{IllegalArgumentException}} : ** query: {{ SELECT MAX(de.yearMonth) FROM DemoEntity de}} ** message: {{ org.hibernate.QueryException: Parameter 1 of function max() has type COMPARABLE, but argument is of type java.time.YearMonth}}
It’s worth noticing that the ORM is able to read/write this column from the DB, it only fails to run functions like MIN or MAX.
The debugging took me to a section of the code in {{ArgumentTypesValidator}} where the code considers that the node type {{YearMonth}} is incompatible with the {{FunctionParameterType}} {{COMPARABLE}}.
Let me know if there is anything else that I can provide. |
|