Hello,
I am trying to setup a Tertiary Entity between Episode and Person to record EpisodeRole.
My Episode entity - which I think is setup correct is:
Code:
@Entity
public class Episode {
public Episode() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
....
@ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinTable(name = "episode_person", joinColumns = @JoinColumn(name = "episode_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "person_id", referencedColumnName = "id"))
private List<EpisodePerson> persons;
@OneToOne(cascade=CascadeType.ALL)
private Address address;
// getters setters
public List<EpisodePerson> getPersons() {
return persons;
}
Person entity is
Code:
@Entity
public class Person {
public Person() {
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
....
@JsonIgnore
@ManyToMany(mappedBy = "persons", cascade = {CascadeType.PERSIST, CascadeType.MERGE})
private List<EpisodePerson> episodes;
// getters and setters
public List<EpisodePerson> getEpisodes() {
return episodes;
}
And finally the EpisodePerson class:
Code:
@Entity
public class EpisodePerson {
@EmbeddedId
private EpisodePersonId id;
@ManyToOne
@JoinColumn(name = "fk_episode", insertable = false, updatable = false)
private Episode episode;
@ManyToOne
@JoinColumn(name = "fk_person", insertable = false, updatable = false)
private Person person;
public enum EpisodeRole {
Goodie,
Baddie,
Helper
}
@Column
@Enumerated(EnumType.STRING)
private EpisodeRole episodeRole;
public EpisodePerson(Episode e, Person p, EpisodeRole r) {
// create primary key
this.id = new EpisodePersonId(e.getId(), p.getId());
// initialize attributes
this.episode = e;
this.person = p;
this.episodeRole = r;
// update relationships to assure referential integrity
p.getEpisodes().add(this);
e.getPersons().add(this);
}
}
There is also a EpisodePersonId class.
Now the issue I am having is that since I started to implement this tertiary entity a number of methods in my EpisodeService have broken due to incompatible types being expected. For example:
EpisodeServiceImpl.java
Code:
@Override
@Transactional
public Episode saveEpisode(Episode episode) {
// CHECK FOR EXISTING PEOPLE ON NID
List mergedPeople = personService.mergeDetachedWithAttached( episode.getPersons() ); << --- not the input parameter required type
episode.setPersons( mergedPeople );
...
[code]
mergeDetachedWithAttached uses episode.getPersons() - but the getPersons method returns
[code]
public List<EpisodePerson> getPersons() {
return persons;
}
returns a List of EpisodePersons. So somewhere on Episode I need to either implement a getter/setter that returns a list of people or change the the saveEpisode method to use a list of EpisodePersons?
Hope this makes sense. I am relatively new so a detailed answer would be most helpful.
Thanks in advance.
Al