1

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**
        }
        ]
        }
}
pirho
  • 11,565
  • 12
  • 43
  • 70
user2207300
  • 121
  • 1
  • 3
  • 5
  • 2
    How can it be transient and an entity? Your question makes absolutely no sense. – Alan Hay Oct 28 '17 at 08:55
  • Please edit your question. You want to populate days based on the JSON response? Tell what you need to do more detailed in question. – pirho Oct 28 '17 at 09:08
  • Or maybe the _response_ is the result of serializing the Calendar to JSON? – pirho Oct 28 '17 at 09:22
  • I am trying to integrate a Gantt chart of www.bryntum.com. I am using Springboot. This component accepts a JSON response as shown above. If the declaration is incorrect, can you please help with the correct way to create the desired JSON – user2207300 Oct 30 '17 at 05:46

1 Answers1

2

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

pirho
  • 11,565
  • 12
  • 43
  • 70