Answer the question
In order to leave comments, you need to log in
How to store complex entity in JPA?
Hello. There was a question of misunderstanding of one moment.
Suppose there is a class
@Entity
@Cacheable
public class Agenda {
@Id
@GeneratedValue
private Integer id;
@ManyToOne
@JoinColumn(name = "event")
private Event event;
@Size(max = 4000)
private String name;
@Size(max = 4000)
private String subject;
private Date startDateTime;
private Date endDateTime;
@ManyToOne
@JoinColumn(name = "room")
private Room room;
@ManyToOne
@JoinColumn(name = "classifier")
private Classifier classifier;
private Date datetime;
@ManyToOne
@JoinColumn(name = "composer")
private Profile composer;
@Version
private Integer version;
private boolean enable;
@Size(max = 250)
private String lector;
.... get'еры и set'еры
@RequestMapping(method = RequestMethod.POST
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public Agenda addAgenda(@RequestBody Agenda agenda) {
return agendaService.addAndReturn(agenda);
}
Answer the question
In order to leave comments, you need to log in
There is a "clumsy" way: before saving, create new instances of " Room, Classifier, Profile, Event ", substitute " these same IDs " in them, then substitute the instances in Agenda.
If you want elegance, reconsider the architecture. Alternatively, map in Agenda not classes, but simple fields - identifiers. To exchange between the client and the server such lightweight entities, and already on the client to substitute instances, getting them from separate storages. This is how, for example, ExtJs works, providing convenient functions for "expanding" ID-shnikov into instances when displayed to the user and "folding" back when transferred to the server.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question