将JPA实体用作域模型是一种好习惯吗? [英] Is it a good practice to use JPA entities as domain models?

查看:56
本文介绍了将JPA实体用作域模型是一种好习惯吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

还是要创建一个由域模型组成的域层,并与JPA实体进行对话以访问数据库?
这两种方法的利弊是什么?谢谢!

or to create a domain layer that is consisted of domain models and talk to JPA entities for database accessing? What are the pros and cons for both approaches ? Thanks!

推荐答案

这确实取决于您对域的编码方式。

This does really depend on how you code your domain.

一般而言(在Java中),我更喜欢创建一组单独的JPA批注的DTO,以实现持久性。这样的DTO将与db表匹配,并将在自定义存储库中使用

In general (in Java) I prefer create a separate set of JPA annotated DTOs to work with persistence. Such DTOs will match db table and will be used within a custom repository that


  • 向客户公开语义API

  • 将针对此类DTO运行查询

  • 将使用工厂初始化域对象以返回客户端

这种方法使域模型与db模式真正脱钩,因此您可以独立地进行扩展。这种方法的缺点是您需要编写更多代码,但是我发现编写维护代码相当容易且便宜。

This approach make domain models truly decoupled from the db schema so that you can evolve both independently. The cons of this approach is that you have more code to write, but I find such code quite easy and cheap to write an maintain.

在实体上使用JPA注释是一种这是非常普遍的方法,但是随着您的领域模型的发展和变得越来越复杂,我的经验是您必须面对比其他解决方案要昂贵得多的问题。

Using JPA annotation on entities is a quite common approach, but as your domain model evolves and becomes more complex, my own experience is that you have to face with problems that are far more expensive than the other solution.

这篇关于将JPA实体用作域模型是一种好习惯吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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