Entity members should they be primitive data types or java data types?

I would usually suggest using the primitive types, just to get rid of null checks all over the place. But it really depends on what you want to say. Your Boolean now can hold 3 values:

  1. true
  2. false
  3. null

And null can make a total new semantic when dealing with entities. I usually use it as “No data available”. Your “enabled” might be a bad example for this kind of field. But lets say you have a number which holds the age of a person.

private Integer age;

When you use null, you can treat this as: “Unknown”. You could also use an int and define a special value (-1) for this case, but null is the more natural solution.

So, to sum it up. Use primitives if there is always a meaningful value (required fields?) and wrapper classes for optional values.

Leave a Comment