Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过Hibernate删除关联实体_Java_Database_Hibernate - Fatal编程技术网

Java 如何通过Hibernate删除关联实体

Java 如何通过Hibernate删除关联实体,java,database,hibernate,Java,Database,Hibernate,我有两个类,对应两个表。请假设所有的getter和setter都已添加到类中 public class Employee implements Serializable { //@ManyToOne( cascade = CascadeType.PERSIST, targetEntity = RackEntity.class ) //@Fetch( FetchMode.SELECT ) //@JoinColumn( name = "orgName", referencedColumnName

我有两个类,对应两个表。请假设所有的getter和setter都已添加到类中

public class Employee implements Serializable {

//@ManyToOne( cascade = CascadeType.PERSIST, targetEntity = RackEntity.class )
//@Fetch( FetchMode.SELECT )
//@JoinColumn( name = "orgName", referencedColumnName = "orgName", nullable = true )
//private Organization org;

private Long id;
private string empName;

@LazyCollection( LazyCollectionOption.FALSE )
@OneToMany( mappedBy = "emp", cascade = { CascadeType.ALL } )
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE,
        org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.PERSIST } )
private Set<Address> empAddress;
}
现在,当我尝试删除Employee实体时,它成功地删除了它,包括关联的地址实体

public void deleteById(Long id) {
    logger.info("Deleting Employee {}", id);
    Employee entity = (Employee) sessionFactory.getCurrentSession()
            .get(Employee.class, id);
    sessionFactory.getCurrentSession().delete(entity);
    sessionFactory.getCurrentSession().flush();
}
但当我在引入另一个类Organization和相应的表后,取消对Employee类中的代码的注释时,问题出现了,如下所示:

public class Organization implements Serializable {

private Long id;
private string orgName;

@LazyCollection( LazyCollectionOption.FALSE )
@OneToMany( mappedBy = "org", cascade = { CascadeType.ALL } )
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE,
        org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.PERSIST } )
private Set<Employee> emps;
}
公共类组织实现可序列化{
私人长id;
私有字符串orgName;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy=“org”,cascade={CascadeType.ALL})
@级联({org.hibernate.annotations.CascadeType.SAVE_UPDATE,org.hibernate.annotations.CascadeType.DELETE,
org.hibernate.annotations.CascadeType.MERGE,org.hibernate.annotations.CascadeType.PERSIST})
专用环境管理计划;
}
在这里,在适当地填充数据库之后,当我尝试使用相同的方法删除员工实体时,我得到以下异常:

org.hibernate.ObjectDeletedException:已删除的对象将通过级联重新保存(从关联中删除已删除的对象)

我猜这是因为员工实体仍然以字段emps的形式在组织实体中被引用。我试图找出解决办法,但没有得到任何详细的解决办法


那么,有谁能帮助我解决这个异常错误以及具体的理由吗?

我的想法是尝试将
孤儿删除=true
(在
OneToMany
-行中)添加到
组织
,然后将员工从集合中删除。它应该能很好地移除所有东西


有点过时:

您是说我需要从组织实体中清除字段、EMP并保存它吗。因此,相应的员工实体成为孤立实体,因此将从数据库中删除??因为如果是的话,那么我想在这里提到我想使用deleteById(Long id)方法来删除一名员工。我不想删除采用组织路线的员工实体!
public class Organization implements Serializable {

private Long id;
private string orgName;

@LazyCollection( LazyCollectionOption.FALSE )
@OneToMany( mappedBy = "org", cascade = { CascadeType.ALL } )
@Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE,
        org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.PERSIST } )
private Set<Employee> emps;
}