• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do not use primitive types in JPA for mapping

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can a expert please verify this statement:

I use JPA 2.0.

Statement 1:
You should always use primitive types for your entity instead of their wrapper-types. Primitiv types are more performant.

Statement 2:
You should always use wrapper types for your entity instead of their primitive-types, because Wrapper-Types can have NULL-Value which can be the representation of a NULL-Value of a column in a database. Hence: Use Wrapper-Types in Entity-Beans.
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nimo frey wrote:

Statement 1:
You should always use primitive types for your entity instead of their wrapper-types. Primitiv types are more performant.

How so?

 
Hauke Ingmar Schmidt
Rancher
Posts: 436
2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a performance problem when accessing the field? If so, you need to optimize. ("Premature optimisation is the root of all evil.") But expressing "special" values like "not initalised" or "infinity" with values like -1 or 9999 will lead to problems.

If not, follow the business. The field can be null? Can't be achieved with primitives, so don't use them then. Typical for primary keys.
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have a performance problem when accessing the field


No not really

"Premature optimisation is the root of all evil."


Yes, indeed its true.

I am convinced now - I use Wrapper-Types. I saw in such tutorials that they use primitives and could not find a reasonable reason.
 
Ran Pleasant
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always use primitives for non-nullable fields and wrapper objects for nullable fields. If you use a primitive on a nullable field JPA (Hibernate in JBoss) will throw an error when it reads a null value from the database and attempts to put that value into a primitive field.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic