Custom AttributeConverter — Concept Check
Questions on Custom AttributeConverter. AttributeConverter<X,Y> maps between a Java type and a DB column type; use for enums, JSON objects, encrypted values, or custom domain types not natively supported by JPA.
Questions in this quiz
8 questions on Custom AttributeConverter — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer wants to store Java enums in the database using a stable code instead of their name. What should they implement?
- AStore enums as integer values
- BUse AttributeConverter to map enum to code
- CDirectly store enum names as strings
- DUse @Enumerated(STRING) annotation
2.A team needs to store complex Java objects as JSON in a database column. Which approach should they use?
- AImplement AttributeConverter for JSON serialization
- BStore objects in multiple columns
- CUse @Enumerated(STRING) for JSON
- DDirectly store objects using toString()
3.An application encrypts sensitive fields before storing them in the database. How can this be achieved transparently?
- AManually encrypt fields before saving
- BStore encrypted data in a separate table
- CUse AttributeConverter with an EncryptionService
- DEncrypt data in the application layer only
4.What happens if you rename an enum used with @Enumerated(STRING) in a JPA entity?
- ARenaming has no effect on stored values
- BStored values are updated to new names
- CDatabase values may become invalid
- DEnum values are automatically updated
5.A developer notices that their custom AttributeConverter isn't applied automatically. What might be the issue?
- A@Converter lacks autoApply=true
- BThe converter is missing @Convert on fields
- CThe database column type is incorrect
- DThe Java type isn't supported by JPA
6.In a JPA entity, how can you ensure a custom type is always converted without specifying @Convert on each field?
- AManually convert fields in the entity
- BUse @Enumerated(STRING) annotation
- CSet autoApply=true in @Converter
- DDefine the converter in persistence.xml
7.A developer uses AttributeConverter for JSON storage but encounters deserialization errors. What might be wrong?
- AMissing @Convert annotation on fields
- BUsing @Enumerated instead of @Converter
- CIncorrect JSON structure in the database
- DDatabase column type mismatch
8.What is the role of the convertToEntityAttribute method in an AttributeConverter?
- AMaps Java type to database column type
- BSerializes Java objects to JSON
- CConverts database column values to Java type
- DEncrypts Java objects for storage