Richard Zowalla created OPENJPA-3073:
----------------------------------------

             Summary: Attribute of a non-Serializable type converted only by an 
autoApply converter is silently left out of the mapping
                 Key: OPENJPA-3073
                 URL: https://issues.apache.org/jira/browse/OPENJPA-3073
             Project: OpenJPA
          Issue Type: Bug
            Reporter: Richard Zowalla
            Assignee: Richard Zowalla
             Fix For: 4.2.0


Description:

An entity or embeddable attribute whose type does not implement 
java.io.Serializable (for example a final value class or a record) and that has 
no mapping annotation is not persisted, even though a @Converter(autoApply = 
true) AttributeConverter<X, Y> is registered for its type. OpenJPA only logs 
'Fields "..." are not a default persistent type, and do not have any 
annotations indicating their persistence strategy. They will be treated as 
non-persistent.' The attribute gets no column, its value is lost on persist, 
JPQL that references it fails with 'No field named "..."', and the metamodel 
has no such attribute. If the attribute is declared @Basic instead, the 
persistence unit fails with 'The type of field "..." isn't supported by 
declared persistence strategy "Basic"'.

 

Example:

  public record Weight(long grams) \{ }

  @Converter(autoApply = true)

  public class WeightConverter implements AttributeConverter<Weight, Long> \{ 
... }

  @Entity public class Parcel \{ @Id long id; Weight weight; }   // weight is 
never stored

 

Jakarta Persistence 3.2, section 3.9: "A converted attribute is considered a 
basic attribute, since, with the aid of the converter, its values can be 
represented as instances of a basic type" and "If the value of the autoApply 
element of the Converter annotation is true, the converter is automatically 
applied to all attributes of the target type".

 

Cause: 
PersistenceMetaDataDefaults.getPersistenceStrategy()/isDefaultPersistent() 
decide whether an unannotated attribute is persistent without looking at the 
auto-apply converter registry of the MetaDataRepository. They only accept basic 
types, enums, embeddables, Serializable types and an explicit @Convert. The 
registry is only used later, in FieldMetaData.resolve(), and only for fields 
that are already persistent. AnnotationPersistenceMetaDataParser.parseBasic() 
likewise rejects @Basic on such a type.

 

Fix: such an attribute becomes a basic attribute mapped through its auto-apply 
converter, with a column of the converter's database type. This applies to 
field and property access and inside embeddables. Identifier, version, 
@Enumerated and @Temporal attributes and attributes with their own @Convert 
(including disableConversion = true) are excluded, as the specification 
requires. An attribute of such a type without any converter is still left out 
with a warning.

 

Upgrade



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to