Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primitive or wrapper for hibernate primary keys

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 ?

like image 576
Anonym Avatar asked Aug 21 '10 00:08

Anonym


People also ask

Should I use primitive or wrapper?

Verdict. Generally, choose primitive types over wrapper classes unless using a wrapper class is necessary. Primitive Types will never be slower than Wrapper Objects, however Wrapper Objects have the advantage of being able to be null.

How does Hibernate generate primary key?

IDENTITY: Hibernate relies on an auto-incremented database column to generate the primary key, SEQUENCE: Hibernate requests the primary key value from a database sequence, TABLE: Hibernate uses a database table to simulate a sequence.

Does Hibernate require primary key?

Hibernate requires that entity tables have primary keys. End of story. 50k records is simply not that many when you're talking about a database.

What is the difference between wrapper and primitive?

Wrapper class creates an object and primitive does not create object. Wrapper classes are used with Collections to represent type. Wrappers have methods and can hold memory address/null and primitives hold default values. Primitives are fast compare to wrapper classes as there is no overhead of methods or object.


1 Answers

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);     } } 
like image 80
Pascal Thivent Avatar answered Sep 19 '22 16:09

Pascal Thivent