问题描述
因此我了解到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注释对方法意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!