java.lang.Integer无法强制转换为java.lang.String

时间:2015-03-25 14:17:29

标签: java hibernate

我不确定我在哪里使用字符串?

public void getRevisionTypeData(){

List<String> duaRevType = new ArrayList<String>();  


duaRevType = getRevisionTypes();
System.out.println("SIZE :" + duaRevType.size());

for (String revtypes : duaRevType) {

    System.out.println("Revision Types: " + revtypes);

}

}

当我运行该方法时,我得到了这个castexception

public List<String> getRevisionTypes() {

List<String> revTypes= null;
int num = 833;

try {
    Session session = factory.openSession();
    AuditReader auditReader = AuditReaderFactory.get(session);

    List<String> resultList = auditReader.createQuery()
            .forRevisionsOfEntity(DuaVO.class, DuaVO.class.getName(), false, true)
            .addProjection(AuditEntity.revisionType()).add(AuditEntity.id().eq(num)).getResultList();

     revTypes = resultList;

} catch (HibernateException e) {
    e.printStackTrace();
}

return revTypes;

控制台输出

      log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
Hibernate: 
    select
        duavo_aud0_.REVTYPE as col_0_0_ 
    from
        DUA_AUD duavo_aud0_,
        REVINFO defaultrev1_ 
    where
        duavo_aud0_.DUA_NUM=? 
        and duavo_aud0_.REV=defaultrev1_.REV
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
    at org.hibernate.type.descriptor.java.StringTypeDescriptor.unwrap(StringTypeDescriptor.java:39)
    at org.hibernate.type.descriptor.sql.VarcharTypeDescriptor$1.doBind(VarcharTypeDescriptor.java:64)
    at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
    at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
    at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
    at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:613)
    at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1900)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1861)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)
    at org.hibernate.loader.Loader.doQuery(Loader.java:909)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:354)
    at org.hibernate.loader.Loader.doList(Loader.java:2553)
    at org.hibernate.loader.Loader.doList(Loader.java:2539)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2369)
    at org.hibernate.loader.Loader.list(Loader.java:2364)
    at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:496)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)
    at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:231)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1264)
    at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)
    at org.hibernate.envers.query.internal.impl.AbstractAuditQuery.buildAndExecuteQuery(AbstractAuditQuery.java:103)
    at org.hibernate.envers.query.internal.impl.RevisionsOfEntityQuery.list(RevisionsOfEntityQuery.java:122)
    at org.hibernate.envers.query.internal.impl.AbstractAuditQuery.getResultList(AbstractAuditQuery.java:109)
    at com.envers.EnversTest.getRevisionTypes(EnversTest.java:104)
    at com.envers.EnversTest.getRevisionTypeData(EnversTest.java:71)
    at com.envers.EnversTest.main(EnversTest.java:50)

1 个答案:

答案 0 :(得分:2)

问题在于你的代码的这部分

List<Integer> resultList = (List<Integer>) auditReader.createQuery()
                .forRevisionsOfEntity(DuaVO.class, DuaVO.class.getName(), false, true)
                .addProjection(AuditEntity.revisionType()).add(AuditEntity.id().eq(num)).getResultList();

getResultList显然会返回一个List,该类型无法直接转换为Integer(String)。

我相信

duavo_aud0_.REVTYPE

属于VARCHAR类型。在使用Integer.parseInt进行操作或更改数据库中的数据类型时,您必须转换它们。


回答更新(根据评论中的讨论)

变化

AuditEntity.id().eq(num) 

AuditEntity.id().eq(String.valueOf(num))