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/

相关文章:

hibernate - 从 grails 中的 Java 类访问数据库

Delphi 泛型 TObjectList<T> 继承

java - 求助理解Java Reflection --> Android ParcelableContainer

java - 在扩展类上声明proporder

java - AES加密跨组件Java/Scala和C

java - "Standardized"处理 Java EE 应用程序生命周期的方式

java - Hibernate 连接表

java - 使用 Stream API 将 Map 转换为另一个 Map

java - JPA/Hibernate : What's better for composite primary keys, @IdClass 或 @EmbeddedId 实现,为什么?

Java 泛型 : creating collections of class objects extending Throwable