多个@ManyToOne字段指向JPA / Hibernate中的相同实体 [英] Multiple @ManyToOne fields pointing to same entity in JPA / Hibernate

查看:200
本文介绍了多个@ManyToOne字段指向JPA / Hibernate中的相同实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Order 实体,它有一个 billingAddress 和一个 shippingAddress 。我也有一个地址实体。我试图让一个地址表包含发货地址和帐单地址,因为没有什么可以区分它们,并且结算和送货地址可以在一个或多个订单中相同。我在 Order 中的地址字段上使用了 @ManyToOne ,但我不确定正确的制作方式这是双向的。

我有两个问题:
$ b $ ol

  • @ManyToOne 适合地址字段,因为它实际上是一个n:2的关系,我只是使用两个单独的字段来表示?如果不是,我应该怎么做?

  • 假设1.是好的,我该如何使映射成为双向的(我应该在中使用哪些注释)地址实体?)?这可以通过在地址



  • 注释中列出多个列来完成。代码:

      @Entity 
    @Table(name =orders)
    public class Order {
    // ...
    私人地址shippingAddress;
    私人地址billingAddress;

    @ManyToOne
    @JoinColumn(name =shipping_address_id,referencedColumnName =address_id,nullable = false)
    public Address getShippingAddress(){
    return shippingAddress;

    $ b @ManyToOne
    @JoinColumn(name =billing_address_id,referencedColumnName =address_id,nullable = false)
    public Address getBillingAddress(){
    返回billingAddress;

    //
    }

    @Entity
    @Table(name =addresses)
    public class Address {
    //地址1,地址2,城市,州等

    //如何链接回订单?
    私人套餐<订单>命令;


    解决方案

    关于你的第一个问题:它是一个ManyToOne?



    这取决于。如果几个订单可以有相同的送货地址,那么它是一个ManyToOne。如果只有一个订单可以有给定的送货地址,那么它是一个OneToOne。帐单地址相同。



    我不确定将关联设为双向是一个好主意。在这种情况下,我可能不会这样做。但是如果你想使其成为双向的,那么你必须使它们成为双向的。你确实在这里有两个不同的协会。映射将如下所示:

      @OneToMany(mappedBy =shippingAddress)
    private Set< Order> ; shippedOrders;

    @OneToMany(mappedBy =billingAddress)
    私人设置< Order> billedOrders;

    或者,如果关联实际上是OneToOne(请参阅第一个问题的答案) p>

      @OneToOne(mappedBy =shippingAddress)
    private订单shippedOrder;

    @OneToOne(mappedBy =billingAddress)
    private订单billedOrder;


    I have an Order entity that has a billingAddress and a shippingAddress. I also have an Address entity. I am trying to make a single address table hold both the shipping and billing addresses, since there is nothing to differentiate them, and the billing and shipping address can be the same in one or multiple orders. I have used @ManyToOne on the address fields in Order, but I'm not sure of the proper way to make this bidirectional.

    I have two questions:

    1. Is @ManyToOne appropriate for the address fields, since it is really an n:2 relationship that I simply am using two separate fields to represent? If not, what should I do instead?
    2. Assuming 1. is OK, how do I make the mapping bidirectional (What annotation(s) should I use in the Address entity?)? Can this be done by listing multiple columns in an annotation in Address?

    Code:

    @Entity
    @Table(name = "orders")
    public class Order {
      //...
        private Address shippingAddress;
        private Address billingAddress;
    
        @ManyToOne
        @JoinColumn(name = "shipping_address_id", referencedColumnName = "address_id", nullable = false)
        public Address getShippingAddress() {
            return shippingAddress;
        }
    
        @ManyToOne
        @JoinColumn(name = "billing_address_id", referencedColumnName = "address_id", nullable = false)
        public Address getBillingAddress() {
            return billingAddress;
        }
      //...
    }
    
    @Entity
    @Table(name = "addresses")
    public class Address {
        //address1, address2, city, state, etc.
    
        //how to link back to Orders?
        private Set<Order> orders;
    }
    

    解决方案

    Regarding your first question: is it a ManyToOne?

    It depends. If several orders can have the same shipping address, then it's a ManyToOne. If only one order can have a given shipping address, then it's a OneToOne. Same for billing address.

    I'm not sure making the association bidirectional is a good idea. I probably wouldn't do it in this case. But if you want to make it bidirectional, then you have to make them bidirectional. You indeed have two different associations here. The mapping would thus look like the following:

    @OneToMany(mappedBy = "shippingAddress")
    private Set<Order> shippedOrders;
    
    @OneToMany(mappedBy = "billingAddress")
    private Set<Order> billedOrders;
    

    or, if the association is in fact a OneToOne (see answer to the first question):

    @OneToOne(mappedBy = "shippingAddress")
    private Order shippedOrder;
    
    @OneToOne(mappedBy = "billingAddress")
    private Order billedOrder;
    

    这篇关于多个@ManyToOne字段指向JPA / Hibernate中的相同实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆