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

标签 java hibernate generics

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

从表中读取值的方法:

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 ,它具有从数据库获取的值。之后,我尝试根据某些条件提取某些值。在此我调用上述方法并迭代它

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

有人可以帮我吗?

最佳答案

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

关于java - Hibernate java.util.ArrayList 无法转换..类转换异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15225359/

相关文章:

java - 如何在 JPA 标准 API 中进行不同计数?

java - 当前使用 WSO2 Identity Server API 的示例

java - 注入(inject)的实例变量在方面执行时为空

java - 将 LocalDateTime 设置为使用 hibernate 4.3.10 自动生成

java - 组合 Hibernate 查询而不进行表扫描

java - Hibernate 实体和存储库的正确文件路径

java - 泛型/接口(interface)方法不适用

java - 使用 Java 从 JSON 中提取名称/值对

swift - 如何在类层次结构中应用 Swift 泛型/协议(protocol)?

java - 在这种情况下如何声明和使用通用映射