JAXB 解组到实体
我正在开发一个涉及 XML 流的项目,并将该数据转换为数据库中的持久数据。到目前为止,我已经可以使用我的 Web 服务了,我生成了一些 JAXB POJO,并且已经开始对 XML 流进行解组。
我试图弄清楚是否有一种方法可以直接从 XML 转到实体。
我知道我可以设计我的新数据库,生成一些实体,然后将 XML 解组到生成的 POJO,然后将 POJO 数据移动到实体并保留它。但这听起来很荒谬。任何人都给我提示在哪里寻找,我还没有发现任何谷歌搜索问题。我正在使用 Netbeans 6.9。
I am working on a project involving an XML stream and converting that data to persisted data in my Database. So far I have gotten to the point where I can consume my webservice, I generated some JAXB POJO's and have gotten unmarshaling of the XML stream started.
I am trying to figure out if there is a way to go right from XML to entities.
I know I can design my new database, generate some entities and then unmarshl the XML to the generated POJO's, then move the POJO data to entities and persist that. But that sounds pretty ridiculous. Anyone give me a hint where to look, I have not found anything googling the problem. I am using Netbeans 6.9.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建自己的 JAXB 注释对象 也是 Java Persistence API (JPA) 带注释的对象。使用 JPA,您可以将这些对象持久保存到数据库中。将 JAXB 和 JPA 注释保留在单个类上的好处是,对对象的任何更改只需要在该类、UI 表示和关联的 XSD 中进行,
但是,这在某些方面打破了 N 层背后的想法建筑学。您正在有效地将业务逻辑与持久层合并。为了消除重复,这可能是一个好主意,但有时您会发现自己使用
@Transient
和@XmlTransient
将仅在 XML 或数据库中需要的内容放入类,而不让它们影响不需要它们的区域。所以这是可能的,但在做之前你要权衡利弊。另请参阅此问题和< a href="https://stackoverflow.com/questions/3269654/combining-jaxb-and-jpa-in-one-model">这个问题了解更多关于权衡(都倾向于将两组注释放在一个地方)。
这个问题涵盖了一些工具,可以最大限度地减少在 XML 之间传输数据的工作量以及通过 JAXB 的数据存储。
You can create your own JAXB-annotated objects that are also Java Persistence API (JPA)-annotated objects. Using JPA you can persist those objects to your database. The benefit of keeping JAXB and JPA annotations on a single class is that any changes to your object only need to be made in that class, the UI representation, and an associated XSD
This does, however, in some ways break the ideas behind N tier architecture. You are effectively merging your business logic with your persistence tier. For the sake of removing duplication it might be a good idea, but occasionally you will find yourself using
@Transient
and@XmlTransient
to put things only needed in XML or the database into the class without having them effect the areas where they aren't needed.So it is possible, but you want to weigh the pros and cons before you do it. See also this question and this question for more thoughts on the trade-offs (all leaning to the side of putting both sets of annotations in one place).
This question covers some tools to minimize the effort of shuttling data between XML and a datastore via JAXB.