简体   繁体   English

使用Web Service客户端JAXB生成的类作为JPA实体

[英]Using Web Service Client JAXB Generated Classes as JPA Entities

I am working on a project to retrieve data from a SOAP webservice and store it in a database for later use using JPA. 我正在研究一个从SOAP Web服务检索数据并将其存储在数据库中以供以后使用JPA使用的项目。 Presently, I am using Spring WS as the client, and generating web service classes using JAXB. 当前,我使用Spring WS作为客户端,并使用JAXB生成Web服务类。

The goal is to replicate all of the data contained in these classes (which are rather large) faithfully in the database, and to that end it is mildly tempting to apply JPA annotations to the JAXB generated classes. 目标是忠实地在数据库中复制这些类(相当大)中包含的所有数据,因此,将JPA批注应用于JAXB生成的类是很诱人的。 In the long run this seems dangerous, because the generated classes seem rather ephemeral, and I would do a lot of work re-applying jpa annotations while regenerating code if the wsdl ever changed. 从长远来看,这似乎很危险,因为生成的类看起来很短暂,而且如果wsdl发生更改,我将在重新生成jpa批注的同时进行大量工作,同时重新生成代码。

The other option is to have my webservice client copy data into JPA entities created by me, possibly using a factory pattern. 另一个选择是让我的Web服务客户端将数据复制到我创建的JPA实体中,可能使用工厂模式。 This decouples JPA persistence from the possibly changing whims of the WSDL designer, and somehow feels safer. 这使JPA持久性与WSDL设计器可能改变的想法分离开来,并且感觉更安全。 It also feels safer because the entity classes can never be overwritten by a build task. 由于实体类永远不会被构建任务覆盖,因此它也更安全。

What is the best practice for this situation? 在这种情况下,最佳做法是什么? Obviously, excessive decoupling does not pay off when it means lots of data transfer objects, but is this case special? 显然,过度的去耦在意味着大量数据传输对象时并没有得到回报,但是这种情况特殊吗? Should JAXB created classes be used only in the webservice client, and quickly forgotten by the higher level application? JAXB创建的类是否应该仅在Web服务客户端中使用,并很快被更高级别的应用程序所忘记?

As a principle DO NOT change the objects generated by JAXB. 作为原则,请勿更改JAXB生成的对象。 As very rightly pointed out by you, these would change with every minor change to the WSDL. 正如您非常正确地指出的那样,随着WSDL的每次微小更改,这些都会更改。

The most accepted process would be to decouple your JAXB objects and the JPA Entities. 最可接受的过程是将JAXB对象和JPA实体分离。 There would be 2 primary advantages of the same 一样会有两个主要优点

  1. You can change your data binding provider at a later point of time if you feel JAXB is too slow or too memory intensive 如果您觉得JAXB太慢或太占用内存,则可以在以后的时间更改数据绑定提供程序。
  2. You are not tying your JPA code to your DB code. 您没有将JPA代码与数据库代码绑定在一起。 So if tomorrow your WSDL changes or your DB structure changes either of these are not impacted. 因此,如果明天您的WSDL更改或数据库结构更改,那么这两个都不受影响。

To transfer data between the 2 you can use a factory pattern or use a bean mapping framework like 要在两者之间传输数据,您可以使用工厂模式或使用Bean映射框架,例如

  1. Dozer 推土机
  2. Orika Orika
  3. Other options 其他选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM