简体   繁体   English

JPA - 什么时候使用关系?

[英]JPA - when to use relationships?

I am still not a stage where I feel completely comfortable with JPA.我还没有完全适应 JPA 的阶段。

Right now I am torn between using relationship annotations or simply retrieving the related objects when I need them with queries.现在,我在使用关系注释或在需要查询时简单地检索相关对象之间左右为难。

For instance I have a User who owns Projects.例如,我有一个拥有项目的用户。 I can use a .netomany relationship and retrieve the projects in an object fashion way OR I can simply query the user's projects when I need them.我可以使用 .netomany 关系并以 object 时尚方式检索项目,或者我可以在需要时简单地查询用户的项目。

The latter solution involves more code but somehow I have more freedom and control over what I want to do, or at least it is my impression.后一种解决方案涉及更多代码,但不知何故我对我想做的事情有更多的自由和控制权,或者至少这是我的印象。 The former will obviously take care of quite a fair amount of boiler plate code such as create/delete/update of objects, but there are quite a few tricks to learn along the way.前者显然会处理相当多的样板代码,例如对象的创建/删除/更新,但在此过程中有很多技巧需要学习。

I would greatly appreciate if someone could come up with a simple rule of thumb on when to use relationship annotations in JPA, preferably based on her/his experience.如果有人能就何时在 JPA 中使用关系注释提出一个简单的经验法则,最好是根据她/他的经验,我将不胜感激。

Thanks,谢谢,

Thomas托马斯

The basic rule is that you should create a relationship when you need it.基本规则是您应该在需要时建立关系。

For one-to-one and many-to-one relationships it's quite easy: you need them almost always.对于一对一和多对一的关系,这很容易:您几乎总是需要它们。 For example, when you display Project information you almost always need to display its owner information as well, thus creating a relationship is a good choice here.例如,当您显示Project信息时,您几乎总是需要同时显示其所有者信息,因此在这里创建关系是一个不错的选择。

One-to-many and many-to-many relationships require more care, since overuse of them can cause performance problems.一对多和多对多关系需要更加小心,因为过度使用它们会导致性能问题。

My personal rule of thumb is the following: if you don't need to display all Project s of User at once without pagination or filtering (or at least don't need to do it often), don't create a relationship.我个人的经验法则如下:如果您不需要在不进行分页或过滤的情况下一次显示User的所有Project (或者至少不需要经常这样做),则不要创建关系。 Otherwise you can create it (for example, you usually need all OrderLine s of Order at once, thus you need a one-to-many relationship in this case).否则您可以创建它(例如,您通常一次需要Order的所有OrderLine ,因此在这种情况下您需要一对多关系)。

Your issues are valid, relationships are hard in JPA-compliant ORMs.您的问题是有效的,在符合 JPA 的 ORM 中关系很难。 However the benefit of relationships is the ability to use them in JPA QL queries.然而,关系的好处是能够在 JPA QL 查询中使用它们。 Eg when you want to fetch all projects created by user with a given name you can write:例如,当你想获取由具有给定名称的用户创建的所有项目时,你可以这样写:

SELECT p
FROM Project p
WHERE p.user.name = 'Smith'

You cannot do this when instead of:你不能这样做而不是:

@ManyToOne
private User user

in the Project relationship you have a simple:Project关系中你有一个简单的:

private int userId;

Also investigate various fetching strategies to feel more comfortable with relationships.还要研究各种获取策略,以使人际关系更加舒适。

when there is a relationship between two entities and you expect to use it, use JPA relationship annotations.当两个实体之间存在关系并且您希望使用它时,请使用 JPA 关系注释。 One of the main advantages exactly is to not have to manually make extra queries when there is a relationship between entities.主要优点之一就是当实体之间存在关系时不必手动进行额外查询。

I would go for the annotation.我会 go 作为注释。 Its simpler and produces less code and it's kind of the whole point of using JPA. And also the one-to-many sets are lazy fetched by default, so you don't risk fetching stuff from the database that you don't need.它更简单,产生的代码更少,这就是使用 JPA 的全部意义。而且默认情况下,一对多集合是延迟获取的,因此您不会冒险从数据库中获取不需要的东西。

But I recommend reading up on lazy/eager fetching and learning how that works.但我建议阅读惰性/急切获取并了解其工作原理。

It all depends on how your application works with regards to retrieving data and the instances it needs them.这完全取决于您的应用程序如何检索数据以及它需要它们的实例。 Although JPA can be used anywhere, even outside a Java EE container (which is a huge advantage to use in a standard Java SE app, or with Spring etc.) I find that its 'way of thinking' is very much oriented towards Request - Response style interactions, mapping to web applications and the like (which to be fair is the most popular type of enterprise server-side application these days).尽管 JPA 可以在任何地方使用,甚至可以在 Java EE 容器之外使用(这是在标准 Java SE 应用程序或 Spring 等中使用的巨大优势)我发现它的“思维方式”非常面向请求 -响应式交互,映射到 web 应用程序等(公平地说,这是当今最流行的企业服务器端应用程序类型)。 However, unless you have some very specific needs I would definitely invest in learning the JPA relationship annotations.但是,除非您有一些非常具体的需求,否则我肯定会投资学习 JPA 关系注释。

In JPA everything goes around the EntityManager session. If you have an EntityManager instance managing your entities throughout the workflow you are mentioning where you need to get Users, Projects etc., all within one request-response then definitely go for using @OneToMany etc. because it will take care of getting the child entities whenever you need them without caring about how to join foreign keys and also carries with it optimisations on when to retrieve what.在 JPA 中,一切都围绕着 EntityManager session。如果你有一个 EntityManager 实例在整个工作流程中管理你的实体,你提到你需要在哪里获取用户、项目等,所有这些都在一个请求-响应中,那么肯定是 go 用于使用 @OneToMany 等。因为它会在您需要时负责获取子实体,而无需关心如何加入外键,并且还会对何时检索内容进行优化。 Even if you need to have the same Entity carried across requests, its pretty simple with refresh() and merge() .即使您需要跨请求携带相同的实体,使用refresh()merge()也非常简单。

I agree with you that the annotations tend to get a bit hairy with FetchType, CascadeType etc. and you might also face the occasional infamous cannot simultaneously fetch multiple bags PersistenceException, so its a bumpy ride, but once you get the hang of it I think its worth the investment.我同意你的看法,使用 FetchType、CascadeType 等时,注释往往会变得有点毛茸茸,而且你可能还会偶尔遇到臭名昭著的不能同时获取多个包的 PersistenceException,所以这是一个坎坷的旅程,但一旦你掌握了它,我认为值得投资。

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

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