Hibernate java.util.ArrayList无法强制转换..类强制转换异常

时间:2013-03-05 13:45:30

标签: java hibernate generics

我正在尝试从hsql db中读取某些值,这些值将作为map返回,其中包含键和值。我还有一个接受这些映射值的方法,它将迭代它并将根据条件获取某些值。之后它将所有这些值添加到列表中。对我来说条件和第一种方法工作正常,但在将值添加到列表时,我面临类强制转换异常

从表中读取值的方法:

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

此方法返回一个map,它具有从db获取的值。在那之后我试图根据一些条件提取某些值。在这里我调用上面的方法,我正在迭代它

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)时,我得到一个类强制转换异常。我的POJO类有setter和getter方法

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

有人可以帮我吗?

3 个答案:

答案 0 :(得分:2)

entry.getValue()List<PropertyMap>类型的对象,而不是PropertyMap

答案 1 :(得分:0)

如果你不能施展,只需建造并填补一个新的。

答案 2 :(得分:0)

更改行

trgtPropNameList.add(( (PropertyMap) entry.getValue()).getTgtpropnm());

for(PropertyMap map : entry.getValue(){
    trgtPropNameList.add(((PropertyMap)map).getTgtpropnm());
}

应该解决铸造问题。

相关问题