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

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.

Leave a Comment