我在用户表和主题表之间有多对多的关系。连接表是 user_topic。主题实体有这个:
@ManyToMany(fetch = FetchType.LAZY, targetEntity = IntermediateUser.class)
@JoinTable(name = "user_topic", catalog = DataBaseConstants.DBCATALOG, joinColumns = { @JoinColumn(name = "topic_id", nullable = false, updatable = false) }, inverseJoinColumns = { @JoinColumn(name = "user_id", nullable = false, updatable = false) })
@Cascade(value = CascadeType.DELETE)
public Set<IntermediateUser> getVotingUsers() {
return votingUsers;
}
我在瞬态主题实例中更新集合“votingUsers”,然后调用休眠会话的更新方法。我不使用 setVotingUsers 方法,而是按方法清除 VotingUsers 集合votingUsers.clear()
,然后通过添加votingUsers.add(voter);
来更新我调用此方法:
sessionFactory.currentSession().update(topic);
结果,我在日志中看到以下几行
DEBUG org.hibernate.SQL- update javahelpdb.topic set content=?, `PUBLISH_DATE`=?, rate=?, forum=?, title=?, user=?, viewquantity=? where topic_id=?
DEBUG org.hibernate.SQL- delete from javahelpdb.user_topic where topic_id=?
DEBUG org.hibernate.SQL- insert into javahelpdb.user_topic (topic_id, user_id) values (?, ?)
所有参数都正确。我希望只有一个查询,例如:
update user_topic set topic_id=?, user_id=? where topic_id=?
如何指示休眠更新 user_topic 而不是删除和插入?
谢谢!