JPA entity id - primitive or object type (long or Long)

Viewed 30308

Should the ID of your Entity be long (primitive type) or Long (object type)?

  • The ID is the primary key of my table and is never 'null' in the database.
  • My co-workers suggest to use an Object Type Long.
  • Hibernate Reverse Engineering tool generates a primitive type long for id by default.

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() {
    }
3 Answers

Use a String type for your entity id. Numeric types are intended for sequences or performing mathematical comparison and computations. If using a guid/uuid generator for id, you'll likely need to allow for non-numeric characters anyway (e.g. hyphens).

Related