gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:30:05 26 4
gpt4 key购买 nike

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

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

}

}

当我运行该方法时,我收到此强制转换异常

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)

最佳答案

问题出在代码的这一部分

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(字符串)。

我相信

duavo_aud0_.REVTYPE

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

<小时/>

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

改变

AuditEntity.id().eq(num) 

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

关于java.lang.Integer 无法转换为 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29258236/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com