java 在一个模型中结合 JAXB 和 JPA
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3269654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Combining JAXB and JPA in one model
提问by Mike Baranczak
I have to design a data model (in a Java EE 6 application) that will be persisted via JPA, and that also needs to be serialized via JAXB. The last time I did that, I had one group of entity classes with JAXB annotations, and another with JPA annotations. This meant that I had to have a lot of boilerplate code for translating between the two. I'm thinking of combining them, so that each class will have both types of annotations. I know this canbe done, but my question is, shouldit be? Will it cause any problems?
我必须设计一个将通过 JPA 持久化的数据模型(在 Java EE 6 应用程序中),并且还需要通过 JAXB 进行序列化。上次我这样做时,我有一组带有 JAXB 注释的实体类,另一组带有 JPA 注释。这意味着我必须有很多样板代码来在两者之间进行转换。我正在考虑将它们组合起来,以便每个类都有两种类型的注释。我知道这可以做,但我的问题是,应该是什么?它会导致任何问题吗?
采纳答案by Dan LaRocque
The question is a little too broad for me to answer. But I do have specific related experience using Hymansonunder JAXB with JPA entities that you might find interesting.
这个问题有点太宽泛了,我无法回答。但我确实有在 JAXB 下使用Hymanson和 JPA 实体的特定相关经验,您可能会觉得有趣。
In my case, I had a JPA model with roughly three dozen entities and lots of cyclic references. The graph of relationships between entities was also almost strongly connected. In other words, it was possible to navigate to nearly any entity in the set from any other by following entity relationships. In my case, with entities as described and Hymanson 1.5, overlaying JAXB annotations on my JPA entities turned out to be a bad idea.
就我而言,我有一个 JPA 模型,其中包含大约三打实体和大量循环引用。实体之间的关系图也几乎是强连接的。换句话说,通过遵循实体关系,可以从任何其他实体导航到集合中的几乎任何实体。就我而言,使用所描述的实体和 Hymanson 1.5,在我的 JPA 实体上覆盖 JAXB 注释被证明是一个坏主意。
For one thing, Hymanson 1.5 got into infinite recursion on the cyclic references. I consider that operator error rather than a bug. Hymanson is awesome software. Also, I think the upcoming 1.6 release provides new features to handle this per HymanSON-235. So this might be moot soon!
一方面,Hymanson 1.5 在循环引用上陷入了无限递归。我认为操作员错误而不是错误。Hymanson 是很棒的软件。此外,我认为即将发布的 1.6 版本提供了新功能来处理HymanSON-235 的每个问题。所以这可能很快就没有实际意义了!
My other difficulty related to serialized compactness in the face of strongly connected entities. Serializing all my entity relationships was impractical. I would have serialized an obscene amount of irrelevant information in every request by naively following all entity relationships to their full depth.
我的另一个困难与面对强连接实体时的序列化紧凑性有关。序列化我所有的实体关系是不切实际的。我会天真地跟踪所有实体关系到它们的全部深度,从而在每个请求中序列化大量不相关的信息。
I wanted to specify multiple serializations of my JAXB objects, choosing one with appropriate fields and relationships depending on the intended use. But, as far as I'm aware, JAXB and Hymanson offer no such flexibility. They offer significant flexibility in defining therepresentation -- what's transient, how lists look, etc. -- but I don't think multiple representations are possible for one object. Maybe there's a clever way to define multiple representations under JAXB or Hymanson and switch at runtime... I'd be interested to learn if such a thing exists. Perhaps there's a feature for this that I'm ignorant of, or some trickery that can be played with subclassing. But I couldn't find it, so ultimately I gave up and went with DTOs.
我想指定 JAXB 对象的多个序列化,根据预期用途选择一个具有适当字段和关系的序列化。但是,据我所知,JAXB 和 Hymanson 没有提供这种灵活性。他们在界定提供显著灵活性的表现-什么是短暂的,名单怎么看,等等-但我不认为多表示是可能的一个对象。也许有一种聪明的方法可以在 JAXB 或 Hymanson 下定义多个表示并在运行时切换……我很想知道是否存在这样的东西。也许有一个我不知道的功能,或者一些可以通过子类化来玩的技巧。但是我找不到它,所以最终我放弃了并选择了 DTO。
Again, this is all pretty specific to the model. Maybe these are non-issues for you (or maybe you have clever solutions for these problems!)
同样,这一切都非常特定于模型。也许这些对您来说不是问题(或者您对这些问题有聪明的解决方案!)
回答by bdoughan
This can definitely be done. I actually find the prospect of maintaining code to copy between the models more problematic.
这绝对可以做到。我实际上发现维护代码以在模型之间复制的前景更成问题。
EclipseLinkis a great choice for this application as it contains both a JPA (EclipseLink is the RI and open sourced from TopLink), and a JAXB implementation.
EclipseLink是此应用程序的绝佳选择,因为它包含 JPA(EclipseLink 是 RI 并从 TopLink 开源)和 JAXB 实现。
EclipeLink JAXB (MOXy)also contains a number of extensions for mapping JPA entities to XML:
EclipeLink JAXB (MOXy)还包含许多用于将 JPA 实体映射到 XML 的扩展:
- XPath base mappings
- Mapping bi-directional relationships (using @XmlInvereseReference)
- Handling composite key relationships
- Handling embedded key classes
- Handling java.sql.* types
- XPath 基础映射
- 映射双向关系(使用@XmlInvereseReference)
- 处理复合键关系
- 处理嵌入的键类
- 处理 java.sql.* 类型
For more information see:
有关更多信息,请参阅:
回答by DataNucleus
DataNucleus allows persistence using JPA to RDBMS (using JDBC behind the scenes) and XML (using JAXB behind the scenes). It can interprete your JPA annotations as defining how the JAXB serialisation is performed - you could add JAXB annotations too if you so wished
DataNucleus 允许持久性使用 JPA 到 RDBMS(在后台使用 JDBC)和 XML(在后台使用 JAXB)。它可以将您的 JPA 注释解释为定义 JAXB 序列化的执行方式 - 如果您愿意,也可以添加 JAXB 注释

