Should the ID of your Entity be long (primitive type) or Long (object type)?
What to choose? long or Long?
@Entity
@Table(name = "COUNTRY")
public class CountryEntity implements java.io.Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "ID")
private long id;
@Column(name = "NAME")
private String name;
@Column(name = "CURRENCY")
private String currency;
@Column(name = "PEOPLE")
private Long people;
@Column(name = "SIZE")
private Long size;
public CountryEntity() {
}
I consider having Long is better, as is more correct to check whether an entity has persistent identity by checking against the null
value (in MySQL you can have an ID of value 0). Also some libraries like Spring base on an ID of type Long (by default) in their logic. See this implementation for an example.
The small advantage of the primitive: it takes a bit less space.
PS:Both are correct & supported according to the JPA specification and the answers to this question are somehow opinion-based.