java实现多对多关系的方法_java – 如何在JPA中实现复杂的多对多关系?
這里是db模式
CREATE TABLE Products
(
id INT NOT NULL AUTO_INCREMENT,
category_id INT NOT NULL,
description VARCHAR(100),
price DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (category_id) REFERENCES Categories(id)
) ENGINE = INNODB;
CREATE TABLE Orders
(
id INT NOT NULL AUTO_INCREMENT,
customer_id INT NOT NULL,
status VARCHAR(20) NOT NULL,
date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (id),
FOREIGN KEY (customer_id) REFERENCES Customers(id)
) ENGINE = INNODB;
CREATE TABLE OrderDetails
(
product_id INT NOT NULL,
order_id INT NOT NULL,
quantity INT NOT NULL,
subtotal DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (product_id, order_id),
FOREIGN KEY (product_id) REFERENCES Products(id),
FOREIGN KEY (order_id) REFERENCES Orders(id)
) ENGINE = INNODB;
模特
@Embeddable
public class OrderDetailPK
{
private Product product;
private Order order;
public OrderDetailPK() {}
public OrderDetailPK(Product product, Order order)
{
this.product = product;
this.order = order;
}
}
public class OrderDetail {
@EmbeddedId
private OrderDetailPK id;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="product_id", insertable=false, updatable=false)
private Product product;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="order_id", insertable=false, updatable=false)
private Order order;
private int quantity;
private double subtotal;
public OrderDetail() {}
public OrderDetail(OrderDetailPK id, int quantity, double subtotal)
{
this.product = id.getProduct();
this.order = id.getOrder();
this.quantity = quantity;
this.subtotal = subtotal;
}
// getters, setters
}
public class Product {
@Id
private int id;
private String description;
private double price;
@ManyToOne
@JoinColumn(name="category_id")
private Category category;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "Products")
private List orderDetail;
}
public class Order {
@Id
private int id;
@ManyToOne
@JoinColumn(name="customer_id")
private Customer customer;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "Orders")
private List orderDetail;
}
由于某些原因,我不斷收到錯誤
Concrete type "class models.OrderDetail" with application identity does not declare any primary key fields.
誰能指出我問題出在哪里?謝謝
最佳答案 當我之前這樣做時(詳見
this question and answer),我在可嵌入的ID原語中創建了字段(對應于所引用實體的ID字段),然后在實體中使用了
@MapsId.我相信這是滿足所有要求的最簡單(并且我敢說是正確的):實體中的字段是關系,ID類中的字段是原始的,每列都只映射一次(@MapsId字段不是真正的映射,而是一些別名).
將其應用于您的案例,ID類如下所示:
@Embeddable
public class OrderDetailPK {
private final int productId;
private final int orderId;
public OrderDetailPK(int productId, int orderId) {
this.productId = productId;
this.orderId = orderId;
}
}
實體類看起來像:
public class OrderDetail {
@EmbeddedId
private OrderDetailPK id;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("productId")
private Product product;
@ManyToOne(cascade = CascadeType.ALL)
@MapsId("orderId")
private Order order;
private int quantity;
private double subtotal;
public OrderDetail(Product product, Order order, int quantity, double subtotal) {
this.id = new OrderDetailPK(product.getId(), order.getId());
this.product = product;
this.order = order;
this.quantity = quantity;
this.subtotal = subtotal;
}
protected OrderDetail() {}
}
總結
以上是生活随笔為你收集整理的java实现多对多关系的方法_java – 如何在JPA中实现复杂的多对多关系?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 平安是国企还是私企
- 下一篇: 农夫山泉股票代码是多少