wood burning stoves 2.0*
The moose likes Object Relational Mapping and the fly likes Do not use primitive types in JPA for mapping Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Do not use primitive types in JPA for mapping" Watch "Do not use primitive types in JPA for mapping" New topic
Author

Do not use primitive types in JPA for mapping

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Jul 20, 2005
Posts: 9941
    
159

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?


[My Blog] [JavaRanch Journal]
Hauke Ingmar Schmidt
Rancher

Joined: Nov 18, 2008
Posts: 433
    
    2
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

Joined: Jun 28, 2008
Posts: 580
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

Joined: Jan 16, 2003
Posts: 75
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.


Ran
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do not use primitive types in JPA for mapping
 
Similar Threads
Hibernate Identifier and primitive types
Primitive Type/Wrapper Type
when to use ...implements Serializable
when to use property or parameter?
Hibernate Annotation Yes/No