From the standpoint of sleep mode, this does not change anything, since Hibernate uses the same type of Hibernate to represent them.
However, as Bytecode Ninja points out, you cannot distinguish the default value of a primitive int 0 from the assigned 0, while there is no ambiguity with zero (a null identifier always means a new object), so I prefer to use a nullable shell type.
And this is the recommendation of Hibernate. From the reference documentation:
4.1.2. Specify an identifier property (optional)
Cat has a property called id. This property maps to the primary key column of the database table. A property could be called by anything, and its type could be any primitive type, any primitive wrapper type, java.lang.String or java.util.Date. If the legacy database table contains compound keys, you can use a custom class with the properties of these types (see the section on compound identifiers later in the chapter.)
The identifier property is strictly optional. You can leave them and let Hibernate track object identifiers within the company. However, we do not recommend this.
In fact, some functions are only available for classes that declare an identifier property:
Transitive re-binding for individual objects (cascading updates or merging cascades) - see section 10.11 "Transitive persistence" Session.saveOrUpdate () Session.merge () We recommend that you declare identifier properties with the same name in constant classes and use a type with a null value (i.e. Not primitive).
And I really use 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); } }
Please check more details here: https://stackoverflow.com/posts/3537407/edit