I have a Question entity with a list of another entity called Alternatives like this:
public class Question {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "question", cascade = CascadeType.ALL)
@JsonManagedReference
private List<Alternative> alternativeList;
}
public class Alternative implements Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "questionId", nullable = false)
@JsonBackReference
private Question question;
}
Then I wanted to update an exiting Question entry with a new set of Alternative list. For this I'm calling merge method of my JpaDao element with the new Question object:
@Repository
public class JpaQuestionDao implements QuestionDao {
@PersistenceContext
private EntityManager em;
@Transactional
public Question update(Question question) {
return em.merge(question);
}
}
However, this is in fact merging the two lists: the one already in database and the new provided one. I haven't had any problem with such method when I have non-list objects, that's why I kept using merge method.
Is there an alternative method to not merge but only update a list?