0

Hi everyone,

I'd like to ask for help modeling my JPA entities and their relationships with each other. I use more abstract class names as class names in my project, so I'm going to translate that into a "more tangible" example.

I start from a festival where several gigs take place. Of course, every performance is assigned to exactly one festival. In a performance several instruments are played, each instrument is unique and accurately assigned to a performance. An instrument is only played by a musician and a musician plays only one instrument.
The classes have the following JPA annotations - Code (Java):

@Entity
public class Veranstaltung implements Serializable {
..
@OneToMany(mappedBy = "veranstaltung", cascade = CascadeType.ALL)
    private List<Auftritt> auftritte;
..
}

@Entity
public class Auftritt implements Serializable {
..
@ManyToOne
    private Veranstaltung veranstaltung;
@OneToMany(mappedBy = "auftritt", cascade = CascadeType.ALL)
    private List<Instrument> instrumente;
..
}

@Entity
public class Instrument implements Serializable {
..
@ManyToOne(cascade = CascadeType.PERSIST)
    private Auftritt auftritt;
..
}

I provided the whole thing with a REST interface, which returns test-based database entries as JSON.

Problem 1: In the relationship between instrument <-> musicians, I am unsure whether 1: n or 1: 1, as certain musicians in other performances also play other instruments.
Problem 2: I can query an event wonderfully and see which shows belong to it. But as soon as I ask for a certain appearance, there is no information about which event this belongs to. This is exactly the case with gigs <-> instruments. If I ask an appearance, I see which instruments are played. But if I ask for an instrument, I do not see to which concrete appearance it belongs ... I have implemented the relationships bidirectionally.

According to the JPA specification, the many side is the owner and the one side is the inverse side. The problem here is that querying the owner page does not provide the desired information. Hope is understandable, otherwise I have to post a little more code ...
Mental information?

1
Contributor
0
Replies
6
Views
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.