I have a situation like this
@MappedSuperclass
public class BaseEntity {
@Column(name = "COL_1)
private String column1;
@Column(name = "COL_2)
private String column2;
}
@Entity
@Table(name = "TABLE_A")
public class EntityA extends BaseEntity {
@Id
private Long idA;
}
@Entity
@Table(name = "TABLE_B")
public class EntityB extends BaseEntity {
@Id
private Long idB;
}
Having an instance of EntityA
, what I want is to create a new instance of EntityB
with all the attribute inherited from superclass "copied" from the instance of EntityA
, exept the EntityB
specific ones.
There is a "smart" way to do this WITHOUT doing each single set/get?
N.B. The code above is just one example, in my real case the superclass has over 100 attributes...
Apache Beanutils library has such functionality.
package test;
import java.lang.reflect.InvocationTargetException;
import org.apache.commons.beanutils.BeanUtils;
...
EntityA instanceA = ...
EntityB instanceB = ...
BeanUtils.copyProperties(instanceB, instanceA);
// exceptions are thrown when some negative situation occur.
// I have no knowledge r/o property (having only getter) is not set / thow exception
//You should review, how this automatic task is executed in Your scenario. But it is auto
...persist(instanceB);