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.

谁能指出我问题出在哪里?谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值