简体   繁体   中英

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?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM