我有一个带有jpa注释但没有jaxb注释的JPA实体类:
@Entity
public class Category extends EntityObject {
@Id
private long id;
// getter setter and stuff
}
jaxb的所有内容都在外部xml文件中配置(因为我们需要对象的不同序列化)。
<xml-bindings package-name="mystuff.category">
<java-types>
<java-type name="mystuff.Category" xml-accessor-type="NONE">
<xml-root-element name="category" />
<java-attributes>
<xml-attribute name="name" java-attribute="name" />
<xml-element name="id2" java-attribute="id" />
</java-attributes>
</java-type>
// morestuff ...
当我将类别实例归结为xml时,我的问题就开始了。结果显示未在xml中配置的其他id元素。并且由于category(或entityObject)没有jaxb注释,我不明白它来自何处。
<category xsi:type="category" name="Category_3">
<id>1073741951</id>
<id2>1073741951</id2>
</category>
当我向moxy-xml显式添加id的xml元素条目时,我得到一个包含id两次的元素:
<id>10737419511073741951</id>
有人可以告诉我如何摆脱这个标签,它来自哪个?
修改
这里是EntityObject-Class
中与id相关的代码@MappedSuperclass
public abstract class EntityObject implements Serializable {
private static final long serialVersionUID = 1L;
public abstract long getId();
@Field // a solr annotation
public void setId(long id) {
if (getId() <= 0) {
setID(id);
}
}
protected abstract void setID(long id);
答案 0 :(得分:2)
问题是由于id
上的EntityObject
属性被覆盖在子对象Category
上。
解决方案#1 - EntityObject
和Category
位于同一个包中
假设EntityObject
与Category
位于同一个包中,您可以执行以下操作:
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="mystuff.category">
<xml-schema namespace="http://www.example.com/customer"
element-form-default="QUALIFIED" />
<java-types>
<java-type name="EntityObject">
<java-attributes>
<xml-transient java-attribute="id" />
</java-attributes>
</java-type>
<java-type name="Category" xml-accessor-type="NONE">
<xml-root-element name="category" />
<java-attributes>
<xml-attribute name="name" java-attribute="name" />
<xml-element name="id2" java-attribute="id" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
解决方案#2 - EntityObject
和Category
位于不同的包中
如果EnityObject
和Category
位于不同的包中,您可以创建第二个外部地图文档:
<?xml version="1.0" encoding="UTF-8"?>
<xml-bindings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
package-name="another.pkg">
<xml-schema namespace="http://www.example.com/customer"
element-form-default="QUALIFIED" />
<java-types>
<java-type name="EntityObject">
<java-attributes>
<xml-transient java-attribute="id" />
</java-attributes>
</java-type>
</java-types>
</xml-bindings>
下面是一些从多个外部映射文档引导的示例代码:
package forum9724475;
import java.util.*;
import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.JAXBContextFactory;
import another.pkg.EntityObject;
import mystuff.category.Category;
public class Demo {
public static void main(String[] args) throws Exception {
Map<String, Object> properties = new HashMap<String, Object>(1);
List<String> oxm = new ArrayList<String>(2);
oxm.add("mystuff/category/oxm.xml");
oxm.add("another/pkg/oxm.xml");
properties.put(JAXBContextFactory.ECLIPSELINK_OXM_XML_KEY, oxm);
JAXBContext jc = JAXBContext.newInstance(new Class[] {Category.class}, properties);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Category category = new Category();
category.setId(1073741951);
marshaller.marshal(category, System.out);
}
}
以下是运行演示代码的输出:
<?xml version="1.0" encoding="UTF-8"?>
<category xmlns="http://www.example.com/customer">
<id2>1073741951</id2>
</category>