在Hibernate映射中将Set转换为List

时间:2014-05-27 07:36:25

标签: java hibernate arraylist

以下是BillDetails中我的pojo课程,我使用了billProductDetailses Set。 现在我想使用Set我想使用List。 为此,我在BillDetails.hbm.xml中进行了更改,但在映射到列表时遇到问题。

public class BillDetails implements java.io.Serializable {
private Long billNo;
private CustomerDetails customerDetails;
private Float subTotal;
private Float vat;
private Float total;
private String paymentType;
private String status;
private Timestamp addDate;
private Set<BillProductDetails> billProductDetailses = new HashSet(0);
//getter and setter
}

public class BillProductDetails implements java.io.Serializable {

    private BillProductDetailsId id;
    private ProductDetails productDetails;
    private BillDetails billDetails;
    private long qty;
    private float unitPrice;
    private float sellingPrice;
    private Integer discountPercent;
      //getter and setter
     }
public class BillProductDetailsId  implements java.io.Serializable {


     private long productId;
     private long billNo;
     //getter and setter
     }

在BillDetails.hbm.xml

  <hibernate-mapping>
    <class name="iland.hbm.BillDetails" table="bill_details" catalog="retail_shop">
        <id name="billNo" type="java.lang.Long">
            <column name="bill_no" />
            <generator class="identity"></generator>
        </id>
        <many-to-one name="customerDetails" class="iland.hbm.CustomerDetails" fetch="join">
            <column name="customer_id" not-null="true" />
        </many-to-one>
        <property name="subTotal" type="java.lang.Float">
            <column name="sub_total" precision="10" />
        </property>
        <property name="vat" type="java.lang.Float">
            <column name="vat" precision="10" />
        </property>
        <property name="total" type="java.lang.Float">
            <column name="total" precision="10" />
        </property>
        <property name="paymentType" type="string">
            <column name="payment_type" length="30" not-null="true" />
        </property>
        <property name="status" type="string">
            <column name="status" length="20" />
        </property>
        <property name="addDate" type="timestamp">
            <column name="add_date" length="19" not-null="true" />
        </property>
        <set name="billProductDetailses" table="bill_product_details" inverse="true" lazy="false" fetch="join">
            <key>
                <column name="bill_no" not-null="true" />
            </key>
            <one-to-many class="iland.hbm.BillProductDetails" />
        </set>

    </class>
</hibernate-mapping>

这里我想使用List而不是Set 所以我补充道 private List<BillProductDetails> billProductList = new ArrayList<BillProductDetails>();BillDetails

并在hbm.xml中

<list name="billProductList" table="bill_product_details" inverse="true" lazy="false" fetch="join">
            <key>
                    <column name="??" not-null="true" />
                </key>
            <index column="??"/>
             <one-to-many class="iland.hbm.BillProductDetails" />
        </list>

我想知道应该是什么 <key><column name="??" not-null="true" /><index column="??"/>

1 个答案:

答案 0 :(得分:1)

在这种情况下你可以使用Bag映射。参考http://www.javatpoint.com/mapping-bag-in-collection-mapping

 <bag name="billProductDetailses" table="bill_product_details" lazy="false" cascade="all">
        <key>
            <column name="bill_no"/>
        </key>
        <one-to-many class="iland.hbm.BillProductDetails" />
    </bag>