views:

2927

answers:

2

Can I setup JPA/hibernate to persist Boolean types as 'Y'/'N' in the database (the column is defined as varchar2(1)). It currently stores them as '0'/'1'. The database is Oracle.

A: 

The only way I've figured out how to do this is to have two properties for my class. One as the boolean for the programming API which is not included in the mapping. It's getter and setter reference a private char variable which is Y/N. I then have another protected property which is included in the hibernate mapping and it's getters and setters reference the private char variable directly.

Spencer Ruport
I had to do something similar - I changed the type of the member from Boolean to String. In the getters and setters (which got and set Boolean) I wrote code to convert Y/N to the corresponding Boolean value.
askullhead
+9  A: 

Hibernate has a built-in "yes_no" type that would do what you want. It maps to a CHAR(1) column in the database.

Basic mapping: <property name="some_flag" type="yes_no"/>

Annotation mapping (Hibernate extensions):

@Type(type="yes_no")
public boolean getFlag();
ChssPly76
For those who are interested, there is also a "true_false" type that will store either "T" or "F".
Matt Solnit
This worked, but I could not use it because it's a hibernate specific annotation. Thanks for the answer. Might use it in a different project.
askullhead