gpt4 book ai didi

java - 使用 Hibernate 保存对象时获取 DataViolationIntegrityException

转载 作者:行者123 更新时间:2023-11-30 00:45:20 25 4
gpt4 key购买 nike

我使用 Hibernate 作为 ORM 映射,使用 MySQL 作为后端服务器。

public class Child{


@NonNegative
@Column(name = "height", nullable = false)
private Double height;

setter//

public Double getHeight(){

return (height!=null)?height:0.0;

}

}

因此,当我进行保存调用时,我收到 DataViolationIntegrityException。我从客户端发送 null 高度,但 getter 方法将其转换为 0.0;那为什么我会收到该异常。

  session.save(child);

提前致谢。

最佳答案

DataViolationIntegrityException 如果我们在实体上执行 CRUD 操作时违反了实体或域类上的任何事物/约束,则会出现 DataViolationIntegrityException。

在上面的情况下 nullable=false 并且我试图保存 null,因此要么转换 0.0 而不是 null,要么将返回类型更改为 double 而不是 Double。

关于java - 使用 Hibernate 保存对象时获取 DataViolationIntegrityException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21424910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com