N
N
Nevars2015-11-17 23:48:46
Hibernate
Nevars, 2015-11-17 23:48:46

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'еры

I accept the given entity in the controller
@RequestMapping(method = RequestMethod.POST
            consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)
    public Agenda addAgenda(@RequestBody Agenda agenda) {
        return agendaService.addAndReturn(agenda);
    }

As Room, Classifier, Profile, Event, I have to accept only id's. Question: how, knowing these same IDs, to save the Agenda entity to the database and at the same time not to fence the garden in the spirit of "at the service level, we will define the repositories of the Room, Classifier, Profile, Event entities, respectively", since for the sake of one method I don’t really want to inject several repositories + I think it's not entirely reasonable to make several queries to extract the relevant entities by IDs, and then save them in Agenda.
Can you please tell me if there is a more elegant way to get around this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Smirnov, 2015-11-26
@bobzer

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 question

Ask a Question

731 491 924 answers to any question