Grant Zhu
Grant Zhu

Reputation: 3018

Use long or Long to map a "NUMBER" column in oracle?

I have a database column whose type is "NUMBER" in oracle.

I want to map it by long or Long. But I'm not sure which type I should use, the primitive type or the Object type? Is there a convention? My case is that the value is the only thing I want from DAO method:

public class SampleDAO{
  public long fetchNumberValue(){
    //Is it better to return long value instead of Long?
  }
}

And what if I want to wrap that value in an object?

public class SampleObject(){
  //Is it better to use Long to use methods like hashCode() inherited from Object?
  private Long value;       
}

EDIT:
Thanks all for the answers. I got it that it depends on whether I need accept null value.

This database column is not nullable, actually it's the primary key. And it may have such values like 0, 1, 2 etc... OK, another question appears, how can I know if I get a matched row with a 0 value or no matched row? java.sql.ResultSet.getLong returns a long value and if the column's value is SQL NULL then 0 is returned. Or is it a bad practice to have a 0 valid in such a column?

Upvotes: 0

Views: 1137

Answers (4)

ziesemer
ziesemer

Reputation: 28697

Long will permit a null value, where as long must not be null. So I would match it do your database column - is it nullable or not?

Edit to the edited question:

To check if a value was actually 0 or null in the database, use ResultSet.wasNull.

Upvotes: 3

Carlos Gavidia-Calderon
Carlos Gavidia-Calderon

Reputation: 7243

You should consider also that Long objects consume more memory -not sure in what amount- than long primitives. If the Nullable attribute is not a problem, you should use primitive data types.

Upvotes: 0

user1131435
user1131435

Reputation:

Usually, you want to use the primitive type, and not the object, if the only thing you want is the value, and that value won't be null. For all your conversion needs, you have the class (such as String.parse).

Upvotes: 1

Thilo
Thilo

Reputation: 262684

It comes down to if you want to accept nulls or not.

Upvotes: 1

Related Questions