在 JPA(Hibernate 实现)中,哪种类型更适合用于实体的 id:
Boxed 类型(例如 Integer
)还是 Unboxed 类型(例如 int
)?
friend 说应该使用 Boxed 类型,因为当你在程序中创建一个新实体时,Hibernate 看到 id 为 null
并理解它应该在数据库中创建一个新行(在对比如果 id 不是 null
Hibernate 可能会更新数据库中的现有行)。
但我的实体的 id 是 int
,它运行良好,没有任何错误,我们知道原始实例变量的默认值为 0
。所以他说,也许hibernate把0
当作特殊的,假设对象是新的。
最佳答案
似乎 Current Documentation 建议使用盒装类型。
We recommend that you declare consistently-named identifier attributes on persistent classes and that you use a nullable (i.e., non-primitive) type.
关于java - 盒装与原始类型作为实体 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51642979/