在一个模型中组合JAXB和JPA

在一个模型中组合JAXB和JPA

本文介绍了在一个模型中组合JAXB和JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须设计一个将通过JPA持久化的数据模型(在Java EE 6应用程序中),并且还需要通过JAXB进行序列化。我最后一次这样做,我有一组带有JAXB注释的实体类,另一组带有JPA注释。这意味着我必须有很多样板代码才能在两者之间进行转换。我正在考虑将它们组合在一起,以便每个类都有两种类型的注释。我知道可以完成,但我的问题是,应该吗?它会导致任何问题吗?

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 can be done, but my question is, should it be? Will it cause any problems?

推荐答案

这个问题对我来说有点过于宽泛。但我确实在JAXB下使用与您可能感兴趣的JPA实体有相关的相关经验。

The question is a little too broad for me to answer. But I do have specific related experience using Jackson under JAXB with JPA entities that you might find interesting.

在我的情况下,我有一个JPA模型,其中包含大约三十个实体和大量循环引用。实体之间的关系图也几乎紧密相连。换句话说,通过遵循实体关系,可以从任何其他实体导航到集合中的几乎任何实体。就我而言,对于所描述的实体和Jackson 1.5,在我的JPA实体上覆盖JAXB注释被证明是个坏主意。

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 Jackson 1.5, overlaying JAXB annotations on my JPA entities turned out to be a bad idea.

首先,Jackson 1.5对循环引用进行了无限递归。我认为操作员错误而不是错误。杰克逊是很棒的软件。此外,我认为即将发布的1.6版本提供了新的功能来处理每个。所以这可能很快就没有了!

For one thing, Jackson 1.5 got into infinite recursion on the cyclic references. I consider that operator error rather than a bug. Jackson is awesome software. Also, I think the upcoming 1.6 release provides new features to handle this per JACKSON-235. So this might be moot soon!

面对强关联实体时,我的其他困难与序列化紧凑性有关。序列化我所有的实体关系是不切实际的。我会通过天真地跟踪所有实体关系到它们的全部深度,在每个请求中序列化一些淫秽的无关信息。

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.

我想指定我的JAXB对象的多个序列化,根据预期用途选择具有适当字段和关系的字段。但是,据我所知,JAXB和杰克逊没有提供这样的灵活性。它们在定义 表示时提供了极大的灵活性 - 什么是瞬态,列表的外观等等 - 但我不认为一个对象可能有多种表示。也许有一种聪明的方法可以在JAXB或Jackson下定义多个表示并在运行时切换......我有兴趣了解这样的事情是否存在。也许有一个功能,我不知道,或一些可以通过子类化播放的技巧。但是我找不到它,所以最终我放弃了并且使用了DTO。

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 Jackson offer no such flexibility. They offer significant flexibility in defining the representation -- 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 Jackson 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.

同样,这一切都非常特定于模型。也许这些对你来说都不是问题(或者你可能有解决这些问题的聪明方法!)

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!)

这篇关于在一个模型中组合JAXB和JPA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:45