Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure hibernate (using JPA) to store Y/N for type Boolean instead of 0/1

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.

like image 247
sengs Avatar asked Jul 20 '09 17:07

sengs


People also ask

What is @ID annotation in JPA?

In JPA the object id is defined through the @Id annotation and should correspond to the primary key of the object's table. An object id can either be a natural id or a generated id. A natural id is one that occurs in the object and has some meaning in the application.

Is @column mandatory in JPA?

@Column. Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.


1 Answers

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(); 
like image 91
ChssPly76 Avatar answered Oct 13 '22 04:10

ChssPly76