Rachel
Rachel

Reputation: 103397

How to map boolean in hibernate hbm file

In my database, i have column like

"ISDEFAULTPAYMENTFORCURRENCY" CHAR(1 BYTE) NOT NULL ENABLE,
CHECK (ISDEFAULTPAYMENTFORCURRENCY IN ('N','Y')) ENABLE,

In my bean, i have

    private Boolean isDefaultPaymentForCurrency;

my question, how can i map this isDefaultPaymentForCurrency in hibernate hbm file?

<property name="isDefaultPaymentForCurrency" type="???" column="ISDEFAULTPAYMENTFORCURRENCY" not-null="true"/>

Upvotes: 7

Views: 10186

Answers (4)

AechoLiu
AechoLiu

Reputation: 18368

I refer the link, Hibernate Mapping Types, and use boolean finally.

A hbm.xml example.

    <property name="scheduable" type="boolean" >
        <column name="SCHEDUABLE" not-null="true" default="false" />
    </property>

The mapping types image

Upvotes: 3

cvsr.sarma
cvsr.sarma

Reputation: 909

Type mappings from Java primitives or wrapper classes to appropriate (vendor-specific) SQL column types. boolean, yes_no, and true_false are all alternative encodings for a Java boolean or java.lang.Boolean.

http://docs.jboss.org/hibernate/stable/core.old/reference/en/html/mapping-types.html

Upvotes: 3

nachokk
nachokk

Reputation: 14413

Use

<property name="isDefaultPaymentForCurrency"  type="yes_no" column="ISDEFAULTPAYMENTFORCURRENCY" not-null="true"/>

And to use hql you can set this property in hibernate.cfg

<property name="hibernate.query.substitutions">true 'Y', false 'N'</property>

Upvotes: 3

blackpanther
blackpanther

Reputation: 11486

You may want to try the following type: org.hibernate.type.YesNoType. The following reference may be used for this (in particular section 6.1.1.3). Registered as yes_no

Upvotes: 0

Related Questions