I have a method to convert between objects of two different classes. These are DTO objects and hibernate entity classes.
public static DomainObject1 convertToDomain(PersistedObject1 pObj) {
if (pObj == null)
return null;
DomainObject1 dObj = new DomainObject1();
BeanUtils.copyProperties(pObj,dObj); //Copy the property values of the given source bean into the target bean.
return dObj;
}
Instead of having same method with DomainObject2
and PersistedObject2
and so on .. Is it possible to have a generic method with below signature? (without having to pass source and target class)
public static<U,V> U convertToDomain(V pObj) {
...}
PS: (A different topic that it is wasteful to use DTO when entities have same structure which some people don't agree to despite hibernate documentation and other sources)