英文:
How to map a foreign key with hibernate?
问题
我正在使用Spring Boot
(MVC
)和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;
}
英文:
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;
}
答案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
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论