gpt4 book ai didi

java - 我是否需要使用 @Access=FIELD 注释来进行多对一关系?

转载 作者:行者123 更新时间:2023-12-01 19:00:26 25 4
gpt4 key购买 nike

我有分为几类的实体。每个实体可以属于多个类别,因此我有一对多关联。

下面的 ORM 处理是否正确:

@OneToMany
@Access(AccessType.FIELD)
private Set<Category> parents = new HashSet<Category>();
public Set<Category> getParents() {
return parents;
}
public boolean addParent(Category parent) {
return parents.add(parent);
}
public boolean removeParent(Category parent) {
return parents.remove(parent);
}

我的具体问题是我是否需要使用@Access注释?如果我不使用它,那么 Hibernate 如何知道不映射 getParents getter?

最佳答案

我不知道您是否需要 @Access(AccessType.FIELD) 注释,因为这取决于一些默认值(见下文)。但如果您使用字段访问类型(默认情况下或通过 @Access(AccessType.FIELD)),则Hibernate 将直接访问 Field,而不使用 getter 或 setter!

参见Hibernate Reference, chapter 5.1.4.1.2. Access type ,了解有关访问类型确定算法的更多详细信息。

By default the access type of a class hierarchy is defined by the position of the @Id or @EmbeddedId annotations. If these annotations are on a field, then only fields are considered for persistence and the state is accessed via the field. If there annotations are on a getter, then only the getters are considered for persistence and the state is accessed via the getter/setter. That works well in practice and is the recommended approach.

关于java - 我是否需要使用 @Access=FIELD 注释来进行多对一关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12449906/

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