休眠AliasToBean变压器,将CHAR(1)转换为字符串:“预期类型= java.lang.String”错误

时间:2018-12-02 22:56:16

标签: spring hibernate

我正在使用AliasToBean Transformer填充POJO bean的应用程序上。数据库是Oracle。

String sql = "select app.id as \"applicationId\", 
              app.valid_flag as \"validFlag\" from applications_t app";
    query.setResultTransformer(Transformers.aliasToBean(Report.class));     
    // run the query
    List<Report> result = (List<Report>)query.list();

一个字段是一个字符串,它从CHAR(1 CHAR)获取其值。

public class Report implements Serializable {
    String validFlag;  // With Getter/Setter
    //...
}

我收到如下所示的CHAR到字符串转换错误:

Caused by: org.hibernate.PropertySetterAccessException: IllegalArgumentException occurred 
while calling setter for property [validFlag (expected type = java.lang.String)]; 
target = [com.app.Report@1a89016a], property value = [N] setter of com.app.Report.validFlag
    at ...

无法将CHAR(1)的{​​{1}}属性值自动存储在字符串中。有什么解决方案?

请注意,这不是我可以执行N的Table bean。这是转换后的自定义POJO。

1 个答案:

答案 0 :(得分:0)

您可以在查询中尝试cast(app.valid_flag as VARCHAR2(1)) as \"validFlag\"。或query.addScalar("validFlag", StringType.INSTANCE).list()