问题描述
所以我了解到 Java 中的 transient
关键字意味着实体不会持久化,而 JPA 中的 @Transient
注释意味着不持久化字段到数据库.但是当 @Transient
应用于方法而不是变量时,这意味着什么?
So I have learned that the transient
keyword in Java means that an entity does not persist, and that the @Transient
annotation in JPA means don't persist a field to the database. But what does it mean when @Transient
is applied to a method rather than a variable?
这是我在我们的代码中找到它的地方:
This is where I found it in our code:
@Transient
public boolean getTabFoo() {
if ((this.viewFoo1 != ACCESS_NONE)
|| (this.viewFoo2 != ACCESS_NONE) || (this.viewFoo3 != ACCESS_NONE)
|| (this.getViewFoo4() != ACCESS_NONE)) {
return true;
}
return false;
}
推荐答案
所有字段级 JPA 注释都可以放在字段或属性上,它决定了实体的访问类型(即 JPA 提供者将如何访问该实体的字段)实体 - 直接或使用 getter/setter).
All field-level JPA annotations can be placed either on fields or on properties, it determines access type of the entity (i.e. how JPA provider will access fields of that entity - directly or using getters/setters).
默认访问类型由 @Id
注释的位置决定,并且它应该对实体的所有字段(或继承实体的层次结构)保持一致,除非被 @Access 显式覆盖
用于某些字段.
Default access type is determined by placement of @Id
annotation, and it should be consistent for all fields of the entity (or hiererchy of inherited entities), unless explicitly overriden by @Access
for some fields.
因此,getter 上的 @Transient
与字段上的 @Transient
具有相同的含义 - 如果您的实体的默认访问类型是属性访问,则需要注释所有 getter与 @Transient
不对应的持久属性.
So, @Transient
on getters has the same meaning as @Transient
on fields - if default access type for your entity is property access, you need to annotate all getters that don't correspond to persistent properties with @Transient
.
这篇关于@Transient 注释对方法意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!