Hansi B (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63736b9...
) *created* an issue
Hibernate ORM (
https://hibernate.atlassian.net/browse/HHH?atlOrigin=eyJpIjoiNjA4Y2U2NDBj...
) / Bug (
https://hibernate.atlassian.net/browse/HHH-15733?atlOrigin=eyJpIjoiNjA4Y2...
) HHH-15733 (
https://hibernate.atlassian.net/browse/HHH-15733?atlOrigin=eyJpIjoiNjA4Y2...
) @Convert on Map should not require "attributeName=value" (
https://hibernate.atlassian.net/browse/HHH-15733?atlOrigin=eyJpIjoiNjA4Y2...
)
Issue Type: Bug Affects Versions: 6.1.5, 5.6.14 Assignee: Unassigned Components:
hibernate-core Created: 17/Nov/2022 11:42 AM Priority: Major Reporter: Hansi B (
https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=63736b9...
)
This entity definition results in a runtime exception:
@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);
}
}
The exception points to the Convert-annotation on the Map:
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)
Indeed, the code in CollectionPropertyHolder explicitly enforces this, and a comment
states:
// ... JPA says
// that @Convert on a Map always needs to specify attributeName of key/value (or
prefixed with
// key./value. for embedded paths).
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.html: (
https://docs.jboss.org/hibernate/jpa/2.2/api/javax/persistence/Convert.html: )
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;
(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 Maps with basic type values, since the standard specifies:
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.
(Indeed, Eclipselink will raise an exception when attributeName="value" is
specified on a Map with values of a basic type.)
(
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 )