ElementCollection with Converter and mapped entity


#1

Hi,

I have an entity with @ElementCollection on List<MyValue> with an converter for the type MyValue, but I also have another entity with @ElementCollection on List<MyValue> and without a converter specified.

If MyValue is not mapped as a entity it respect the converter, but if the MyValue is an entity it’s ignored. This is tested with Hibernate 5.0.12.Final

I was kinda hopping I could mix this, so one entity use the converter and the other map it as an relation.

Is this possible?

@Entity
class MyValue {
 ....
}

class MyFirstEntity {
   @ElementCollection
   List<MyValue> myValueList;
} 

class MySecondEntity {
  @Convert( converter = MyValueTypeConverter.class )
  @ElementCollection
   List<MyValue> myValueList;
} 

Thanks
/Flemming


#2

@ElementCollection works with non-entity types like Basic or Embeddable types. You can’t use an entity with an @ElementCollection.


#3

You’r right it was a mistake from my side to simplify the example, the places where I map the entity and it’s relation it’s actually mapped as @OneToOne with the type MyType.

But you could argue if the @ElementCollection is used with List<Entity Type> with an converter, it should treat it as non-entity and use the converter instead


#4

But you could argue if the @ElementCollection is used with List with a converter, it should treat it as non-entity and use the converter instead

This is what the JPA spec says:

The Convert annotation should not be used to specify conversion of the following: Id attributes
(including the attributes of embedded ids and derived identities), version attributes, relationship
attributes, and attributes explicitly annotated (or designated via XML) as Enumerated or Temporal.
Applications that specify such conversions will not be portable.

The Convert annotation may be applied to a basic attribute or to an element collection of basic type
(in which case the converter is applied to the elements of the collection). In these cases, the
attributeName element must not be specified.

The Convert annotation may be applied to an embedded attribute or to a map collection attribute
whose key or value is of embeddable type (in which case the converter is applied to the specified
attribute of the embeddable instances contained in the collection). In these cases, the attribute-
Name element must be specified.

To override conversion mappings at multiple levels of embedding, a dot (".") notation form must be
used in the attributeName element to indicate an attribute within an embedded attribute. The value
of each identifier used with the dot notation is the name of the respective embedded field or property.

When the Convert annotation is applied to a map containing instances of embeddable classes, the
attributeName element must be specified, and “key.” or “value.” must be used to prefix the
name of the attribute that is to be converted in order to specify it as part of the map key or map value.

When the Convert annotation is applied to a map to specify conversion of a map key of basic type,
“key” must be used as the value of the attributeName element to specify that it is the map key
that is to be converted.

The Convert annotation may be applied to an entity class that extends a mapped superclass to specify
or override a conversion mapping for an inherited basic or embedded attribute.

More, related to @ElementCollection, the JPA spec says:

The ElementCollection annotation defines a collection of instances of a basic type or embeddable
class.

Therefore, you imply a feature that’s never been specified. You can use an Embeddable instead of an entity for the Converter.