gpt4 book ai didi

java - 将 JPA 注释添加到字段与 getter 之间有什么区别?

转载 作者:塔克拉玛干 更新时间:2023-11-01 23:04:04 26 4
gpt4 key购买 nike

<分区>

总的来说,我是 Spring Boot 和 JPA 的新手。我见过在字段声明上添加 JPA 注释的示例,例如:

@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 上放置相同注释的示例:

@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:

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com