Hibernate使用列表中的外键传递查询一对多

时间:2017-08-03 06:36:10

标签: java hibernate

这是我的hibernate映射文件:

<class name="com.cms.entity.ContentPartnerMaster" table="BFContentPartnerMaster">
    <!-- <cache include="non-lazy" usage="read-only"/> -->
    <id name="partnerId" type="int" column="Id">
        <generator class="native" />
    </id>
    <property name="partnerName" column="PartnerName" type="string" />
    <property name="partnerDescription" column="PartnerDescription"
        type="string" />
    <property name="isActive" column="isActive" type="boolean" />
    <property name="partnerSalt" column="PartnerSalt" type="string" />
    <bag name="carousels" table="BFCaraousal" inverse="true" lazy="true"
        fetch="select">
        <key>
            <column name="Id" not-null="true" />
        </key>
        <one-to-many class="com.cms.entity.Carousel" />
    </bag>

</class>

<class name="com.cms.entity.Carousel" table="BFCaraousal">

    <id name="caraousalId" type="int" column="CaraousalId">
        <generator class="native" />
    </id>

    <property name="section" column="Section" type="string" />
    <property name="caraousalName" column="CaraousalName" type="string" />
    <property name="dateModified" column="DateModified" type="date" />
    <property name="dateAdded" column="DateAdded" type="date" />
    <property name="addedBy" column="AddedBy" type="int" />
    <property name="countryId" column="countryid" type="int" />
    <property name="partnerId" column="PartnerId" type="int" />

</class>

这是我的代码:

public class ContentPartnerMaster implements Serializable {
    private static final long serialVersionUID = 1L;
    private int partnerId;
    private String partnerName;
    private String partnerDescription;
    private Boolean isActive;
    private String partnerSalt;
    private ArrayList<Carousel> carousels;

    // getter setter ..


public class Carousel implements Serializable {
    private static final long serialVersionUID = 1L;
    private Integer caraousalId;
    private Integer partnerId;
    private Integer countryId;
    private String section;
    private String caraousalName;
    private Integer addedBy;
    private Date dateAdded;
    private Date dateModified;

在列表中使用外键传递一对多的Hibernate查询

在调用com.cms.entity.ContentPartnerMaster.carousels的setter时发生错误,如id IllegalArgumentException

1 个答案:

答案 0 :(得分:0)

尝试使用Set&lt;&gt;而不是ArrayList。

public class ContentPartnerMaster implements Serializable { ... private Set<Carousel> carousels;

稍后当您使用它时,可以将其强制转换为List&lt;&gt;。