Kevin
Kevin

Reputation: 3509

Data Access Object Primitive Type vs Object type?

Would like opinions on the best way to go.

As you can see int cannot be null. Where as the object of Integer can.

Problem: Database values with a column that is number can be null or can contain a number. When the database passes the value of null, then we receive and exception stating that "primitive values cannot be null"

But when we use Integer class, we are creating that object (which of course is bigger/bulkier than a primitive type int)

So that brings up to me a couple of choices.

  1. Use Integer type.
  2. Set Database column to "default"
  3. Set int to default if there is something different in the database, then accept that

Any other suggestions?

Upvotes: 1

Views: 927

Answers (3)

java_mouse
java_mouse

Reputation: 2109

My choices:

First- If it is possible and if it is logically ok to set a default in database (might be a FK to a ref table which has all values + a default).

Second - If first is not possible, I would use Integer objects without any reservation. I don't think you will have perf issues. The code will be clean but document in the variable that "can contain null and check for null while working on this variable".

I would always go for something which is more readable and understandable to keep the software maintainable and flexible.

Upvotes: 2

korifey
korifey

Reputation: 3509

I don't think you must worry about Integer <-> int converting performance (100000000 opsec) if you query database (5000 op/sec). Use boxed types courageously.

Upvotes: 3

cdeszaq
cdeszaq

Reputation: 31280

Use Hibernate (or similar ORM) and let the framework deal with the database directly. Then you can program it how you like, and not have to deal with converting.

Reinventing the wheel seldom works as well as just using someone else's wheel in the first place, especially when thousands of others already use the same wheel.

Upvotes: 2

Related Questions