Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of primary key for Long data type in Java (and Hibernate)?

log(2^32) / log(10) =~ 9.63295986126
log(10^0.63295986126) / log(2) =~ 2.10264714605 > 2 bit

log(2^64) / log(10) =~  19.2659197225
log(10^0.2659197225) / log(2) =~ 0.883366197155 < 2 bit

As you can see 9 digits for Integer doesn't lead to negative values in Integer type.

But 19 digits in Long can cause sign overflow... I usually see NUMBER(18) as type of ID column...

Is it possible to have problem with mapping NUMBER(19) to Long in Hibernate?

like image 887
gavenkoa Avatar asked Nov 03 '22 03:11

gavenkoa


1 Answers

Do not use Long - it's intended for numbers. You never multiply, add, subtract PKs. (you probably even never sort them). For Oracle datatype NUMBER use either oracle.sql.NUMBER or java.math.BigDecimal.

like image 167
ibre5041 Avatar answered Nov 14 '22 22:11

ibre5041