JAXb、Hibernate 和 beans

发布于 2024-12-11 20:00:51 字数 372 浏览 0 评论 0原文

目前我正在开发一个使用 Spring Web 服务、hibernate 和 JAXb 的项目。

1)我已经使用IDE“hibernate代码生成”生成了hibernate bean,

2)另外,我已经使用maven编译器生成了jaxb bean。

..

现在,我的问题是,

1)这是正确的方法吗? (有这么多豆子)。

2) 我应该使用 JAXb beans 来处理服务层吗?如何保持层解耦?

3)或者,我是否需要创建另一组bean,即。将(JAXb beans)映射到(新bean)到(hibernate beans)?

请说说你的看法?

谢谢, 阿迪

Currently I am working on a project with Spring web-service, hibernate and JAXb.

1) I have generated hibernate beans using IDE 'hibernate code generation,

2) also, I have generated the jaxb beans using maven compiler.

..

Now, my question is,

1) Is this the right approach? (to have so many beans).

2) Shall I use JAXb beans for processing into the service layer? How can I keep layers decoupled?

3) Or, do I need to create another set of beans ie. map (JAXb beans) to (new beans) to (hibernate beans)?

.

Please tell your views?

Thanks,
adi

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光沙漏 2024-12-18 20:00:51

你知道,你不可能让所有事情都完全解耦。总会有一个层知道其他两层。

通常,当我设计 3 层架构时,例如:

  1. 服务层 - 可能使用 JAXB、公开 Web 服务或其他 API 的
  2. 业务层 - 任何实际逻辑
  3. 持久层 - hibernate

我允许业务层了解服务层 (JAXB)以及关于持久层(hibernate beans)。但我不允许服务层和持久层相互了解。

You know, you cannot have everything fully decoupled. There will be always a layer that will know the other two layers.

Usually when I design 3 layers architecture, like:

  1. Service Layer - the one that probably uses JAXB, exposes web services or other APIs
  2. Business Layer - any real logic
  3. Persistence Layer - hibernate

I allow the Business layer to know about the Service Layer (JAXB) and about the Persistence Layer (hibernate beans). But I don't allow the Service Layer and the Persistence Layer to know about each other.

沒落の蓅哖 2024-12-18 20:00:51

注意:我是EclipseLink JAXB (MOXy) 的领导者和 JAXB 2 的成员(JSR-222)专家组。 EclipseLink 还提供了一个优秀的 JPA 实现(开源于TopLink)。

维护多个模型是有成本的。您添加的每个模型都会引入必须编写、测试和维护的 bean 到 bean 转换。

另一种方法是对 JPA 和 JAXB 绑定使用相同的 bean。对于此用例,从域模型开始并添加 JAXB 和 JAXB 会更容易。用于将映射应用到 XML 和数据库的 JPA 元数据。下面是利用单个模型创建 RESTful Web 服务的示例:

由于 EclipseLink 提供了两者JAXB 和 JPA 实现我们提供了许多扩展来简化此操作:


更新

响应:

同意你所说的。然而,使用相同的豆子会耦合
代码非常紧密并且会高度依赖。换一换
图层其他地方也需要更改。你说什么?

这完全取决于你如何看待事物。我对构建数据访问服务的偏好是设计和构建可靠的域模型。然后使用JPA和JAXB来解决对象关系和对象XML之间的阻抗不匹配。

一种模型方法

对 JPA 和 JAXB 使用一种模型意味着当您对模型进行更改时,您需要当时决定如何针对 JPA 和 JAXB 处理该模型(这可能很好)或坏)。如果您不希望模型中的每个新增内容都影响 JAXB 映射,您可以利用 JAXB 概念,例如 @XmlAccessorType(XmlAccessType.NONE)

两个(或更多)模型方法

当您想要添加映射到两个模型的字段时关系和XML你需要将其添加到两个模型中并添加必要的转换逻辑。在这种情况下,保持模型解耦是有成本的。

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group. EclipseLink also provides an excellent JPA implementation (open sourced from TopLink).

There are costs to maintaining multiple models. Each model you add introduces a bean-to-bean conversion that must be written, tested, and maintained.

Another approach is to use the same beans for both the JPA and JAXB bindings. For this use case it will be easier to start with domain model and add JAXB & JPA metadata to apply mappings to XML and the database. Below is example of where a single model is leveraged to create a RESTful web service:

Since EclipseLink provides both JAXB and JPA implementations we provide a number of extensions to make this easier:


UPDATE

In response to:

Agree to what you are saying. However, using same beans will couple
the code very tightly and will be highly dependent. Change in one
layer will need changes elsewhere as well. What you say?

It all depends how you look at things. My preference for building data access services is to design and build a solid domain model. Then use JPA and JAXB to solve the impedance mismatches between object-relational and object-XML.

One Model Approach

Using one model for both JPA and JAXB means that when you make a change to the model you need to decide at that time how it will be handled for both JPA and JAXB (this can be good or bad). If you don't want every new addition to the model to affect the JAXB mapping you can leverage JAXB concepts like @XmlAccessorType(XmlAccessType.NONE).

Two (or More) Models Approach

When you want to add a field that is mapped to both relational and XML you need to add it to two models and add the necessary conversion logic. In this case there is a cost to keeping the models de-coupled.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文