Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.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 休眠从连接的父级删除子级_Java_Mysql_Hibernate - Fatal编程技术网

Java 休眠从连接的父级删除子级

Java 休眠从连接的父级删除子级,java,mysql,hibernate,Java,Mysql,Hibernate,我有一个这样的父母: @Entity @Table(name="employee") public class Employee implements Serializable { @Id @GeneratedValue() @Column(unique=true, nullable=false) private int id; @OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER, targetEntity=Priority

我有一个这样的父母:

@Entity
@Table(name="employee")
public class Employee implements Serializable {

@Id
@GeneratedValue()
@Column(unique=true, nullable=false)
private int id;

@OneToMany(cascade={CascadeType.ALL}, fetch=FetchType.EAGER, targetEntity=Priority.class, orphanRemoval=true)
@IndexColumn(name="idx")
@JoinColumn(name="employee_id")
private List<Priority> priorities;
@Entity
@Table(name="priority")
public class Priority implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(unique=true, nullable=false)
private int id;

@Column(length=255)
private String focus;

@ManyToOne(optional=true)
@JoinColumn(name="employee_id", nullable=false)
private Employee employee;
我只是和父母一起做手术。我想阅读、添加、编辑和删除父级的优先级。我不想为自己更新每个更改。我想一次更新所有更改。 我所做的: 阅读员工并列出其优先事项。现在我添加一个优先级,更改一个条目。 现在用

utx.begin();
emp = em.merge(employee);
utx.commit();
添加、编辑和阅读都很好,但删除和保存时会出现异常

08:57:02,159 INFO  [stdout] (http-localhost-127.0.0.1-8080-1) Hibernate: update priority set employee_id=null, idx=null where employee_id=? and id=?
08:57:02,161 WARN  [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) SQL Error: 1048, SQLState: 23000
08:57:02,162 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) Column 'employee_id' cannot be null
08:57:02,164 WARN  [com.arjuna.ats.arjuna] (http-localhost-127.0.0.1-8080-1) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple< 0:ffff7f000101:3dfa0b66:52a577bb:11, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@438f8fc4 >: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Column 'employee_id' cannot be null
我尝试了不同的方法,但我得到了一个异常,或者它没有从数据库中删除它


(顺便说一句,我正在使用MySQL作为数据库)

您从hibernate获得了正确的错误

当您将
员工
注释为

@JoinColumn(name="employee_id", nullable=false)
这意味着优先级类的employee字段不应为空

priority.setEmployee(null)

在这一行中,您将employee设置为null,并试图违反您在优先级类别的employee字段上应用的NOT null约束

08:57:02,162 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) Column 'employee_id' cannot be null

您还提到了
cascade={CascadeType.ALL}
这意味着在父类上执行的任何操作都应该在子类上执行,因此当您删除父类时,该父类的所有相关子项也会被删除,因此请根据您的需要设置它

您可能需要的是
inverse=“true”cascade=“ALL;delete orphan”

对您的问题感到困惑,您能否添加一些删除和保存员工优先级的代码
08:57:02,162 ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (http-localhost-127.0.0.1-8080-1) Column 'employee_id' cannot be null