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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With