休眠主键的原语或包装器 [英] Primitive or wrapper for hibernate primary keys

查看:29
本文介绍了休眠主键的原语或包装器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看各种 hibernate 教程和示例,对于它们的身份/主键属性,有些使用 Java 原始类型,有些使用包装类型,即;

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;

 private int id; 

对比

 private Integer id;

为什么以及什么时候我会使用一个而不是另一个,作为实体键?

Why and when would I use one over the other, for the entity key ?

推荐答案

从 Hibernate 的角度来看,它不会改变任何东西,因为 Hibernate 使用相同的 Hibernate 类型来表示它们.

From an Hibernate point of view, it doesn't change anything as Hibernate uses the same Hibernate type to represent them.

然而,正如 Bytecode Ninja 所指出的,你无法区分原始 int 0 的默认值和分配的 0 ,而没有可能的歧义带有 null(null id 总是表示一个新实体),这就是为什么我更喜欢使用可为 null 的包装器类型.

However, as pointed out by Bytecode Ninja, you can't distinguish the default value of a primitive int 0 from a an assigned 0 while there is no possible ambiguity with a null (a null id always means a new entity), which is why I prefer to use a nullable wrapper type.

这是 Hibernate 的建议.来自参考文档:

And this is the Hibernate recommendation. From the Reference Documentation:

Cat 有一个名为 id 的属性.这个属性映射到主键数据库表的列.这属性可能已被调用任何东西,它的类型可能是任何原始类型,任何原始类型包装器"类型,java.lang.String 或java.util.Date.如果你的遗产数据库表有复合键,你可以使用用户定义的类这些类型的属性(请参阅稍后关于复合标识符的部分在章节中.)

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.)

标识符属性是严格的可选的.你可以离开它们让 Hibernate 跟踪对象内部标识符.我们不不过推荐这个.

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:

  • 分离对象的传递重新附加(级联更新或级联合并) - 见第 10.11 节,传递持久性"
  • Session.saveOrUpdate()
  • Session.merge()

我们建议您声明一致命名的标识符持久类的属性和您使用可空值(即非原始)类型.

We recommend that you declare consistently-named identifier properties on persistent classes and that you use a nullable (i.e., non-primitive) type.

我实际上在我的基类中利用了这一点:

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);
    }
}

这篇关于休眠主键的原语或包装器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆