Skip to main content



      Home
Home » Eclipse Projects » EclipseLink » Mapping Oracle NUMBER type to Java Type Options(Can Java Integer be used for Oracle NUMBER instead of the default Java BigDecimal)
Mapping Oracle NUMBER type to Java Type Options [message #1064735] Thu, 20 June 2013 13:34 Go to next message
Eclipse UserFriend
I have a project where Oracle is the database and ID columns are type NUMBER(32). The JPA entity classes are using Java BigDecimal as the field mappings to those Oracle ID columns. That works of course but since the purpose of the database column is to just be an integer value, I was wondering if it would be more efficient to use Java Integer.

In testing this I made some JPA entity classes that used Java Integer for the NUMBER(32)columns. That works with regular queries and native queries for data.

I run into a problem when I try a single column fetch using a native query though.
For example - SELECT someIdColumn FROM someTable
...where I try to map the results into a List<Integer> or List<BigInteger>

In that test I get an exception that says I can't cast BigDecimal to Integer. So the EntityManager is automatically mapping the Oracle NUMBER(32) to a BigDecimal. Is there a way I can tell it to use something else instead of BigDecimal?
Re: Mapping Oracle NUMBER type to Java Type Options [message #1065395 is a reply to message #1064735] Tue, 25 June 2013 10:36 Go to previous messageGo to next message
Eclipse UserFriend
NUMBER is return as BigDecimal, so this is what a native query will return. If you want your object type, you need to use JPQL, not a native query.

You could also cast to List<Number> and use number.getInt().

In general NUMBER(32) is bigger than a Java int, so you should at least be using a long.
Re: Mapping Oracle NUMBER type to Java Type Options [message #1065447 is a reply to message #1065395] Tue, 25 June 2013 14:12 Go to previous messageGo to next message
Eclipse UserFriend
I see. Thanks for the explanation. I appreciate it.

Is it better then, in the case of having Oracle as the database, to just use BigDecimal as the mapping for NUMBER columns being used as the record primary key? Or would it be more efficient (in terms of memory usage) to use a Long (or even a BigInteger)?
Re: Mapping Oracle NUMBER type to Java Type Options [message #1066621 is a reply to message #1065447] Wed, 03 July 2013 09:31 Go to previous message
Eclipse UserFriend
Using a long is normally more convenient in Java, but either is fine, whichever best suites your application.
Previous Topic:Opened EntityManager
Next Topic:ElementCollection Map and CriteriaQuery
Goto Forum:
  


Current Time: Fri Jul 25 20:54:28 EDT 2025

Powered by FUDForum. Page generated in 0.03085 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top