@Convert annotation on attribute of @Embeddable does not work

Description

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)`.

Environment

Wildfly 10

Activity

Show:
Thorsten Möller
June 15, 2016, 9:04 PM
Edited

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.

Thorsten Möller
June 22, 2016, 12:09 PM

Anyone?

Simon Guertin
July 29, 2016, 7:22 PM

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

Stefan Ocke
June 8, 2020, 3:19 PM
Edited

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

Thorsten Möller
August 2, 2020, 8:59 AM

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

Assignee

Unassigned

Reporter

Thorsten Möller

Fix versions

None

Labels

None

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

Priority

Major
Configure