Java Hibernate/JPA - 注释 bean 方法与字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/942035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Hibernate/JPA - annotating bean methods vs fields
提问by benstpierre
I have a simple question about usage of Hibernate. I keep seeing people using JPA annotations in one of two ways by annotating the fields of a class and also by annotating the get method on the corresponding beans.
我有一个关于 Hibernate 使用的简单问题。我一直看到人们通过注释类的字段和注释相应 bean 上的 get 方法,以两种方式之一使用 JPA 注释。
My question is as follows: Is there a difference between annotating fields and bean methods with JPA annoations such as @Id.
我的问题如下:使用 JPA 注释(例如 @Id)注释字段和 bean 方法之间有区别吗?
example:
例子:
@Entity
public class User
{
**@ID**
private int id;
public int getId(){
return this.id;
}
public void setId(int id){
this.id=id;
}
}
-----------OR-----------
- - - - - -或者 - - - - - -
@Entity
public class User
{
private int id;
**@ID**
public int getId(){
return this.id;
}
public void setId(int id){
this.id=id;
}
}
采纳答案by duffymo
Yes, I believe you want to search on field versus property access:
是的,我相信您想搜索字段与属性访问:
Hibernate Annotations - Which is better, field or property access?
Hibernate Annotations - 哪个更好,字段或属性访问?
The Spring preference is field access. That's what I follow.
Spring 首选项是field access。这就是我所遵循的。
回答by Steve Skrla
Yes, if you annotate the fields, Hibernate will use field access to set and get those fields. If you annotate the methods, hibernate will use getters and setters. Hibernate will pick the access method based on the location of the @Id
annotation and to my knowledge you cannot mix and match. If you annotate a field with @Id
, annotations on methods will be ignored and visa versa. You can also manually set the method with the class level annotation @AccessType
是的,如果您对字段进行注释,Hibernate 将使用字段访问来设置和获取这些字段。如果对方法进行注释,hibernate 将使用 getter 和 setter。Hibernate 将根据@Id
注释的位置选择访问方法,据我所知,您不能混合和匹配。如果您用 注释一个字段@Id
,方法上的注释将被忽略,反之亦然。也可以手动设置类级别注解的方法 @AccessType
The Hibernate Annotations reference guidehas proven to be an extremely useful resource for questions like this and details how access types cascade down hierarchies.
在Hibernate注解参考指南,已被证明是这样的问题和细节访问类型如何向下级联层次结构一个非常有用的资源。
回答by Brian
My recommendation is to annotate the methods. By doing so, you gain a little flexibility. For example, let's say you have a few classes:
我的建议是对方法进行注释。通过这样做,您可以获得一点灵活性。例如,假设您有几个类:
AbstractEntity
StringIdEntity
AutoIdEntity
AbstractEntity
StringIdEntity
AutoIdEntity
AbstractEntity
defines the id field/getter/setter. The classes StringIdEntity
and AutoIdEntity
inherit from AbstractEntity
, but use different @Id
strategies. If you annotate the field, you cannot change it from class to another.
AbstractEntity
定义 id 字段/getter/setter。类StringIdEntity
和AutoIdEntity
继承自AbstractEntity
,但使用不同的@Id
策略。如果您对该字段进行注释,则不能将其从类更改为另一个类。
If you annotate the methods, you mark getId()
as @Transient/abstract
in AbstractEntity
and then in the subclasses, you simply override the method and apply the strategy you wish to use. I used to annotate fields myself and ran into this and decided that I will always annotate methods moving forward.
如果您对方法进行注释,则将其标记getId()
为@Transient/abstract
inAbstractEntity
然后在子类中,您只需覆盖该方法并应用您希望使用的策略。我曾经自己注释字段并遇到了这个问题,并决定我将始终注释前进的方法。
So, even if you don't see the benefits of annotating methods immediately, it may become obvious down the line when you have so many classes that switching is going to become an absolute headache.
因此,即使您没有立即看到注释方法的好处,当您有如此多的类时,切换将成为一个绝对令人头疼的问题,这可能会变得很明显。
回答by Joseph Lust
Why would you annotate the accessors? It just looks plain messy and is a pain to maintain. Now I have to hunt all over the class to find out how JPA or Hibernate is applied. When working with some Hibernate code generation plugins for Eclipse this is the default and it drivers me bonkers.
为什么要注释访问器?它看起来很乱,维护起来很痛苦。现在我必须在全班寻找 JPA 或 Hibernate 的应用方式。当使用 Eclipse 的一些 Hibernate 代码生成插件时,这是默认设置,它让我发疯。
Not to mention that another reason we use accessors is to add logic to the property access or reference parent or other object which does not meld well with said annotations.
更不用说我们使用访问器的另一个原因是将逻辑添加到属性访问或引用父对象或其他与所述注释不能很好融合的对象。
回答by Mohammad Adnan
There are several discussion available that gives edge to use FIELD over property (see - http://java.dzone.com/tips/12-feb-jpa-20-why-accesstype). Even spring framework also recommends using FIELD over property (see - http://static.springsource.org/spring/docs/2.5.x/reference/orm.html). I find FIELD more cleaner approach not only because you are not forced to implement setters/getters but you can have your custom type setters/getters in your class. So a better encapsulation and more clean classes. Performance wise FIELD has a bit edge over PROPERTY but which is negligible.
有几个可用的讨论提供了在属性上使用 FIELD 的优势(参见 - http://java.dzone.com/tips/12-feb-jpa-20-why-accesstype)。甚至 spring 框架也建议使用 FIELD over property(参见 - http://static.springsource.org/spring/docs/2.5.x/reference/orm.html)。我发现 FIELD 更清洁的方法不仅因为您不必强制实现 setter/getter,而且您可以在类中使用自定义类型的 setter/getter。所以更好的封装和更干净的类。性能方面的 FIELD 比 PROPERTY 略有优势,但可以忽略不计。
IN one line use FIELD over PROPERTY. Use PROPERTY if u really need it.
在一行中使用 FIELD over PROPERTY。如果您确实需要,请使用 PROPERTY。