Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what data type should I use for Oracle Numeric and Date

 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When creating mapping class for the following oracle table

Treasure (
id NUMBER
price NUMBER(10,3)
quantity NUMBER(6,0)
name varchar2(30)
purchase_date DATE
)

should I use

class Treasure (
Long id;
double price;
int quantity;
String name;
Date pDate;
}

Question --

1. For id, can I use either Integer or Long in the class ? any restriction ?
2. For oracle DATE, shall I use java.sql.Date or java.util.Calendar ?
3. if it is a NUMBER with floating digits, shall I use float or double in class ?
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see what ORM technology you are using. Usually, there is some
reverse engineering tool that can help you generate classes and mappings
from a database schema.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I didn't specify it clearly. I am using hibernate and oracle. So far I found that, if I define a "NUMBER" data type in orcle, then in my POJO class, I can NOT use primitive data type (like int) and I have to use wrapper type (like INTEGER). Is it true, and why ?

secondly, if oracle use "DATE" type, then what should I use in the POJO class ?
 
Edvins Reisons
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My point was that the "what maps to what" type questions are best "answered" by a tool.
Hibernate Tools include reverse engineering which can generate classes and mappings from a database definition.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

So far I found that, if I define a "NUMBER" data type in orcle, then in my POJO class, I can NOT use primitive data type (like int) and I have to use wrapper type (like INTEGER). Is it true, and why ?

The only reason would be that primitive types don't support null values. Is your field nullable? Otherwise primitives should be fine.


secondly, if oracle use "DATE" type, then what should I use in the POJO class ?

A java.util.Date should be fine.

The only decision you have with your number fields are based on the precision and scale. Off the top of my head I don't know for sure what the default precision is for a number data type (read Oracle's documentation), but I think its nine. So the only safe thing to put in there would be a Short (or short, if your field is not nullable). I'd recommend you always define the precision of your fields - a number(10) should be able to store the maximum range of value an Integer can produce.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:

A java.util.Date should be fine.

The only decision you have with your number fields are based on the precision and scale. Off the top of my head I don't know for sure what the default precision is for a number data type (read Oracle's documentation), but I think its nine. So the only safe thing to put in there would be a Short (or short, if your field is not nullable). I'd recommend you always define the precision of your fields - a number(10) should be able to store the maximum range of value an Integer can produce.



Paul, you are right. I found that if a column is nullable, then it can only use wrapper class (like Integer); if it is not nullable it takes "int" primitive type. So, is there any way to get it around ? Thanks in advance !
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you are asking here. Is using an Integer a problem? Given your data model has a nullable column, it sounds like your domain model should use a nullable type.
 
ben oliver
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Sturrock:
Not sure what you are asking here. Is using an Integer a problem? Given your data model has a nullable column, it sounds like your domain model should use a nullable type.


But, I also found that for the PK studentID that I specified in xml

<id name="studentId" column="STUDENT_ID" type="integer">
</id>

I can define its type as "int" in my POJO like

private int studentID;

public int getStudentID() {...}

EVEN THOUGH this "STUDENT_ID" column is NOT nullable in database.

So, it seems there is a difference between an "id" attribute and non-id type attribute, in terms of whether it can use a primitive ??
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Primary keys can never be nullable. If they are, they are not primary keys.


I can define its type as "int" in my POJO like

private int studentID;

public int getStudentID() {...}

EVEN THOUGH this "STUDENT_ID" column is NOT nullable in database.


This is what I'd expect. Re-read my posts; the problem with using primitive types is when you use them with nullable columns. Your example is using them with a columm that can't be null.
 
Walter Bernstein
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

1. For id, can I use either Integer or Long in the class ? any restriction ?
2. For oracle DATE, shall I use java.sql.Date or java.util.Calendar ?
3. if it is a NUMBER with floating digits, shall I use float or double in class ?

1) Long
2) java.util.Date/Timestamp (date/time).
3) Double. Is there a float in Java?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic