【问题标题】:Getting DataViolationIntegrityException while saving object using Hibernate使用 Hibernate 保存对象时获取 DataViolationIntegrityException
【发布时间】:2014-01-29 07:40:10
【问题描述】:

我使用 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。我从客户端发送空高度,但 getter 方法将其转换为 0.0;那为什么我会得到那个异常。

  session.save(child);

提前致谢。

【问题讨论】:

  • 如果不能为空,你能用双精度代替双精度吗?
  • 我可以使用它来制作我的解决方案。但是在项目中这个 pojo 类是先创建的,不建议更改它。

标签: java mysql hibernate annotations nullable


【解决方案1】:

DataViolationIntegrityException 如果我们在对 Entity 执行 CRUD 操作时违反了 Entity 或 Domain 类的任何事物/约束。

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

【讨论】:

    猜你喜欢
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-23
    • 2017-05-02
    • 2017-07-10
    • 1970-01-01
    相关资源
    最近更新 更多