JPA - 复制2个不同的实体之间的数据与通用超

问题描述:

我有这样JPA - 复制2个不同的实体之间的数据与通用超

@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; 
} 

具有EntityA实例的情况下,我要的是创造EntityB新实例继承了所有的属性从“复制”来自EntityA的实例的超类,免除了特定的那些。

有没有一个“聪明”的方式来做到这一点,而不是做每一个单一/获得?

N.B.上面的代码只是一个例子,在我的真实情况下,超类拥有超过100个属性...

Apache Beanutils库具有此类功能。

https://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtils.html#copyProperties-java.lang.Object-java.lang.Object-

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);