[英]How to populate @Transient field in JPA?
days
is a transient variable in Calendar
class. days
是Calendar
类中的一个临时变量。 CalendarDay
class has foreign key reference to Calendar
class via the field called calendarId
. CalendarDay
类通过名为calendarId
的字段具有对Calendar
类的外键引用。
When I fetch Calendar
object, days
field remains null. 当我获取
Calendar
对象时, days
字段保持为空。 How can I populate the field? 如何填充该字段?
@Entity
@Table(name = "calendars")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "calendars")
public class Calendar extends Node {
@Transient
@OneToMany(fetch = FetchType.EAGER)
@CollectionTable(name = "calendar_days", joinColumns = @JoinColumn(name = "calendarid"))
protected List<CalendarDay> days;
@JsonProperty("Days")
public List<CalendarDay> getDays() {
return days;
}
@JsonProperty("Days")
public void setDays(List<CalendarDay> days) {
this.days = days;
}
}
@Entity
@Table(name = "calendar_days")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Document(indexName = "calendar_days")
public class CalendarDay extends General {
@ManyToOne
@JoinColumn(name = "calendarid")
protected Calendar calendarId;
}
Here Days is null. 在这里,Days为空。 days variable in Calendar class is transient variable.
Calendar类中的days变量是瞬态变量。 How can I populate this transient variable in JPA?
如何在JPA中填充此瞬态变量?
Following is the response: 以下是响应:
{"calendars": {
"rows": [
{
"parentId": null,
"phantomParentId": null,
"children": null,
"expanded": false,
"index": null,
"leaf": true,
"Id": 1,
"Name": "General",
"DaysPerMonth": 20,
"DaysPerWeek": 5,
"HoursPerDay": 8,
"WeekendsAreWorkdays": false,
"WeekendFirstDay": 6,
"WeekendSecondDay": 0,
"DefaultAvailability": [
"08:00-12:00",
"13:00-17:00"
],
**"Days": null**
}
]
}
}
Because of @Transient
annotation days
are not persisted. 由于使用
@Transient
注释,因此无法@Transient
days
。 When fetching from database it will therefore remain null
. 因此,从数据库获取时,它将保持为
null
。
Transient variables are such that are ignored when serializing - java keyword transient
- or persisted - JPA annotation @Transient
. 瞬态变量在序列化时会被忽略-java关键字
transient
或持久化@Transient
批注@Transient
。
These values can - just as an example - hold data that is calculated or other way derived from other variables. 仅作为示例,这些值可以保存从其他变量计算得出或以其他方式导出的数据。 In such case the value is calculated once and stored in the variable so that it is not needed to be calculated again unless some of the values that it has been calculated from changes.
在这种情况下,该值只计算一次并存储在变量中,这样就无需再次计算它,除非已根据计算得出的某些值发生了变化。
So if we assume that it is @Transient
for a good reason you should somehow calculate the value of days
after fetch yourself. 因此,如果我们有充分的理由假设它是
@Transient
,则应该以某种方式计算获取自己后的days
。 If assumed that you want to do it based on the JSON you should then - for example - create a method that gets the required fields from JSON and populates days
based on those values. 如果假设您想基于JSON进行操作,则应该(例如)创建一个方法,该方法从JSON获取必填字段,并根据这些值填充
days
。
If the JSON is the result of serializing class you need to anyway calculate days
somehow still assuming it should be @Transient
. 如果JSON是序列化类的结果,则无论如何都需要以某种方式计算出
days
仍然假设它应该是@Transient
。
BUT: as the first commentator states the JPA annotation combination you have is not sane. 但是:正如第一个评论者所述,您拥有的JPA注释组合并不健全。 Maybe you want to remove the
@Transient
annotation, so that when you calculate the values once from JSON you then persist them to db. 也许您想删除
@Transient
批注,以便从JSON计算一次值后将其持久保存到db。 See more information: 查看更多信息:
Why does JPA have a @Transient annotation? 为什么JPA具有@Transient批注?
above also about the difference between annotation @Transient
and keyword transient
and because you deal with JSON also this might be good to read: 上面还谈到了注解
@Transient
和关键字transient
之间的区别,并且由于您使用JSON处理,因此可能也很容易阅读:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.