osh osh - 1 year ago 82
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

). 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
? 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
and unsigned integer so i guess what i was thinking was "unsigned integer versus
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 Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download