Boolean value not getting mapped properly in Hibernate with MySQL

拈花ヽ惹草 提交于 2020-01-04 03:52:10

问题


I am trying to map the result of an exists query (which returns TRUE/FALSE) from a MySQL database to a POJO via resultSetTransformer. I would hope the result of this exists query can get mapped to a boolean but it does not and throws the below error:

org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of TestBean.value

The cause of this exception is shown as:

java.lang.IllegalArgumentException: argument type mismatch


My sample class:

public class TestHibernate {

    private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();

    public static void main(String[] args) throws ParseException {
        try {
            Query query = sessionFactory.openSession().createSQLQuery(
                "SELECT " +
                "EXISTS (SELECT * FROM A WHERE id = 3) AS value"
            );

            query.setResultTransformer(Transformers.aliasToBean(TestBean.class));
            List<TestBean> beanList = (List<TestBean>) query.list();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}

The POJO:

public class TestBean {

    private boolean value;

    public boolean isValue() {
        return value;
    }

    public void setValue(boolean value) {
        this.value = value;
    }

}

Am I missing something or is it a bug with Hibernate or MySQL JDBC Driver?

Hibernate version: 3.2.6GA
MySQL JDBC Driver: mysql-connector-java-5.1.2


回答1:


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();



回答2:


Your problem may be caused by the case mapping of the selected column, q.v. my solution below. I also parametrized your WHERE clause to avoid SQL injection.

Query query = session.createSQLQuery("select exists(select * from A where id = :id) as value");
    .setParameter("id", "3");
    .addScalar("value")
    .setResultTransformer( Transformers.aliasToBean(TestBean.class))

List result = query.list();
TestBean theBean = (TestBean)result.get(0);



回答3:


The transform of the result query can be explicitly set each parameter to the corresponding model datatype using hibernate addScalar() method. Please find the solution below.

    Query query = sessionFactory.openSession().createSQLQuery(""+
                "select " +
                " exists(select * from A where id = 3) as value"
                ).addScalar("value",  BooleanType.INSTANCE);

This will resolve to set to the Boolean value.




回答4:


I know this is old answer, I tried to resolve this coz answer from here not worked for me.

with Addition to Answer from @anil bk, I overloaded a setter method accepting String as argument. Now It worked as expected.

public void setPriority(String priority) {
    this.priority = "true".equals(priority);
}

Here is my answer



来源:https://stackoverflow.com/questions/36994153/boolean-value-not-getting-mapped-properly-in-hibernate-with-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!