Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA entity id - long or Long

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() {     } 
like image 918
Dimitri Dewaele Avatar asked Dec 06 '13 10:12

Dimitri Dewaele


People also ask

Can we create JPA entity without ID?

If your object does not have an id, but its' table does, this is fine. Make the object an Embeddable object, embeddable objects do not have ids. You will need a Entity that contains this Embeddable to persist and query it.

Is @ID mandatory in JPA?

Id is required by JPA, but it is not required that the Id specified in your mapping match the Id in your database. For instance you can map a table with no id to a jpa entity. To do it just specify that the "Jpa Id" is the combination of all columns.

What does @ID do in JPA?

Simple Identifiers. The most straightforward way to define an identifier is by using the @Id annotation. Simple ids are mapped using @Id to a single property of one of these types: Java primitive and primitive wrapper types, String, Date, BigDecimal and BigInteger.

How do you indicate a JPA entity?

The Id Annotation. Each JPA entity must have a primary key that uniquely identifies it. The @Id annotation defines the primary key. We can generate the identifiers in different ways, which are specified by the @GeneratedValue annotation.

How to generate a JPA entity’s ID?

Each JPA entity must have a primary key which uniquely identifies it. The @Id annotation defines the primary key. We can generate the identifiers in different ways which are specified by the @GeneratedValue annotation. We can choose from four id generation strategies with the strategy element. The value can be AUTO, TABLE, SEQUENCE, or IDENTITY.

What are the different types of JPA identifiers?

The value can be AUTO, TABLE, SEQUENCE, or IDENTITY. If we specify GenerationType. AUTO, the JPA provider will use any strategy it wants to generate the identifiers.

Why JPA entity classes should not be final?

Because various JPA implementations will try subclassing our entity in order to provide their functionality, entity classes must not be declared final. 2.2. The Id Annotation Each JPA entity must have a primary key which uniquely identifies it.

What are the state transitions in JPA?

Entity Instance states JPA defines four states and state transitions for the persistence life cycle. Following image illustrates the overview of JPA entity lifecycle state transitions. 2. Transient State : When an entity object is initially created, it’s state is New or Transient.


2 Answers

I consider having Long is better, as it 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(d) 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.

like image 154
V G Avatar answered Oct 10 '22 19:10

V G


I'd prefer Long, for the simple reason that if you let the database generate ids for you (which you should), you can tell that a recently instantiated CountryEntity object is not yet persisted by checking for id==null. If you use long, then id will always have a non-null value (initially 0), which will change upon persisting the entity.

like image 33
wallenborn Avatar answered Oct 10 '22 19:10

wallenborn