Hibernate 复合注释

发布于 2024-12-22 22:43:57 字数 743 浏览 0 评论 0原文

我是 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 示例。任何帮助将不胜感激。

谢谢

I am new to Hibernate, and am trying to map extra columns in a join table to the parent class. I could find an example at 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>

I want the above xml example in pure annotation. Any help will be greatly appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2024-12-29 22:43:57

采购类别:

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
}

Purchase class:

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 class:

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
}
锦上情书 2024-12-29 22:43:57

请参阅http://docs.jboss.org /hibernate/core/3.6/reference/en-US/html_single/#collections-ofvalues。从 3.6、IIRC 开始,注释就成为 hibernate 核心的一部分,因此注释的文档被嵌入到 3.6 的核心文档中。对于以前的版本,它是单独记录的(请参阅 http:// docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1821

See http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#collections-ofvalues. annotations are part of the hibernate core since 3.6, IIRC, and the documentation for annotations is thus embedded in the core documentation of 3.6. For previous versions, it was documented separately (see http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e1821)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文