【发布时间】:2019-04-07 10:09:49
【问题描述】:
我有两个 JPA 对象,即 Product 和 Order,它们之间存在多对多关系。
@Entity
@Table(name = "orders")
@JsonInclude(NON_NULL)
public class Order implements Serializable {
public static final int PRECISION = 2;
@Id
@GeneratedValue(strategy = IDENTITY)
@JsonIgnore
private String orderId;
@Column(unique = true, nullable = false, length = 8)
private String orderNumber;
@Column
private BigDecimal discount;
@Column(nullable = false)
private BigDecimal taxPercent;
private BigDecimal total;
private BigDecimal totalTax;
private BigDecimal grandTotal;
@Column(length = 10)
private String status;
@ManyToMany(cascade = ALL, fetch = FetchType.EAGER)
@JoinTable(
name = "order_product",
joinColumns = @JoinColumn(name = "order_id", updatable = false, nullable = false),
inverseJoinColumns = @JoinColumn(name = "product_id", updatable = false, nullable = false)
)
private List<Product> products = new ArrayList<>();
public BigDecimal getTotal() {
BigDecimal total = new BigDecimal(ZERO);
if (products == null || products.isEmpty()) {
return total;
}
for (Product product : products) {
total = total.add(product.getPrice());
}
return scaled(total);
}
public BigDecimal getTotalTax() {
return scaled(getTotal().multiply(taxPercent.divide(new BigDecimal("100"))));
}
public BigDecimal getGrandTotal() {
BigDecimal total = this.getTotal().add(getTotalTax());
if (discount != null) {
return scaled(total.subtract(discount));
}
return scaled(total);
}
private BigDecimal scaled(BigDecimal value) {
return value.setScale(PRECISION, ROUND_FLOOR);
}
.. Getters and setters ...
}
和产品
@Entity
@Table(name = "products")
public class Product implements Serializable {
@Id
@GeneratedValue(strategy = IDENTITY)
@JsonIgnore
private String productId;
@Column(unique = true, nullable = false, length = 10)
private String upc;
@Column(unique = true, nullable = false, length = 13)
private String sku;
@Column(nullable = false)
private String description;
@Column(nullable = false)
private BigDecimal price;
public BigDecimal getPrice() {
return price;
}
@ManyToMany(mappedBy="products", fetch=FetchType.EAGER)
@JsonIgnore
private List<Order> orders = new ArrayList<>();
// Getters and setters.
}
这是我的 DML
insert into products (product_id, upc, sku, description, price) values ('1', '1257833283', '9394550220002', 'Diva Jeans', 39.99);
insert into products (product_id, upc, sku, description, price) values ('2', '1358743283', '7394650110003', 'Polo Shirt', 19.99);
insert into products (product_id, upc, sku, description, price) values ('3', '1458843283', '7394750120000', 'Floral Swing Skirt', 69.99);
insert into products (product_id, upc, sku, description, price) values ('4', '1358753283', '7394850130001', 'Denim Short', 29.99);
insert into products (product_id, upc, sku, description, price) values ('5', '1258793283', '7394950140000', 'True Skinny Jeans', 49.99);
insert into orders (order_id, order_number, tax_percent, status) values ('1', 'RTL_1001', 10, 'SHIPPED');
insert into orders (order_id, order_number, discount, tax_percent, status) values ('2', 'RTL_1002', 15.55, 10, 'FULFILLED');
insert into order_product (order_id, product_id) values ('1', '1');
insert into order_product (order_id, product_id) values ('1', '2');
insert into order_product (order_id, product_id) values ('2', '2');
我只想选择这样的产品
-
如果我按不存在的订单查询产品(比如 order_id = 3 不存在),我想返回所有符合条件的产品,因为订单尚未创建。
如果我按确实存在的订单查询产品,那么我希望数据库返回与订单无关的所有产品。例如对于
order_id = 1,我希望返回 id = 3、4 和 5 的产品。由于 order_id = 1 已经与 product_id = 1 和 2 相关联,我不希望这些产品被退回。
如果我要编写一个 JDBC SQL 查询
SELECT p.product_id
FROM Product p
WHERE NOT EXISTS
(
SELECT 1
FROM order_product
WHERE product_id = p.product_id
AND
order_id = ?
)
OR
NOT EXISTS
(
SELECT 1
FROM order_product
WHERE order_id = ?
)
我不知道如何使用 JPA 创建类似的查询。我能想到的都是这样的。
创建一个继承自 JPARepository 的 ProductRepository
从产品表中获取所有产品,包括 order_product 表中的产品。
使用 JPA 获取给定订单 id 的产品,并从 (2) 中的产品中删除所述产品
我希望有更好的方法来做到这一点。
【问题讨论】:
标签: java jpa many-to-many