Hi,
I would advocate for Lindas proposal, giving some additional
unique/global/local arguments
Am 12.01.2012 01:49, schrieb Linda DeMichiel:
> Are there any additional comments on this proposal?
>
> If so, please post them now.
>
> If not, I will proceed to add this to the spec.
>
> thanks,
>
> -Linda
>
I see a problem: the name of the converter is unique in the pu but
the usage mimics locality for an entity.
In JSF there is also a converter annotation called FacesConverter
doing similar things. However it is used to annotate the converter (as
the name implies ?)
The refactored example:
@Converter("booleanToInteger")
public class BooleanToIntegerConverter implements
...
}
@Entity
public class Employee {
@Id long id;
@Convert("booleanToInteger")
boolean fullTime;
...
}
The Converter annotation uses "value" instead of "name" and is therefore
a little bit less verbose.
And the usage of "booleanToInteger" reflects its uniqueness instead
of doubling the string literal in one single entity class.
To demand for global application of the converter there is a second
attribute "forClass" in @Converter defining the Class to apply for.
Example
@Converter(forClass=SomeBusinessPropertyType.class)
public class ConverterForSomeBusinessPropertyType {
...
}
@Entity
public class Employee {
...
SomeBusinessPropertyType sbpt; // Converter applied automatically
...
}
regards
Bernd
>>> |A converter is defined using a javax.persistence.Converter
>>> annotation. It is similar to a TableGenerator and
>>> SequenceGenerator in that it can be defined on any entity class,
>>> mapped superclass, or entity attribute, and given a
>>> name, but the name must be unique within the scope of the persistence
>>> unit.
>>>
>>> The annotation to use on the attribute is defined as:
>>>
>>> |_at_Target({TYPE, METHOD, FIELD})
>>> @Retention(RUNTIME)
>>> public @interface Converter {
>>> String name();
>>> Class converterClass();
>>> }
>>> |
>>> In order to make use of a converter the @Convert annotation is used
>>> on an entity or mapped superclass attribute. The
>>> @Convert annotation is defined as:
>>>
>>> |_at_Target({METHOD, FIELD})
>>> @Retention(RUNTIME)
>>> public @interface Convert {
>>> String value();
>>> }
>>> |
>>> It annotates an entity attribute and specifies the name of the
>>> converter. The same converter can be referenced by any
>>> number of @Convert annotated attributes.
>>> Example:
>>>
>>> |_at_Entity
>>> @Converter(name="booleanToInteger",
>>> converterClass=BooleanToIntegerConverter.class)
>>> public class Employee {
>>> @Id long id;
>>> @Convert("booleanToInteger")
>>> boolean fullTime;
>>> ...
>>> }||
>>> |
>>> |public class BooleanToIntegerConverter implements
>>> EntityAttributeConverter<Boolean,Integer> {
>>> public Integer convertToDatabaseColumn (Boolean attributeObject) {
>>> return (attributeObject ? 1 : 0);
>>> }
>>> public Boolean convertToEntityAttribute (Integer dbData) {
>>> return (dbData > 0)
>>> }
>>> }
>>>
>>