如何使用Hibernate映射外键?

huangapple go评论82阅读模式
英文:

How to map a foreign key with hibernate?

问题

我正在使用Spring BootMVC)和hibernate编写一个在线商店。我有一个订单类,其中我需要一个Сart链接。但问题是在数据库中我没有一个特定的Сart表,而是有一个cart_products表,其中主键由两列组成(如下图所示!)。我确实需要在Order类中建立一个连接,所以我决定在Hibernate级别上创建一个复合主键(似乎我已经做到了),但我不知道接下来该怎么办!我被卡住了。请告诉我该去哪里?我该如何解决我的问题?

OrderClass:

@Entity
@Table(name = "pg_order")
public class Order {

    // Fields
    //
    private @Id
    @GeneratedValue
    Long id;

    private String address;

    @Column(name = "phone_number")
    private String phoneNumber;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "date_order")
    private Date dateOrder;

    @Enumerated(EnumType.STRING)
    @Column(name = "order_status")
    private OrderStatus orderStatus;

    @Column(name = "total_cost")
    private BigDecimal totalCost;

    // Relationships
    //
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

//    @OneToMany
//    @JoinColumn(name = "cart_products_pkey")
//    private Cart cart;
}

Cart:

@Entity
@Table(name = "cart_products")
public class Cart {

    @Embeddable
    @NoArgsConstructor
    @AllArgsConstructor
    static class CartId implements Serializable {

        private Long orderId;

        private Long drinkId;
    }


    // Fields
    //
    @EmbeddedId
    private CartId cartId;

    @ManyToOne(optional = false)
    @JoinColumn(name = "order_id")
    private Order order;

    @ManyToOne(optional = false)
    @JoinColumn(name = "drink_id")
    private Drink drink;

    private int count;
}

如何使用Hibernate映射外键?

如何使用Hibernate映射外键?

如何使用Hibernate映射外键?

英文:

I am writing an online store using Spring Boot (MVC) and hiberbate. I have an order class where I need a Сart link. But the problem is that in the database I do not have a specific Сart table, but there is a cart _products table, where the peimary key consists of two columns (as shown in the picture below!). I really need a connection in the Order class, so I decided to make a Composite Primary Key at the hibernate level (and I seem to have done it), but I can't figure out what to do next! I am stuck. Please tell me where to go? How can I solve my problem?

OrderClass:

@Entity
@Table(name = "pg_order")
public class Order {

    // Fields
    //
    private @Id
    @GeneratedValue
    Long id;

    private String address;

    @Column(name = "phone_number")
    private String phoneNumber;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "date_order")
    private Date dateOrder;

    @Enumerated(EnumType.STRING)
    @Column(name = "order_status")
    private OrderStatus orderStatus;

    @Column(name = "total_cost")
    private BigDecimal totalCost;

    // Relationships
    //
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

//    @OneToMany
//    @JoinColumn(name = "cart_products_pkey")
//    private Cart cart;
}

Cart:

@Entity
@Table(name = "cart_products")
public class Cart {

    @Embeddable
    @NoArgsConstructor
    @AllArgsConstructor
    static class CartId implements Serializable {

        private Long orderId;

        private Long drinkId;
    }


    // Fields
    //
    @EmbeddedId
    private CartId cartId;

    @ManyToOne(optional = false)
    @JoinColumn(name = "order_id")
    private Order order;

    @ManyToOne(optional = false)
    @JoinColumn(name = "drink_id")
    private Drink drink;

    private int count;
}

如何使用Hibernate映射外键?

如何使用Hibernate映射外键?

如何使用Hibernate映射外键?

答案1

得分: 0

If you need access to the 'DRINKS' for that order. You need to change the relation to Cart from the Order class.

You have commented a relationship where ORDER just has access to one CART, since you need to access N CARTS (One to Many), you need to add a SET. Something like this:

@OneToMany
@JoinColumn(name = "cart_products_pkey")
private Set cartProducts;

Now the ORDER has a SET of CART.

You can easily access the CARTS of that ORDER with order.getCartProducts()

And since CART has a key to DRINK, you can easily access it.

Hope this can help you.

英文:

If you need access to the 'DRINKS' for that order. You need to change the relation to Cart from the Order class.

You have commented a relationship where ORDER just have access to one CART, since you need to access N CARTS (One to Many) you need to add a SET. Something like this:

@OneToMany
@JoinColumn(name = "cart_products_pkey")
private Set<Cart> cartProducts;

Now the ORDER has a SET of CART.

You can easily access to the CARTS of that ORDER with order.getCartProducts()

And since CART has a key to DRINK, you can easily access it.

Hope this can help you.

huangapple
  • 本文由 发表于 2020年7月28日 04:44:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63123305.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定