java 休眠主键的原语或包装器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3535791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Primitive or wrapper for hibernate primary keys
提问by Anonym
I've been looking at various hibernate tutorials and samples, for their identity/primary key property, some use a Java primitive type, some uses the wrapper type, that is;
我一直在查看各种 hibernate 教程和示例,对于它们的身份/主键属性,有些使用 Java 原始类型,有些使用包装类型,即;
private int id;
vs
对比
private Integer id;
Why and when would I use one over the other, for the entity key ?
为什么以及什么时候我会使用一个而不是另一个,作为实体键?
采纳答案by Pascal Thivent
From an Hibernate point of view, it doesn't change anything as Hibernate uses the same Hibernate type to represent them.
从 Hibernate 的角度来看,它不会改变任何东西,因为 Hibernate 使用相同的 Hibernate 类型来表示它们。
However, as pointed out by Bytecode Ninja, you can't distinguish the default value of a primitive int 0from a an assigned 0while there is no possible ambiguity with a null(a nullid always means a new entity), which is why I prefer to use a nullable wrapper type.
但是,正如 Bytecode Ninja 所指出的,您无法区分原始 int 的默认值和0分配的,0而 a 不可能有歧义null(nullid 总是意味着一个新实体),这就是为什么我更喜欢使用一个可为空的包装器类型。
And this is the Hibernate recommendation. From the Reference Documentation:
这是 Hibernate 的建议。从参考文档:
4.1.2. Provide an identifier property (optional)
Cat has a property called id. This property maps to the primary key column of a database table. The property might have been called anything, and its type might have been any primitive type, any primitive "wrapper" type, java.lang.String or java.util.Date. If your legacy database table has composite keys, you can use a user-defined class with properties of these types (see the section on composite identifiers later in the chapter.)
The identifier property is strictly optional. You can leave them off and let Hibernate keep track of object identifiers internally. We do not recommend this, however.
In fact, some functionality is available only to classes that declare an identifier property:
- Transitive reattachment for detached objects (cascade update or cascade merge) - see Section 10.11, “Transitive persistence”
- Session.saveOrUpdate()
- Session.merge()
We recommendthat you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.
4.1.2. 提供标识符属性(可选)
Cat 有一个名为 id 的属性。此属性映射到数据库表的主键列。该属性可能被称为任何名称,其类型可能是任何原始类型、任何原始“包装器”类型、java.lang.String 或 java.util.Date。如果旧数据库表具有复合键,则可以使用具有这些类型属性的用户定义类(请参阅本章后面的复合标识符部分。)
标识符属性是严格可选的。您可以不使用它们,让 Hibernate 在内部跟踪对象标识符。但是,我们不建议这样做。
事实上,某些功能仅适用于声明标识符属性的类:
- 分离对象的传递重新附加(级联更新或级联合并) - 请参阅第 10.11 节,“传递持久性”
- Session.saveOrUpdate()
- 会话.merge()
我们建议您在持久类上声明一致命名的标识符属性,并 使用可空(即非原始)类型。
And I actually leverage this in my base class:
我实际上在我的基类中利用了它:
@MappedSuperclass
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
@Transient
public boolean isNew() {
return (this.id == null);
}
}
回答by Behrang Saeedzadeh
I prefer using the wrapper types, because otherwise, when we use primitive types, the primary key idwill be set to 0by default, and 0could be the value of an actual idcolumn in the corresponding table of the entity, and this can sometimes lead to hard to pinpoint bugs.
我更喜欢使用包装器类型,因为否则,当我们使用原始类型时,主键id将0默认设置为,并且0可能是id实体对应表中实际列的值,这有时会导致查明错误。
But using a wrapper, the default value of idwill be nulland a primary key column cannot have a nullvalue, thus you can't update an entity that its idvalue is not set with a valid value by your application's logic.
但是使用包装器,idwill的默认值null和主键列不能有null值,因此您无法更新其id值未由应用程序逻辑设置为有效值的实体。
回答by Colin Hebert
You can specify your primary key as a primitive or a wrapper. And since usually the avantages of an Integeraren't needed, it's best to keep it simple with an int.
您可以将主键指定为原语或包装器。而且由于通常不需要 an 的优点Integer,因此最好使用int.
More info at oracle.com.
更多信息,请访问oracle.com。

