布尔值不能在Hibernate中通过MySql正确映射 [英] boolean value not getting mapped properly in hibernate with MySql
问题描述
我试图通过resultSetTransformer将存在查询的结果(返回TRUE / FALSE)从mysql数据库映射到POJO。我希望这个存在查询的结果可以映射到一个布尔值,但它不会&抛出错误:
org.hibernate.PropertyAccessException:在调用TestBean.value的setter时发生IllegalArgumentException
此异常的原因如下所示:
java.lang.IllegalArgumentException:参数类型不匹配
Java查询代码:
public class TestHibernate {
private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
public static void main(String [] args)throws ParseException {
try {
Query query = sessionFactory.openSession()。createSQLQuery( +
选择+
存在(select * from A where id = 3)作为值
;
query.setResultTransformer(Transformers.aliasToBean(TestBean.class));
List< TestBean> beanList =(List< TestBean>)query.list();
} catch(Exception e){
System.out.println(e);
}
}
}
Pojo :
public class TestBean {
private boolean value;
public boolean isValue(){
return value;
}
public void setValue(boolean value){
this.value = value;
}
}
我错过了什么吗? hibernate或mySql JDBC驱动程序的错误?
Hibernate版本:3.2.6GA
Mysql JDBC驱动程序:mysql-connector-java-5.1.2
我知道这是旧的答案,我试图从这里解决这个问题的答案不适合我。
除了@anil bk的回答,我重载了一个接受 String
作为参数的setter方法。现在它按预期工作。
public void setPriority(String priority){
this.priority =true.equals(priority);
}
这是我的 answer
I am trying to map the result of an exists query(which returns TRUE/FALSE) from a mysql DB to a POJO via resultSetTransformer. I would hope the result of this exists query can get mapped to a boolean but it does not & throws 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
Java Querying Code :
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
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
这篇关于布尔值不能在Hibernate中通过MySql正确映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!