How to fix "could not execute statement; SQL [n/a]; constraint [null]" In OneToMany Relationship in Spring Boot

huangapple go评论69阅读模式
英文:

How to fix "could not execute statement; SQL [n/a]; constraint [null]" In OneToMany Relationship in Spring Boot

问题

我必须通过一个API执行删除操作。一旦我调用该API,它会产生一些嵌套异常,类似于:

无法执行语句;SQL [n/a];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句

无法执行语句;SQL [n/a];约束[null];嵌套异常是org.hibernate.exception.ConstraintViolationException:无法执行语句

我的实体类如下:

@Entity
@Table(name = "EMPLOYEE")
public class Employee {
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    @Id
    private String emp_id;
    
    @OneToMany(targetEntity = SalaryAllowances.class, cascade = CascadeType.ALL, orphanRemoval = true)
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name = "emp_id", referencedColumnName = "emp_id")
    private List<SalaryAllowances> salaryAllowances;
    private String firstName;
    private String lastName;
    //...
}

映射的实体类如下:

@Entity
@Table(name = "Allowances")
public class SalaryAllowances {
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    @Id
    private String allowanceId;
    private String name;
    private Double amount;
    // ...
}

这是我的仓库类。

@Repository
public interface EmployeeRepository extends CrudRepository<Employee, Long> {

    @Modifying
    @Query("DELETE FROM Employee e WHERE e.emp_id = :emp_id")
    void deleteEmployee(@Param("emp_id") String emp_id);

}

有人可以帮我解决这个问题吗?

谢谢。

英文:

I have to perform a delete operation through an API. Once I call that API it gives some nested exceptions like.

could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

and

could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

My Entities like

@Entity
@Table(name = &quot;EMPLOYEE&quot;)
public class Employee {
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    @Id
    private String emp_id;
    
    @OneToMany(targetEntity = SalaryAllowances.class, cascade = CascadeType.ALL, orphanRemoval = true)
    @Fetch(FetchMode.SELECT)
    @JoinColumn(name = &quot;emp_id&quot;, referencedColumnName = &quot;emp_id&quot;)
    private List&lt;SalaryAllowances&gt; salaryAllowances;
    private String firstName;
    private String lastName;
//...
}

and the mapped entity is like

@Entity
@Table(name = &quot;Allowances&quot;)
public class SalaryAllowances {
    @GeneratedValue(strategy= GenerationType.AUTO)
    private Long id;
    @Id
    private String allowanceId;
    private String name;
    private Double amount;
// ...
}

Here is my repository class.

@Repository
public interface EmployeeRepository extends CrudRepository&lt;Employee, Long&gt; {

    @Modifying
    @Query(&quot;DELETE FROM Employee e WHERE e.emp_id = :emp_id&quot;)
    void deleteEmployee(@Param(&quot;emp_id&quot;) String emp_id);

}

Can anyone help me to figure this out?

Thanks.

答案1

得分: 2

> 无法执行语句;SQL [n/a];约束条件 [null];嵌套异常为 org.hibernate.exception.ConstraintViolationException:无法执行语句

这个问题很难回答,没有完整的堆栈跟踪信息(但假设应该是 外键约束错误,因为您正在直接删除父行)

不过,您不必编写删除查询,可以使用 Spring 数据库存储库函数,您只需在调用代码中编写:

Employee employee = .... // 假设您已经从存储库中查询了对象

// 这里可能有一些验证或逻辑

employeeRepository.delete(employee);

// 这将删除与该雇员关联的 SalaryAllowances 实体,因为您使用了 CascadeMode.ALL

自己指定查询不起作用,请查看这个 答案链接 获取更多详细信息。

另外,@GeneratedValue 如果没有 @Id 字段将不起作用,请将该字段标记为 @Id,并使用 @NaturalId 标注 empId 字段。

英文:

> could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

This is difficult to answer without looking at full stacktrace (but assuming there should be Foreign key constraint error, because you are directly deleting parent row)

Although, you don't have to write the query to delete, use the spring data repository functions, you can simply write in your calling code:

Employee employee = .... //Assuming you have queried the object from repository

// Some validation or logic here then

employeeRepository.delete(employee)

// It will delete SalaryAllowances entities associated with this employee, since you have CascadeMode.ALL

Specifying your own query will not work, please check this answer here for more details.

Also, @GeneratedValue without @Id field will not work, please mark the field @Id and denote empId field with @NaturalId

huangapple
  • 本文由 发表于 2020年9月8日 21:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63795061.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定