gpt4 book ai didi

java - EclipseLink - JPA 和 MOXy - 同一数据模型的不同 View

转载 作者:行者123 更新时间:2023-11-29 05:44:03 25 4
gpt4 key购买 nike

这里是挑战......

我有一个基于相同模型的复杂分布式系统

看起来像这样:

A <-(XML)-> B <-(JSON)-> C

A、B 和 C 是不同的应用程序,基本上基于相同的模型,所以我决定在单独的 Java (Maven) 项目中维护该模型。

  • 每个应用程序都使用 JPA 进行持久化
  • 每个应用程序使用并保留仅实体属性的一个子集
  • 数据交换格式基于相同的模型(XML 或 JSON with MOXy)
  • 有两种交换格式仅使用实体属性的一个子集

下面是一个更技术性的实体示例(伪代码):

class Foo {
a;
ab;
bc;
c;
ac;
// ...
}

其中a被应用程序A使用,ab被应用程序A和应用程序B使用,bc被应用程序B和C使用,等等......

交换格式要求相同。

你知道如何实现它吗?

最好的问候。

编辑:该问题的最佳解决方案可能是从全局模型中自动生成不同的类。从上面的示例中获取实体,这看起来像这样:

应用程序 A:

class Foo {
a;
ab;
ac;
// ...
}

应用程序 B:

class Foo {
ab;
bc;
// ...
}

最佳答案

如果您使用的是 EclipseLink 2.5.0,则可以针对此用例利用 MOXy 的 @XmlNamedObjectGraphs 扩展。可以从以下链接下载 EclipseLink 2.5.0 候选发布版:

领域模型(Foo)

@XmlNamedObjectGraph 扩展允许您指定可用于编码和解码的映射子集。

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.*;

@XmlNamedObjectGraphs({
@XmlNamedObjectGraph(
name="a",
attributeNodes={
@XmlNamedAttributeNode("a"),
@XmlNamedAttributeNode("ab"),
@XmlNamedAttributeNode("ac")
}
),
@XmlNamedObjectGraph(
name="b",
attributeNodes={
@XmlNamedAttributeNode("ab"),
@XmlNamedAttributeNode("bc")
}
),
@XmlNamedObjectGraph(
name="c",
attributeNodes={
@XmlNamedAttributeNode("bc"),
@XmlNamedAttributeNode("c"),
@XmlNamedAttributeNode("ac")
}
)

})
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Foo {

int a;
int ab;
int bc;
int c;
int ac;

}

演示

在下面的演示代码中,我们将填充一个 Foo 实例,然后利用我们定义的对象图以四种不同的方式输出它。

import javax.xml.bind.*;
import org.eclipse.persistence.jaxb.MarshallerProperties;

public class Demo {

public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Foo.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

Foo foo = new Foo();
foo.a = 1;
foo.ab = 2;
foo.ac = 3;
foo.bc = 4;
foo.c = 5;

// Marshal to XML - Everything
marshaller.marshal(foo, System.out);

// Marshal to XML - Application A
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "a");
marshaller.marshal(foo, System.out);

// Marshal to JSON - Application B
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "b");
marshaller.marshal(foo, System.out);

// Marshal to JSON - Application C
marshaller.setProperty(MarshallerProperties.OBJECT_GRAPH, "c");
marshaller.marshal(foo, System.out);
}

}

输出

以下是我们从演示代码中生成的四种不同 View 。请记住,我们每次都编码了完全相同的 Foo 实例,并填充了相同的数据。

<?xml version="1.0" encoding="UTF-8"?>
<foo>
<a>1</a>
<ab>2</ab>
<bc>4</bc>
<c>5</c>
<ac>3</ac>
</foo>
<?xml version="1.0" encoding="UTF-8"?>
<foo>
<a>1</a>
<ab>2</ab>
<ac>3</ac>
</foo>
{
"foo" : {
"ab" : 2,
"bc" : 4
}
}{
"foo" : {
"bc" : 4,
"c" : 5,
"ac" : 3
}
}

了解更多信息

关于java - EclipseLink - JPA 和 MOXy - 同一数据模型的不同 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16437943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com