JPA - 在具有公共超类的 2 个不同实体之间复制数据
JPA - Copy data between 2 different entities with a common superclass
我有这样的情况
@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
的新实例,其中所有属性都从 [=11= 的实例继承自超类 "copied" ], 除了 EntityB
个特定的。
有一种 "smart" 方法可以做到这一点,而不需要每个人都做 set/get?
N.B。上面的代码只是一个例子,在我的真实案例中,超类有超过 100 个属性...
Apache Beanutils 库具有这样的功能。
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);
我有这样的情况
@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
的新实例,其中所有属性都从 [=11= 的实例继承自超类 "copied" ], 除了 EntityB
个特定的。
有一种 "smart" 方法可以做到这一点,而不需要每个人都做 set/get?
N.B。上面的代码只是一个例子,在我的真实案例中,超类有超过 100 个属性...
Apache Beanutils 库具有这样的功能。
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);