Say I have an entity that contains an element collection of an embeddable type. The embeddable has an attribute that is an enumeration. I want to represent/encode values of the enumeration in the database as a single character, which is why I wrote a converter to do this. Modulo renaming of classes and attributes, the code looks like this:
Using Postgres, when Hibernate creates the database schema, it will map the embeddable's field to type `integer` rather than `character(1)`.
I have also tried the following, without any difference (still `integer` used in the database):
My first question is whether the JPA spec covers this use case. I could not find any explicit statement that this would not be allowed. In fact, the JPA Wikibook makes the following statement:
Another option, if converting a custom type, is to have the Converter applied to any mapped attribute that has that type.
My interpretation of this statement is that the example described above is a very valid use case. Then, however, the behavior I see seems to be a bug. What do you think?
As a last comment, I have other enumeration attributes in entities (rather than embeddables) where conversion to a single character works well and where the type in the database is indeed `character(1)`.
As a workaround, I got it working by implementing a custom type and placing the `@Type(...)` annotation on the enumeration attribute as described here.
The downside is that the workaround requires imports of Hibernate classes in the code, but I would rather want to use JPA classes only.
I have almost the same issue here, but with a simple String field with a simple <String,String> Converter. My converter does not get called when in a @embedable class
I tested with Hibernate 5.4.15:
For “simple” Embeddables, @Convert annotation works.
For Embeddables in ElementCollections, @Convert annotation also works now. So, the original issue seems to be fixed.
However, in the following situation, @Convert is still ignored:
I have an Element Collection of an Embeddable OuterEmbeddable.
“OuterEmbeddable“ contains another Embeddable called InnerEmeddable
Within InnerEmeddable I use @Convert on basic fields. But it is ignored.
My current workaround is to use @Convert one level higher:
→ new Issue
As mentioned by this comment, I can also confirm that it works for me now (which allowed us to get rid of some Hibernate imports and work with JPA `@Convert` annotations only ). I’m using Hibernate 5.3.17