I have been working with custom attribute types, and JPA's Attribute Converter.
For an example @Entity
class like so:
@Entity
public class ExampleEntity {
private CustomAttribute customAttribute = new CustomAttribute();
public CustomAttribute getCustomAttribute() {
if(this.customAttribute == null){
this.customAttribute = new CustomAttribute();
}
return this.customAttribute;
}
}
My CustomAttribute
has some attributes of itself, and I do not want it to be null (ever). I had to choose between two alternatives, and both of those were examplified simultaneously:
- Instantiate the object on attribute declaration (like I did on the example for the field).
- Instantiate a new object for that attribute in case of a
true
null checking on a getter.
Since my @Entity
on production application has more than 10 attributes, it would be too verbose to apply any of those alternatives on all fields/getters.
Question: Is there any way to instantiate the CustomAttribute object on null field access?
I could not find any answer for this specific question on my research online, so any insight would be appreciated.
EDIT: My question is not about lazy instantiating of fields on relationship mappings, it is about instantiating custom attribute objects on access.