In an entity MyEntity
I found something like this
@Override
public MyEntity clone(){
// TODO: do not use Orika here
MyEntity clone = new DefaultMapper().map(this, MyEntity.class);
clone.setFieldA(null);
clone.setFieldB(null);
return clone;
}
with DefaultMapper
being an orika-mapper:
import javax.enterprise.context.ApplicationScoped;
import ma.glasnost.orika.impl.ConfigurableMapper;
@ApplicationScoped
public class DefaultMapper extends ConfigurableMapper {
}
Is it legit to use orika for a deep copy? Or should one rather use SerializationUtils to achieve this?
The reason for the TODO
is: Everytime we call clone()
orika uses reflection to calculate the actual mapping. Sadly we cannot use injection because we are inside an entity.
We could as well make a constructor in which we map every single field by hand. This is no solution here because there are many fields with deep nesting. Furthermore, if a new column is added, there is a high risk of forgetting to adjust the mapping.
Do you have any better solution than to use orika? Is SerializationUtils an alternative at all?
Orika is way faster. On 10000 map-iterations orika needed 3 ms to map my object. SerializationUtils needed over 3 seconds to do the same. Furthermore, orika has a cache for already mapped objects. Of course this uses more metaspace. It depends on the situation but overall orika is a better solution in my case.