osh osh - 1 month ago 11
Java Question

Long versus Integer for the id field of java entities

Jpa entity generator assigns Integer type to the id fields of my entities. The corresponding attribute/column in my DB is of type serial ( yep

postgres
). I would also assign the integer type to my entities id fields. But i have seen usage of
Long getId()
on this page. I have also seen this type of type assigning on the geomajas examples. Is there any gotcha when it comes to using
Integer
? I mean, yeah you have to be careful with integer that the id is not below 0 but at the same time you also have to make sure that your Long Id is not bigger than 2,147,483,647. So what's deal here?

EDIT: I was making a confusion between
Long
and unsigned integer so i guess what i was thinking was "unsigned integer versus
Integer
for the id field of java entities" which is nonsense now that my confusion between long and unsigned integer is gone. My bad. Thank you for your answers and comments. I guess if i would have used bigserial jpa entity generator would have used Long too.

Answer

Long has a much bigger capacity than Integer data type, so if you are not sure what length your data is going to be, its better to be using Long type data...

On the other hand, since Long has a bigger capacity it uses extra space and if you are sure that your data will not be bigger than the Integer data type then using Long just makes your code inefficient