I am having some trouble understanding what I'm meant to do in the face of the following error: javax.ejb.EJBException: java.lang.IllegalStateException: Multiple representations of the same entity
I have code that's something like this:
@Entity
@Table(name="Survey", schema="dbo")
public class Survey {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column
private String line;
@OneToMany(mappedBy = "survey", cascade=CascadeType.ALL, orphanRemoval = true)
private List<Section> sections;
// getters and setters
...
public boolean equals(Survey other) {
if(other == null) {
return false;
} else if (other.id != id) {
return false;
} else if (!other.line.equals(line)) {
return false;
}
return true;
}
}
@Entity
@Table(name="Section")
public class Section {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="surveyId")
private Survey survey;
@Column
private String sectionName;
}
Issue is, when I call em.merge on a Survey instance, say s1, I get the above Multiple representations error, where one of the representations is s1, and the other is the survey of one of the Section instances within s1's sections list.
From my perspective, these two representations seem identical, and should refer to the same object. How should I setup the relationships between these two classes so that all of the sections in a given Survey refer to the same Survey object?