Skip to content
Advertisement

How to use money data type in Java, SQL, ORM

What are best practises in using money data type in Java application? Money should be in double variable? What about rounding, currencies and so on. Are special libraries for this? And what about ORM and SQL in most popular databases. As I know not in all SQL engines is Money data type. In that case NUMERIC(15,2), DECIMAL(15,2) or REAL data type should be use?

Advertisement

Answer

What are best practises in using money data type in Java application?

Use BigDecimal. Using any primitive will lead to precision problems sooner or later.

And what about ORM and SQL in most popular databases.

Hibernate (and probably all others) can handle BigDecimal just fine. And it translates to the appropriate database type, which is usually DECIMAL.

Advertisement