Я создал образец приложения jHipster (url: http://jhipster.github.io/creating_an_app.html), используя подгенератор сущностей, я создал сущность Event, которая имеет связь OneToMany с EventImages. , EventTickets и EventQuestions, когда я получаю все (приложение, работающее на локальном компьютере, URL-адрес API: http://127.0.0.1:8080/api/события ) события Мне не удалось найти данные EventImages, EventTickets и EventQuestions в ответ.
Объект события
@Entity
@Table(name = "JHI_EVENT")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Event implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/* other fields */
@OneToMany(mappedBy = "event")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnore
private Set<EventTicket> eventTickets = new HashSet<>();
@OneToMany(mappedBy = "event")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnore
private Set<EventImage> eventImages = new HashSet<>();
@OneToMany(mappedBy = "event")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonIgnore
private Set<EventQuestion> eventQuestions = new HashSet<>();
/* getter and setters */
}
Объект EventImages
@Entity
@Table(name = "JHI_EVENTIMAGE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class EventImage implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "image_url")
private String imageUrl;
@ManyToOne
private Event event;
/* getters and setters */
}
аналогично сущности EventTickets и EventQuestions.
После некоторых исследований я обнаружил, что мне нужно удалить аннотацию @JsonIgnore для загрузки данных OneToMany Collections с использованием отложенной выборки, и я получил ответ null для EventImage, EventTicket и EventQuestions, как показано ниже.
[ {
"id": 1,
"title": "First Event",
"eventVenue": "xyz",
"startDate": "2015-05-28T10:10:00Z",
"endDate": "2015-06-20T10:10:00Z",
"eventTickets": null,
"eventImages": null,
"eventQuestions": null
} ]
Затем я обнаружил, что мне нужно использовать @JsonManagedReference и @JsonBackReference для отношения родитель/потомок, но нужно использовать fetch = Fetch.EAGAR (я хочу загружать коллекции OneToMany, когда я устанавливаю FetchType.LAZY по умолчанию, как когда вызывается сущность Event).
Объект события, когда я использовал @JsonManagedReference
@Entity
@Table(name = "JHI_EVENT")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Event implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/* other fields */
@OneToMany(mappedBy = "event", fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonManagedReference
private Set<EventTicket> eventTickets = new HashSet<>();
@OneToMany(mappedBy = "event", fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonManagedReference
private Set<EventImage> eventImages = new HashSet<>();
@OneToMany(mappedBy = "event", fetch = FetchType.EAGER)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@JsonManagedReference
private Set<EventQuestion> eventQuestions = new HashSet<>();
/* getter and setters */
}
Объект EventImage, когда я использовал @JsonBackReference
@Entity
@Table(name = "JHI_EVENTIMAGE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class EventImage implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "image_url")
private String imageUrl;
@ManyToOne
@JsonBackReference
private Event event;
/* getters and setters */
}
Как загрузить коллекции OneToMany лениво, т. е. EventImages, EventTickets и EventQuestions, в ответ на запрос объекта Event, т. е. http://127.0.0.1:8080/api/events Выполняется вызов REST.
Спасибо