基本或休眠主键的包装 [英] Primitive or wrapper for hibernate primary keys

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

问题描述

我一直在研究各种hibernate教程和示例,它们的身份/主键属性,有些使用Java基本类型,有些使用包装类型,即;

  private int id; 

vs

 私人整数ID; 

为什么和什么时候我会用另一个为实体键?

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

$ b 解析方案但是,正如Bytecode Ninja所指出的那样,您无法将基本int 0 的默认值从指定的 0 ,而 null (a null ID总是意​​味着一个新的实体),这就是为什么我更喜欢使用可空的包装类型。

这是Hibernate的建议。从参考文档:


4.1.2。提供一个标识符属性(可选)



Cat有一个名为id的属性。此
属性映射到数据库表的主键
列。
属性可能已被称为
任何东西,其类型可能是
任何原始类型,任何原语
包装器类型,java.lang.String或
java.util.Date。如果您的遗留
数据库表具有组合键,那么
可以使用具有这些类型的
属性的用户定义类(请参阅后面
组合标识符上的
部分)



标识符属性严格为
可选。您可以将它们关闭,
让Hibernate在内部跟踪对象
标识符。但是,我们不推荐


实际上,一些功能是
,仅适用于声明
是标识符属性的类:


  • 分离对象的传递重新连接(级联更新或级联
    合并) - 请参见第10.11节
    传递

  • Session.saveOrUpdate()

  • Session.merge()


我们建议在持久性类和
上声明
一致命名标识符
属性,并使用可空(即,
non-primitive)类型。


我在我的基类中实际使用了这个:

  @MappedSuperclass 
公共类BaseEntity实现Serializable {
private static final long serialVersionUID = 1L;
私人长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);
}
}


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; 

vs

 private Integer id;

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

解决方案

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

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.

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

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 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天全站免登陆