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