2

I have a JPA select:

select c.name, f.id from Child c left join c.father

The expected result is:

Child 1  |  1
Child 2  |  2
Orphan   | null

But I caught

Child 1  |  1
Child 2  |  2
Orphan   |  0  // ZERO ?

I was able to workaround with this select

select c.name, case when (f.id is null) then null else f.id end from Child c left join c.father

There is some setting that I can set to stay way from the workaround?

Beto Neto
  • 3,962
  • 7
  • 47
  • 81

1 Answers1

0

As mentioned in the comments, the problem is the use of primitives. Primitives can only store a value set by default to 'some value.' In the case of int this is a 0 (long would be 0, boolean would be false and so on).

In order to represent a database column being null, the entity in question should use an Integer object. This allows null to be set, and values can also be assigned.

It should be an easy enough change to the entity- make the definition Integer id; (or Long if large values can be used in the id) and update the accessors.

Flare Star
  • 176
  • 1
  • 8