我试图从hsql db中读取某些值,并将这些值作为具有键和值的映射返回。我还有一个方法可以接受这些映射值并对其进行迭代,并根据条件获取某些值,然后将所有这些值添加到列表中。对我来说,条件和第一种方法都工作正常,但是在将值添加到列表时,我遇到了类强制转换异常
从表中读取值的方法:
List<EntityMap> sample = session.createQuery(" FROM EntityMap order by if.ifName").list();
for (Iterator<EntityMap> iterator = sample.iterator(); iterator.hasNext();) {
entityMap = (EntityMap) iterator.next();
if (IfName != entityMap.getIf().getIfName().toString()) {
IfName = entityMap.getIf().getIfName().toString();
entitymapobject = new ArrayList<EntityMap>();
}
entitymapobject.add(entityMap);
EntityMaplist.put(entityMap.getIf().getIfName(),entitymapobject);
}
tx.commit();
该方法返回一个映射,它具有从数据库中获取的值。之后,我尝试根据某些条件提取某些值。在此,我调用上述方法并对其进行迭代
proertyMap = listPROPERTNAMES();
System.out.println("inside loadproperty");
for (Iterator<Integer> itr1 = srcEntityIDList.iterator(); itr1.hasNext();) {
Integer aInteger = itr1.next();
for (Map.Entry<Long, List<PropertyMap>> entry : proertyMap.entrySet()) {
System.out.println(aInteger);
System.out.println(entry.getKey());
Long aLong = entry.getKey();
if (aLong.equals(Long.valueOf(aInteger))) {
System.out.println("values are equal");
trgtPropNameList.add(( (PropertyMap) entry.getValue()).getTgtpropnm());
}
}
}
return trgtPropNameList;
if (tx != null)
tx.rollback();
e.printStackTrace();
} finally {
session.close();
}
return EntityMaplist;
}
在这里尝试将值添加到列表(trgtPropNameList)时,我遇到了类强制转换异常。我的具有setter和getter方法的POJO类是
public class PropertyMap implements java.io.Serializable {
private PropertyMapId id;
private EntityMap entityMap;
private String tgtpropnm;
private String splitrule;
private String combinerule;
private String createdby;
private Date createdon;
public PropertyMap() {
}
public PropertyMap(PropertyMapId id, EntityMap entityMap) {
this.id = id;
this.entityMap = entityMap;
}
public PropertyMap(PropertyMapId id, EntityMap entityMap, String tgtpropnm,
String splitrule, String combinerule, String createdby,
Date createdon) {
this.id = id;
this.entityMap = entityMap;
this.tgtpropnm = tgtpropnm;
this.splitrule = splitrule;
this.combinerule = combinerule;
this.createdby = createdby;
this.createdon = createdon;
}
public PropertyMapId getId() {
return this.id;
}
public void setId(PropertyMapId id) {
this.id = id;
}
public EntityMap getEntityMap() {
return this.entityMap;
}
public void setEntityMap(EntityMap entityMap) {
this.entityMap = entityMap;
}
public String getTgtpropnm() {
return this.tgtpropnm;
}
public void setTgtpropnm(String tgtpropnm) {
this.tgtpropnm = tgtpropnm;
}
public String getSplitrule() {
return this.splitrule;
}
public void setSplitrule(String splitrule) {
this.splitrule = splitrule;
}
public String getCombinerule() {
return this.combinerule;
}
public void setCombinerule(String combinerule) {
this.combinerule = combinerule;
}
public String getCreatedby() {
return this.createdby;
}
public void setCreatedby(String createdby) {
this.createdby = createdby;
}
public Date getCreatedon() {
return this.createdon;
}
public void setCreatedon(Date createdon) {
this.createdon = createdon;
}
}
有人可以在这里帮我吗?
最佳答案
entry.getValue()
是类型List<PropertyMap>
而不是PropertyMap
的对象
关于java - Hibernate java.util.ArrayList无法强制转换。类强制转换异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15225359/