When mapping a mandatory component to an Entity class it is a good practice to initialize it in the Entity constructor and make it _final _like following:
{code:title=FooEntity.java|borderStyle=solid}
@Valid private final MyComp myComp= new MyComp(MyEnum.A);
public MyComp getMyComp() { return this.myComp; } {code}
In this case we must map it using field access Strategy: _access="field"_.
*The Problem:*
Hibernate will create a new instance by reflection, which is not necessary for performance reasons and it will also loose optional arguments in the component construction.
The component also needs a boilerplate empty constructor in this case.
*Feature:*
A new access strategy _access="field-final"_ will not create a new instance and just reuse the existing one.
The _<parent>_ mapping in this case is unnecessary since _this_ can be added to the component constructor.
*Advantages:*
- no need to create component instance by reflection - no empty constructor required? - parent mapping optional
|
|