2011-12-26 200 views
1

我是Hibernate的新手,試圖將連接表中的額外列映射到父類。我可以在http://docs.jboss.org/hibernate/core/3.5/reference/en/html/components.htmlHibernate複合註釋

<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例子找到一個例子。任何幫助將不勝感激。

謝謝

回答

0

採購類:

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 
} 

Order類:

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 
}