Hibernate java.lang.classCastException:arraylist不兼容/未使用arraylist

Hibernate java.lang.classCastException:arraylist不兼容/未使用arraylist,hibernate,arraylist,classcastexception,Hibernate,Arraylist,Classcastexception,使用Hibernate和GenericDAO接口,我指出了以下错误: java.lang.ClassCastException:java.util.ArrayList与OperationalPostBO不兼容 <property name="opCode" column="OPCODE" type="string" /> <property name="label" column="LABEL" type="string" /> <prope

使用Hibernate和GenericDAO接口,我指出了以下错误: java.lang.ClassCastException:java.util.ArrayList与OperationalPostBO不兼容

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>
问题出在这一行代码中: OperationalPostBO OperationalPostBO=operationalPostDAO.findOperationalPostByOpCode(操作码)

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>

问题是:我没有在所有代码中使用任何arrayList。我只使用java.util.Set,并通过Hashset实现它

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>
hbm文件: `

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>

`


    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>
返回一个列表

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>
PortfolioList(实际上是一个集合)是另一种东西

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>
比如:

    <property name="opCode" column="OPCODE" type="string" />
    <property name="label" column="LABEL" type="string" />
    <property name="type" column="TYPE" type="string" />

    <set name="portfolioList" cascade="all">
        <key>
            <column name="OPERATIONALPOST_OID" />
        </key>
        <one-to-many class="PortfolioBO" />
    </set>

    <many-to-one name="structureElement" class="StructureElementBO" column="STRUCTUREELEMENT_OID" />

</class>
<query name="OperationalPostBO.findOperationalPostByOpCode">
    <![CDATA[select o from OperationalPostBO o where o.opCode = ?]]>
</query>
Collection<OperationalPostBO> operationalPostBOs = operationalPostDAO.findOperationalPostByOpCode(opCode);
Collection operationalPostBOs=operationalPostDAO.findOperationalPostByOpCode(操作码);

“我没有在我所有的代码中使用任何arrayList”-我怀疑这是错误的谢谢你的回答@NimChimpsky。我搜索了整个项目,没有找到任何“arraylist”。甚至在我使用的“operationalPost.hbm.xml”中。此外,“find”方法甚至不应该返回一个列表,它应该返回一个项显示findOperationalPostByOpCode()的代码它在hbm文件中实现:是的,你是对的。但它是“postbo”中的嵌套列表。解决方案是什么?创建一个引用,它是列表而不是集合,甚至是集合。参见编辑