在一个模型中结合 JAXB 和 JPA [英] Combining JAXB and JPA in one model

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

问题描述

我必须设计一个数据模型(在 Java EE 6 应用程序中),它将通过 JPA 持久化,并且还需要通过 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 下使用 Jackson 和 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 在循环引用上陷入了无限递归.我认为操作员错误而不是错误.Jackson 是很棒的软件.此外,我认为即将发布的 1.6 版本提供了新功能来处理每个 JACKSON-235.所以这可能很快就没有实际意义了!

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 和 Jackson 没有提供这种灵活性.它们在定义表示方面提供了很大的灵活性——什么是瞬态的,列表的外观等等——但我认为一个对象不可能有多种表示.也许有一种聪明的方法可以在 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆