We have a legacy database whereby integers are stored as varchar padded with zeros, i.e. the value 1
is stored as 001
depending on the column length defined in the schema. The idea is to have each of the entities represent such information with the type Integer
. To achieve this one would normally extend AttributeConverter
, however, this only provides that attribute value to be converted. How can one gain access to the @Column
annotation to retrieve the value for the length?
You can’t. The AttributeConverter
doesn’t know anything about the use-site. You could create a converter for each length though, or you go the more complicated route and implement a custom UserType
and also extend the org.hibernate.usertype.DynamicParameterizedType
interface.
I did try creating a custom UserType
however the conversion is only applied when reading from or writing to the database. For my use case, I would like that this is also applied to the values when used for filtering via a CriteriaBuilder
. I do not know if this is a bug or if the UserType
is intended to work in such a way.
If you share an example of what you tried but doesn’t work then I can help you. UserType
conversion is applied for parameters which are compared against fields of that type though.