3
votes

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

4
You are going about this incorrrctly. Do you want a raw query? Do you want to return an entity or just a list of objects? There are too many problems to motivate me to give an answer, but have a look at a tutorial like this one.Tim Biegeleisen
I don't think there's anything wrong in accepting a single entity returned by the raw query wrapped in a listnikel
We can do it even if TestBean is not related to a table - checkout :Transformers.aliasToBean" methodnikel

4 Answers

0
votes

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

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);
0
votes

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.

0
votes

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