Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 17:34 Go to next message
John Worley is currently offline John WorleyFriend
Messages: 2
Registered: July 2009
Junior Member
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 14:36 Go to previous messageGo to next message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

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.


James : Wiki : Book : Blog : Twitter
Re: Mapping Oracle NUMBER type to Java Type Options [message #1065447 is a reply to message #1065395] Tue, 25 June 2013 18:12 Go to previous messageGo to next message
John Worley is currently offline John WorleyFriend
Messages: 2
Registered: July 2009
Junior Member
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 13:31 Go to previous message
James Sutherland is currently offline James SutherlandFriend
Messages: 1939
Registered: July 2009
Location: Ottawa, Canada
Senior Member

Using a long is normally more convenient in Java, but either is fine, whichever best suites your application.


James : Wiki : Book : Blog : Twitter
Previous Topic:Opened EntityManager
Next Topic:ElementCollection Map and CriteriaQuery
Goto Forum:
  


Current Time: Mon Nov 24 04:23:40 GMT 2014

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

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