Hansi B (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63736b9...
) *updated* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNzg0YWExOGMy...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-15733?atlOrigin=eyJpIjoiNzg0YW...
) HHH-15733 (
https://hibernate.atlassian.net/browse/HHH-15733?atlOrigin=eyJpIjoiNzg0YW...
) @Convert on Map should not require "attributeName=value" (
https://hibernate.atlassian.net/browse/HHH-15733?atlOrigin=eyJpIjoiNzg0YW...
)
Change By: Hansi B (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63736b9...
)
This entity definition results in a runtime exception:
{code:java}@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int id;
@ElementCollection
@Convert(converter = MyStringConverter.class)
private Map<String, String> responsibilities = new HashMap<>();
void put(String key, String value) {
responsibilities.put(key, value);
}
String get(String key) {
return responsibilities.get(key);
}
}{code}
The exception points to the *Convert* annotation on the *Map*:
{noformat}Caused by: java.lang.IllegalStateException: @Convert placed on Map attribute
[org.hibernate.bugs.Employee.responsibilities] must define attributeName of 'key'
or 'value'
at
org.hibernate.cfg.CollectionPropertyHolder.applyLocalConvert(CollectionPropertyHolder.java:133)
at
org.hibernate.cfg.CollectionPropertyHolder.buildAttributeConversionInfoMaps(CollectionPropertyHolder.java:88)
at
org.hibernate.cfg.CollectionPropertyHolder.prepare(CollectionPropertyHolder.java:379){noformat}
Indeed, the code in *CollectionPropertyHolder* explicitly enforces this, and a comment
states:
{noformat}// ... JPA says
// that @Convert on a Map always needs to specify attributeName of key/value (or prefixed
with
// key./value. for embedded paths).{noformat}
However, this does not appear to be the case: While the standard lists a few cases which
require an *attributeName* on a *Map*, it does not require *attributeName* on a *Map* of
basic types; without *attributeName*, the conversion should be applied on the values in
the *Map*. See, e.g., "Example 5" on
[
https://docs.jboss.org/hibernate/jpa/2.2/api/javax/persistence/Convert.ht...]
{noformat}Example 5: Apply a converter to an element collection that is a map or basic
values.
The converter is applied to the map value.
@ElementCollection
@Convert(converter=EmployeeNameConverter.class)
Map<String, String> responsibilities;{noformat}
(This is identical to Example 5 in the Java TM Persistence API, Version 2.2, Chapter
11.1.10.)
One could even go a step further and say that *attributeName="value"* should be
forbidden on *Map*s with basic type values, since the standard specifies:
{noformat}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.{noformat}
(Indeed, Eclipselink will raise an exception when *attributeName="value"* is
specified on a *Map* with values of a basic type.)
I have seen this behaviour when using the Entity in a minimal test (just create an
Employee) both in the *hibernate-test-case-templates/orm/hibernate-orm-5* and
*hibernate-test-case-templates/orm/hibernate-orm-6*
The problem does not occur without the *@ElementCollection*.
(
https://hibernate.atlassian.net/browse/HHH-15733#add-comment?atlOrigin=ey...
) Add Comment (
https://hibernate.atlassian.net/browse/HHH-15733#add-comment?atlOrigin=ey...
)
Get Jira notifications on your phone! Download the Jira Cloud app for Android (
https://play.google.com/store/apps/details?id=com.atlassian.android.jira....
) or iOS (
https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=Em...
) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100210- sha1:9b34d7c )