简体   繁体   English

JAXB解组到托管JPA实体

[英]JAXB unmarshalling to managed JPA entities

I've got a managed JPA entity (with subentities as well). 我有一个受管理的JPA实体(以及子实体)。 Some of the fields of given entities should be filled by unmarshalling an XML file retrieved from another datasource. 给定实体的某些字段应通过解组从另一个数据源检索的XML文件来填充。 The problem is that JAXB will not unmarshall the XML into existing objects but create a new object instead. 问题是JAXB不会将XML解组到现有对象中,而是创建一个新对象。 What is the simplest way to solve the problem? 解决问题的最简单方法是什么?

Instead of unmarshalling into an existing JPA object, you should use the JPA merge operation to merge the state of the detached entity into a persistence context. 您应该使用JPA merge操作将分离的实体的状态合并到持久性上下文中,而不是将其解组到现有的JPA对象中。

public void updateEmployee(Employee detachedEntity) {
   Employee managedEntity = entityManager.merge(detachedEntity);
   // do further operations on managedEntity and not detachedEntity
}

UPDATE UPDATE

If you are using JPA 2.1 then you could leverage entity graphs for this use case. 如果您使用的是JPA 2.1,则可以在此用例中利用实体图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM