Jigar Naik
Jigar Naik

Reputation: 1994

In JPA how can i bind Y N to Boolean type

Is it possible to bind Boolean variable to VARCHAR(1) Y/N in MySQL using JPA annotations ?

I am using below mapping@Basic @Column(name = "IS_ACTIVE", columnDefinition = "VARCHAR", length = 1) private Boolean active;

which is throwing exception

java.lang.IllegalArgumentException: Parameter value [A] did not match expected type [java.lang.Boolean (n/a)]

so I figured out the reason for error, it was because I was passing 'Y' as a parameter to named query.

params.put("isActive", "Y");

but now when I changed it to

params.put("isActive", Boolean.TRUE);

the query is not returning any result it seems it's passing 'true' as a parameter to the query.

Can somebody tell me how to correctly map this ?

<dependency>
        <groupId>org.jboss.spec</groupId>
        <artifactId>jboss-javaee-all-6.0</artifactId>
        <version>3.0.0.Final</version>
        <scope>provided</scope>
    </dependency>

Upvotes: 1

Views: 1777

Answers (1)

Neil Stockton
Neil Stockton

Reputation: 11531

Safest way is to define an AttributeConverter that maps Boolean to String and that maps to "Y" or "N". Then define your Boolean field to use that AttributeConverter.

Upvotes: 2

Related Questions