我应该在哪里应用 JPA 注释、getter 或字段?

发布于 2024-12-05 09:04:28 字数 79 浏览 0 评论 0原文

我是一名 JPA 初学者,我实际上正在尝试学习这个 API 的基础知识,我的问题是;将注释应用于字段和将其应用于其 getter 之间有区别吗?

I'm a JPA beginner and i'm actually trying to learn the basics of this API, my questionn is ; is there a difference between applying an annotation to a field and applying it to its getter?

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

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

发布评论

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

评论(2

善良天后 2024-12-12 09:04:28

我没有很好的 JPA 参考,但如果您使用 Hibernate,则在 Hibernate 注解参考。它说:

根据您是否注释字段或方法,Hibernate 使用的访问类型将是字段或属性。 EJB3 规范要求您在要访问的元素类型上声明注释,即,如果使用属性访问,则声明 getter 方法;如果使用字段访问,则声明字段。应避免在字段和方法中混合注释。 Hibernate会根据@Id或@EmbeddedId的位置猜测访问类型。

Hibernate 使用反射来访问值,这个选择将决定它是调用 getter 和 setter 还是使用直接字段访问。

哪种选择实际上更好是有争议的,并且可能会有所不同,具体取决于您是否超出了 getter 和 setter 中明显的字段获取和设置范围。

I don't have a good JPA reference for it, but if you're using Hibernate there are some notes about this choice in the Hibernate Annotations reference. It says:

Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. the getter method if you use property access, the field if you use field access. Mixing annotations in both fields and methods should be avoided. Hibernate will guess the access type from the position of @Id or @EmbeddedId.

Hibernate uses reflection to access the values, and this choice will determine whether it calls the getters and setters or uses direct field access.

Which choice is actually better is debatable and may vary depending on whether you do work beyond the obvious getting and setting of fields in your getters and setters.

北方的巷 2024-12-12 09:04:28

正如其他人所说,字段访问和属性访问之间存在差异。字段表示持久性提供程序的直接访问,属性(方法访问)表示使用您选择的 getter/setter 来访问数据。

我建议从现场访问开始,因为根据我的经验,这对于新用户来说更容易/更安全。属性访问可能非常有用,并且通常可能受到某些人的青睐,但如果您不小心,它也可能会导致问题(由于 getter 和 setter 可能具有的附加行为)。因此,一般来说,初学者最好使用字段访问,除非您有理由使用属性访问。

如果您刚刚开始使用 JPA,我还建议您阅读 Mike Keith 和 Merrick Schincariol 编写的 Pro JPA 2。它涵盖了这个主题和许多其他相关主题。

There is a difference between Field and Property access as others have stated. Field indicates direct access by the persistence provider and Property (method access) indicates the use of the getter/setter of your choosing to access the data.

I would recommend starting with Field access as in my experience this is easier/safer for new users to get started with. Property access can be very useful, and may be generally preferred by some, but it can also lead to problems if you are not careful (due to the additional behavior that getters and setters may have). So in general it is best to use Field access as a beginner unless you have a reason to use Property access.

I also recommend picking up a copy of Pro JPA 2 by Mike Keith and Merrick Schincariol if you are just getting started with JPA. It covers this and many other related topics.

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