Java 使用同一类的新实例更新/合并分离实体的所有字段-在Spring Boot中
我已经实现了以下继承Java 使用同一类的新实例更新/合并分离实体的所有字段-在Spring Boot中,java,spring,spring-boot,jpa,merge,Java,Spring,Spring Boot,Jpa,Merge,我已经实现了以下继承 abstract Superclass: ConnectionTechnologyDetails Subclasses: SMTPDetails, AS2Details and FTPDetails 子类的实例由Spring的JPA存储库保存。新数据来自前端表单,保存在子类的新实例中 不幸的是,我无法用新Instances的字段更新持久化实体。因为我不知道实例的实际类,所以我不能使用它们的setter。只有超类ConnectionTechnologyDetails的设置器
abstract Superclass: ConnectionTechnologyDetails
Subclasses: SMTPDetails, AS2Details and FTPDetails
子类的实例由Spring的JPA存储库保存。新数据来自前端表单,保存在子类的新实例中
不幸的是,我无法用新Instances的字段更新持久化实体。因为我不知道实例的实际类,所以我不能使用它们的setter。只有超类ConnectionTechnologyDetails的设置器可用
// getting the id of the persisted entity
Long connectionTechnologyDetailsDetachedId = connectionPersisted.getConnectionTechnology()
.getConnectionTechnologyDetails()
.getId();
// detach the persistent entity
ConnectionTechnologyDetails connectionTechnologyDetailsDetached =
connectionTechnologyDetailsRepository
.findById(connectionTechnologyDetailsDetachedId).get();
我想这样更新:
/** Update all fields with the fields of the
* new instance because I can't access
* the setters and getters of the subclasses here.
*/
connectionTechnologyDetailsDetached
.updateALlFieldsWith(
connectionRequest
.getConnectionTechnology()
.getConnectionTechnologyDetails()
);
connectionTechnologyDetailsRepository.save(connectionTechnologyDetailsDetached);
是否有方法更新所有字段,或者是否有可能访问子类的setter和getter?
到目前为止,我的解决方案是:从子类的DiscriminatorColumn注释中获取特定的子类。然后强制转换到特定的类,并用新值手动调用所有setter。Spring的BeanUtils.copyProperties(源、目标)完成了这项任务。谢谢有趣的解决方案。所以你会把所有东西都拉到超类,而不是把它转换到正确的子类?你确定这是个聪明的主意吗?我想我会选演员。因此,我要在哪个类中进行转换的信息可以在Annotation Discriminator列中找到。但是合并所有字段的方法会更有效,因为我必须写下对setter的调用。任何
BeanUtils
或类似的方法都可以让你将属性从一个对象复制到另一个对象。哦,好吧,酷!我对JavaEE、Spring等非常熟悉。类似这样的东西:BeanUtils.copyProperties(这个,另一个Bean)?