Hibernate复合注释

时间:2011-12-26 16:47:35

标签: hibernate annotations set composite

我是Hibernate的新手,我正在尝试将连接表中的额外列映射到父类。我可以在http://docs.jboss.org/hibernate/core/3.5/reference/en/html/components.html

找到一个例子
<class name="eg.Order" .... >
....
<set name="purchasedItems" table="purchase_items" lazy="true">
<key column="order_id">
<composite-element class="eg.Purchase">
<property name="purchaseDate"/>
<property name="price"/>
<property name="quantity"/>
<many-to-one name="item" class="eg.Item"/> <!-- class attribute is optional -->
</composite-element>
</set>
</class>

我想在纯注释中使用上面的xml示例。任何帮助将不胜感激。

由于

1 个答案:

答案 0 :(得分:1)

购买类:

package eg;

@javax.persistence.Embeddable
public class Purchase{

    private Date purchaseDate;
    private Double price;
    private Integer quantity;

    @javax.persistence.ManyToOne
    private Item item;

    // getters and setters
}

订单类:

package eg;

@javax.persistence.Entity
public class Order {

    @javax.persistence.ElementCollection(fetch = javax.persistence.FetchType.LAZY)
    @javax.persistence.CollectionTable( 
         name = "purchase_items", 
         joinColumns = @JoinColumn( name = "order_id" ) 
    )
    java.util.Set<eg.Purchase> purchasedItems;

    // getter and setter for purchasedItems
}