问题
days
is a transient variable in Calendar
class. CalendarDay
class has foreign key reference to Calendar
class via the field called calendarId
.
When I fetch Calendar
object, days
field remains null. 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 variable in Calendar class is transient variable. How can I populate this transient variable in 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**
}
]
}
}
回答1:
Because of @Transient
annotation days
are not persisted. When fetching from database it will therefore remain null
.
Transient variables are such that are ignored when serializing - java keyword transient
- or persisted - JPA annotation @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. 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.
If the JSON is the result of serializing class you need to anyway calculate days
somehow still assuming it should be @Transient
.
BUT: as the first commentator states the JPA annotation combination you have is not sane. Maybe you want to remove the @Transient
annotation, so that when you calculate the values once from JSON you then persist them to db.
See more information:
Why does JPA have a @Transient annotation?
above also about the difference between annotation @Transient
and keyword transient
and because you deal with JSON also this might be good to read:
JPA Transient Annotation and JSON
来源:https://stackoverflow.com/questions/46986778/how-to-populate-transient-field-in-jpa