Monday, February 14, 2011

JPA mapping boolean to CHAR(1) yes_no

After having googled around and found this and this, I have decided for the simplest possible solution: use a char in the Entity and expose it with a boolean getter/setter - anyway JPA uses "field" access to the Entity, so she is not disturbed by my setters and getters.

private char sysparam = 'N';

public void setSysparam(boolean sysparam) {
 this.sysparam = sysparam ? 'Y' : 'N';
}

public boolean getSysparam() {
 return sysparam == 'Y';
}



Incidentally all my boolean fields are NOT NULL, so I use directly a boolean rather than a Boolean.

It's a shame that JPA doesn't cover something so basic. Everybody knows that Oracle doesn't support a BOOLEAN data type; so I expect everyone is having the same problem.

1 comment:

Unknown said...

Thanks for the simple example. I'm encountering the same problem.