Hibernate SQL query does not correctly map Boolean value?

Run the following through any SQL client

SELECT FALSE, TRUE;

return

p>

f, t

But this SQL query:

SQLQuery qry = session.createSQLQuery("SELECT FALSE, TRUE;");
Object[] result = (Object[]) qry.uniqueResult();

Returns an Object array containing two Boolean objects:

< p>

[false, false]

I am using postgres 9.0-801.jdbc4 and hibernate.

How to return the corresponding boolean value through hibernate sql query ?

Try to change the definition of the boolean attribute in the mapping file:

Or if you use comments to add this:

< p>

@Type(type="true_false")

This should help, because by default hibernate maps boolean properties to BIT instead of CHAR(1), I think This is why you have such a problem. (look at hibernate mapping types).

Edit:

If you need to use SQLQuery to select boolean values ​​(as separated values ​​instead of entity fields) , You can use this method:

SQLQuery q = session.createSQLQuery("SELECT FALSE a, TRUE b;");
q.addScalar("a ", new TrueFalseType());
q.addScalar("b", new TrueFalseType());

Object[] result = (Object[]) q.uniqueResult();

As you can see in this case, you need to clearly define the type of the fetched value so that hibernate can convert it correctly. I tested this method in MySQL and everything works fine (except in my In this case, I specified the type BooleanType() because MySQL uses Boolean values ​​as 1/0).

Hibernate uses three Boolean formats:

1) “Y / N” – > CHAR(1)ansi sql type -> “yes_no” hibernate type -> YesNoType class;

2) “T/F” -> CHAR(1) -> “true_false” hibernate type -> TrueFalseType Class;

3) “1/0” -> BIT ansi sql type -> “boolean” hibernate type -> BooleanType class;

So you need to choose the right type for your database (as I understand it, and in my example you should use TrueFalseType).

Run the following through any SQL client

SELECT FALSE, TRUE;

report

f, t

But this SQL query:

SQLQuery qry = session.createSQLQuery("SELECT FALSE, TRUE;");
Object[] result = (Object[]) qry.uniqueResult();

Returns an Object containing two Boolean objects Array:

[false, false]

I am using postgres 9.0-801.jdbc4 and hibernate.

How to pass hibernate sql query returns the corresponding boolean value?

Try to change the definition of the boolean attribute in the mapping file:

Or if you add this using comments:

@Type( type="true_false")

This should help, because by default hibernate maps boolean properties to BIT instead of CHAR(1), I think this is why you have such a problem. (Look at hibernate mapping types).

Edit:

If you need to use SQLQuery to select boolean values ​​(as separated values ​​instead of entity fields), you can use this method:

SQLQuery q = session.createSQLQuery("SELECT FALSE a, TRUE b;");
q.addScalar("a", new TrueFalseType());
q.addScalar("b", new TrueFalseType());

Object[] result = (Object[]) q.uniqueResult();

As you have in this case As you can see, you need to clearly define the type of the fetched value so that hibernate can convert it correctly. I tested this method in MySQL and everything works fine (except in my case I specified the type BooleanType(), because MySQL Use boolean value as 1/0).

Hibernate uses three boolean formats:

1) “Y / N” -> CHAR(1)ansi sql type -> “yes_no “Hibernate type -> YesNoType class;

2) “T/F” -> CHAR(1) -> “true_false” hibernate type -> TrueFalseType class;

3)” 1/0″ -> BIT ansi sql type -> “boolean” hibernate type -> BooleanType class;

So you need Choose the right type for your database (as I understand it, and in my example you should use TrueFalseType).

Leave a Comment

Your email address will not be published.