JPA更新一对多关系列表 [英] JPA update list of one to many relationship
问题描述
public class Question {
@OneToMany(fetch = FetchType.LAZY,mappedBy =question,cascade = CascadeType.ALL)
@JsonManagedReference
private List< Alternative> alternativeList;
$ b $ public class Alternative实现Serializable {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name =questionId,nullable = false)
@JsonBackReference
私人问题问题;
}
然后我想用新的替代列表的一套。为此,我使用新的Question对象调用JpaDao元素的合并方法:
@Repository
public class JpaQuestionDao实现了QuestionDao {
@PersistenceContext
private EntityManager em;
@Transactional
public问题更新(问题){
return em.merge(question);
}
}
然而,这实际上是合并了两个列表:已经在数据库中的一个和新提供的一个。我有这样的方法没有任何问题,当我有非列表对象,这就是为什么我一直使用合并方法。
有没有替代方法不合并,但只更新列表?
首先使用EntityManager.find()检索问题对象。问题对象将作为结果进行管理。由于alternativeList是延迟加载的,因此您必须调用collections对象上的一个方法来获取它并进行管理。对问题对象和列表的任何更新都会自动发送到数据库。
@Transactional
public Question更新(问题){
问题q = em.find(Question.class,question.getId());
q.setAlternativeList(null);
q.setAlternativeList(question.getAlternativeList());
...
return q;
}
或者,您可以尝试对@OneToMany集合使用orphanRemoval = true
@OneToMany(fetch = FetchType.LAZY,mappedBy =question,cascade = CascadeType.ALL,orphanRemoval = true)
@JsonManagedReference
private List< Alternative> alternativeList;
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?
Retrieve the question object first using EntityManager.find(). Question object will become managed as a result. Since alternativeList is lazily loaded, you have to invoke a method on the collections object to fetch it and make it managed as well. Any updates to the Question object and to the list will be automatically sent to the DB.
@Transactional
public Question update(Question question) {
Question q = em.find(Question.class, question.getId());
q.setAlternativeList(null);
q.setAlternativeList(question.getAlternativeList());
...
return q;
}
Alternatively, you can try using orphanRemoval=true on your @OneToMany collection
@OneToMany(fetch = FetchType.LAZY, mappedBy = "question", cascade = CascadeType.ALL, orphanRemoval=true)
@JsonManagedReference
private List<Alternative> alternativeList;
这篇关于JPA更新一对多关系列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!