简体   繁体   English

JPA双向关系与存储库/ DAO的使用

[英]JPA Bidirectional Relationships vs Repository/DAO-Use

i`m struggling myself since a while about this. 我已经为此奋斗了一段时间。 I have an entity graph with 30 entities. 我有一个包含30个实体的实体图。 Most of them are connected through bidirectional relationships. 它们中的大多数通过双向关系连接。 Some connections are very useful and some not, because they only serve the convenience in case of deletions between the entities. 有些连接非常有用,有些则没有用,因为它们仅在实体之间删除时才提供便利。

Furthermore I have repositories to create/find/store the entities. 此外,我还有用于创建/查找/存储实体的存储库。 Unfortunately there is a repository for every entity and it feels like, that these repositories are windows into my object graph. 不幸的是,每个实体都有一个存储库,感觉这些存储库是我的对象图中的窗口。 Is such thing a recommended implementation? 这是推荐的实现方式吗?

Because of the boiler plate code caused by the bidirectional relationships maintaining the parent/child associations I had the idea to replace some of these bidirectional relationships and put this into the repository. 由于由维持父/子关联的双向关系引起的样板代码,我想到了替换其中一些双向关系并将其放入存储库的想法。 Some of these relationships are only useful in conjunction with further parameters. 其中一些关系仅与其他参数结合使用。 Lets take the classic Customer <-> Order example. 让我们以经典的客户<->订单示例为例。 I think that the direction Customer -> Order is not always good, because sometimes there is a need to limit the result to a specific period. 我认为“客户->订单”的方向并不总是那么好,因为有时需要将结果限制在特定时期内。

My question is: What do you recommend to get out of this struggle? 我的问题是:您建议如何摆脱这场斗争? Do you always use bidirectional relationships in bigger projects for the convenience? 为了方便起见,您是否总是在大型项目中使用双向关系? Or do you replace them with specific repository queries? 还是用特定的存储库查询替换它们? Is it okay to maintain repositories for every entity? 可以为每个实体维护存储库吗?

I'm torn for a while... :-( 我被撕了一会儿... :-(

Well, in my opinion, it's difficult to say without knowing a little more about your setup. 好吧,我认为,要不多了解您的设置就很难说。 Are you using Spring CrudRepositories , custom Daos or EntityManager? 你是否使用Spring CrudRepositories ,定制Daos或EntityManager的? Perhaps it doesn't make that much of a difference, but I wonder about it. 也许这并没有太大的区别,但我对此感到奇怪。

In short, I was perfectly happy writing an application recently that had 10 or so Enities and only one service layer. 简而言之,最近,我非常高兴地编写了一个包含10个左右Enities和一个服务层的应用程序。 Perhaps it can help to think about business needs and start crafting a new service layer. 也许可以帮助考虑业务需求并开始构建新的服务层。 Despite having 30 or so entities, you may only have a handful of business points of view. 尽管有大约30个实体,但您可能只有少数几个业务观点。 Customers, obviously, and Orders. 客户,显然是订单。 Also, probably, Payments, Shipping, Returns. 另外,可能是付款,运输,退货。

Look at who is using the application and what do they call their departments. 查看谁在使用该应用程序以及他们如何称呼其部门。 Make a service for that department. 为该部门提供服务。 Put a call for every need for that department, even if you are duplicating some code from other departments. 即使您要复制其他部门的某些代码,也请致电该部门的所有需求。 Don't forget the IT department. 不要忘记IT部门。 Later, when you get a good idea of how these services are shaping up, you can factor out common actions. 稍后,当您对这些服务的形成方式有了一个很好的了解时,就可以排除一些常见的措施。

Hope this helps. 希望这可以帮助。

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

相关问题 JPA存储库中的双向关联不起作用 - Bidirectional Association In JPA Repository Not Working JPA - 什么时候使用关系? - JPA - when to use relationships? Spring Boot存储库中的API与DAO - API in Spring Boot Repository vs DAO 使用带有spring Boot的Dao设计模式,而不是使用spring data jpa提供的存储库设计模式,这是个好主意吗? - Is it good idea to use the Dao design pattern with spring Boot, instead of using repository design pattern provided by spring data jpa? 结合使用JPA的最佳方法 - Best way to use JPA with relationships 如何在Spring中使用SQLErrorCodeSQLExceptionTranslator和DAO类与@Repository? - How to use SQLErrorCodeSQLExceptionTranslator and DAO class with @Repository in Spring? 双向关系的垃圾收集 - garbage collection of bidirectional relationships 错误处理 JPA/Hibernate 实体双向关系的循环引用/依赖关系与 Jackson `JsonIgnoreProperties` - Errors Handling Circular References/Dependencies of JPA/Hibernate Entity Bidirectional Relationships with Jackson `JsonIgnoreProperties` jpa中如何使用表之间的关系 - How to use relationships between tables in jpa 是否可以同时使用 JPA 存储库和 Ignite 存储库? - is it possible to use JPA repository and Ignite repository simultaneously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM