JPA entity id - long or Long
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.
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.
Neither. Numeric types in Java are intended for performing mathematical computations. Use a String type for your entity id instead. If using a guid/uuid generator for id, you'll likely need to allow for non-digit characters anyway (e.g. hyphens).