将JPA批注添加到字段与获取器之间有什么区别? [英] What is the difference between adding JPA annotations to fields vs getters?

查看:143
本文介绍了将JPA批注添加到字段与获取器之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般来说,我是Spring Boot和JPA的新手.我已经看到了在字段声明上添加JPA注释的示例,例如:

I am new to Spring Boot and JPA in general. I've seen examples of adding JPA annotations on field declarations such as this:

@Entity
public class Fizz {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    // other fields

    public Fizz(Long id) {
        super();

        setId(id);
    }

    // setter defined here

    public Long getId() {
        return this.id;
    }
}

...以及像这样在getter上添加相同批注的示例:

...as well as examples putting the same annotations on the getters like this:

@Entity
public class Fizz {
    private Long id;

    // other fields

    public Fizz(Long id) {
        super();

        setId(id);
    }

    // setter defined here

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long getId() {
        return this.id;
    }
}

我想知道它们在语义上是否等效,或者是否存在不同的用例,因此您会选择其中一种.我问是因为我实际上是在Groovy中编写我的Spring Boot/JPA应用程序,而您通常不定义getter:

I'm wondering if they are semantically equivalent or if there are different use cases where you'd choose one over the other. I ask because I'm actually writing my Spring Boot/JPA app in Groovy where you typically don't define getters:

@Canonical
@Entity
class Fizz {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id
}

推荐答案

默认情况下,JPA提供程序访问实体字段的值并将这些字段映射到数据库列 使用实体的JavaBean属性访问器(getter)和mutator(setter)方法.因此, 实体中私有字段的名称和类型与JPA无关.相反,JPA只关注 JavaBean属性访问器的名称和返回类型.您可以使用@javax.persistence.Access批注进行更改,这使您可以显式指定访问方法 JPA提供者应该雇用的人.

By default, JPA providers access the values of entity fields and map those fields to database columns using the entity’s JavaBean property accessor (getter) and mutator (setter) methods. As such, the names and types of the private fields in an entity do not matter to JPA. Instead, JPA looks at only the names and return types of the JavaBean property accessors. You can alter this using the @javax.persistence.Access annotation, which enables you to explicitly specify the access methodology that the JPA provider should employ.

@Entity
@Access(AccessType.FIELD)
public class SomeEntity implements Serializable
{
...
}

AccessType枚举的可用选项为PROPERTY(默认值)和FIELD.和 PROPERTY,提供程序使用JavaBean属性方法获取并设置字段值. FIELD使 提供者使用实例字段获取和设置字段值.最佳做法是,您应该坚持 设置为默认值,并使用JavaBean属性,除非您有令人信服的理由否则这样做.

The available options for the AccessType enum are PROPERTY (the default) and FIELD. With PROPERTY, the provider gets and sets field values using the JavaBean property methods. FIELD makes the provider get and set field values using the instance fields. As a best practice, you should just stick to the default and use JavaBean properties unless you have a compelling reason to do otherwise.

您 可以将这些属性注释放在私有字段或公共访问器方法上.如果 您使用AccessType.PROPERTY(默认值)并注释私有字段而不是JavaBean 访问器,字段名称必须与JavaBean属性名称匹配.但是,名称不 如果注释JavaBean访问器,则必须匹配.同样,如果使用AccessType.FIELD和 注释JavaBean访问器而不是字段,字段名称也必须与JavaBean匹配 属性名称.在这种情况下,如果您对字段进行注释,则它们不必匹配.最好只是 保持一致并为AccessType.PROPERTY注释JavaBean访问器,并为注释字段 AccessType.FIELD.

You can put these property annotations on either the private fields or the public accessor methods. If you use AccessType.PROPERTY (default) and annotate the private fields instead of the JavaBean accessors, the field names must match the JavaBean property names. However, the names do not have to match if you annotate the JavaBean accessors. Likewise, if you use AccessType.FIELD and annotate the JavaBean accessors instead of the fields, the field names must also match the JavaBean property names. In this case, they do not have to match if you annotate the fields. It’s best to just be consistent and annotate the JavaBean accessors for AccessType.PROPERTY and the fields for AccessType.FIELD.

永远不要混用JPA属性注释和JPA字段注释 在同一实体中.这样做会导致未指定的行为,并且非常 可能会导致错误.

You should never mix JPA property annotations and JPA field annotations in the same entity. Doing so results in unspecified behavior and is very likely to cause errors.

这篇关于将JPA批注添加到字段与获取器之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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