1Z0-900 · Question #22
Given the code fragment: How can you apply DateConverter to the birthday field?
The correct answer is A. by adding @Convert(to=Date.class) at line 3. Applying @Convert(converter=DateConverter.class) directly on the birthday field (line 3) is the correct JPA approach - it tells the persistence provider to use that specific AttributeConverter implementation when reading/writing that field to the database. Why the distractors…
Question
Given the code fragment:
How can you apply DateConverter to the birthday field?
Exhibit
Options
- Aby adding @Convert(to=Date.class) at line 3
- Bby invoking the setConverter(DateConverter.class) method on the EntityManager object
- Cby adding @Converter(autoApply=true) at line 1
- Dby adding @Convert((DateConverter.class)) at line 2
How the community answered
(34 responses)- A85% (29)
- B9% (3)
- C3% (1)
- D3% (1)
Explanation
Applying @Convert(converter=DateConverter.class) directly on the birthday field (line 3) is the correct JPA approach - it tells the persistence provider to use that specific AttributeConverter implementation when reading/writing that field to the database.
Why the distractors are wrong:
- B is wrong because
EntityManagerhas nosetConverter()method; JPA converters are wired declaratively via annotations, not programmatically. - C is wrong in intent:
@Converter(autoApply=true)belongs on the converter class itself (line 1), not the field - and it applies globally to all compatible types, not selectively tobirthday. - D is wrong due to invalid syntax;
@Convertrequires the named attributeconverter=, so the double-parenthesis form@Convert((DateConverter.class))won't compile.
Memory tip: Distinguish the two annotations by their targets - @Converter (with an r) lives on the converter class to register it; @Convert (no r) lives on the entity field to apply it. If you see "apply to a specific field," always reach for @Convert on the field.
Topics
Community Discussion
No community discussion yet for this question.
