Hibernate 如何删除JPA实体中的M2M对象?
在用户模型类中,我有以下内容:Hibernate 如何删除JPA实体中的M2M对象?,hibernate,jpa,jpql,Hibernate,Jpa,Jpql,在用户模型类中,我有以下内容: public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @ManyToMany(mappedBy = "attendees", cascade = CascadeType.ALL) @Cascade(org.hibernate.annotations.CascadeType.A
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToMany(mappedBy = "attendees", cascade = CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Set<Timeslot> timeslots = new HashSet<Timeslot>();
}
公共类用户实现可序列化{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@许多(mappedBy=“Attendes”,cascade=CascadeType.ALL)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
私有集时隙=新HashSet();
}
我想删除时间段。我尝试了一些东西,但没有成功,如下所示:
public static boolean deleteUserTimeslot(EntityManager em, Timeslot ts) {
EntityTransaction transaction = em.getTransaction();
try {
ArrayList<User> attendeeList = (ArrayList<User>) ts.getAttendees();
List<User> attendeesToRemove = (List<User>) getAllUsers(em);
transaction.begin();
for(User u: attendeesToRemove){
for(int i=0;i<attendeeList.size();i++){
if(attendeeList.get(i).getId()==u.getId()){
em.remove(u.getTimeslots());
break;
}
}
}
transaction.commit();
return true;
} catch (PersistenceException ex) {
//Rolling back data transactions
if (transaction != null && transaction.isActive()) {
transaction.rollback();
}
logger.error("Error making database call for update timeslot status");
ex.printStackTrace();
} catch (Exception e) {
logger.error(e.getMessage(), e);
e.printStackTrace();
}
return false;
}
公共静态布尔deleteUserTimeslot(EntityManager em,Timeslot ts){
EntityTransaction=em.getTransaction();
试一试{
ArrayList AttendeList=(ArrayList)ts.getAttendees();
列表参与者StoreMove=(列表)getAllUsers(em);
transaction.begin();
用于(用户u:attendeesToRemove){
对于(int i=0;i您真的需要与会者成为列表而不是集合吗?请参阅
I关于级联,有两个观察结果:
要级联注释,请删除其中一个(或两个)
不建议在许多关联中添加级联的状态
在or上启用级联通常没有意义
Cascade通常用于
和协会
在我看来,你想删除一个时隙,对吗?先删除时隙和用户之间的关系怎么样
// All attendees with this TimeSlot
ArrayList<User> attendees = (ArrayList<User>) ts.getAttendees();
// Bidirectional association, so let's update both sides of the relationship
// Remove TimeSlot from Attendees
for(User a : attendees){
a.getTimeslots().remove(ts);
}
// Remove Attendees from TimeSlot
ts.clear();
// Save the changes
em.merge(ts);
em.remove(ts);
//此时间段的所有与会者
ArrayList attendees=(ArrayList)ts.getAttendees();
//双向关联,所以让我们更新关系的双方
//从与会者中删除时间段
用于(用户a:与会者){
a、 getTimeslots()。删除(ts);
}
//从时间段中删除与会者
t.clear();
//保存更改
em.merge(ts);
em.remove(ts);
tryu.getTimeslots().clear();em.persist(u);
。您必须从用户中删除插槽并将其持久化,而不是直接删除插槽。谢谢,尝试过这样做,但似乎也不起作用。我认为级联不会起作用。级联意味着如果删除用户
,则其时隙
也将被删除。您需要的是孤立删除
标志,该标志已从集合中删除的实体。但是,这仅在@OneToMany
上可用,而不是@ManyToMany
。您的代码-我主要查看try
中的位-非常巴洛克。您真的在循环数据库中的所有用户
并将其与每个用户进行比较吗de>用户
s与特定的时隙
关联?为什么?您的最终目标是什么?最终目标是将每个用户放入数据库中,并检查该用户是否分配到时隙,如果是,则删除该用户的时隙。但这不起作用。尝试了各种方法,但均无效。谢谢,但它不起作用。此外re不是这样的删除方法,它给出了一个错误。请参阅我对“删除”的编辑。执行此操作后,记录是否仍在JoinTable中(可能命名为TimeSlot\u User)?