Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boxed vs primitive type as entity id

In JPA (Hibernate implementation) which type is better to use for id of the entity: Boxed type (e.g. Integer) or Unboxed type (e.g. int)?

A friend said that you should use Boxed types because when you create a new entity in your program, Hibernate sees that the id is null and understands that it should create a new row in database (In contrast if id is not null Hibernate may update an existing row in databse).

But the id of my entities was int and it worked well without any error and we know that the default value of primitive instance variables is 0. So he said that maybe hibernate treats 0 as special and assumes that the object is a new one.

like image 628
Mahozad Avatar asked Sep 09 '25 19:09

Mahozad


1 Answers

Seems Current Documentation recommends to use Boxed Type.

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

like image 156
gtgaxiola Avatar answered Sep 12 '25 10:09

gtgaxiola