对于我可能愚蠢的问题,我很抱歉。我有产品和订单表(具有多对多关系),还有一个用户表。现在我想通过 user_id 和特殊字段“order_satus”获取产品数量。我可以进行两个查询,按特殊标准获取订单,然后按顺序获取产品尺寸。但这根本不是最优的。当我使用 JDBCTemplate 时,我做了很多连接并且只得到一个查询。
这是我的实体:
@Entity
@Table(name = "shop.order")
public class Order {
@Id
@Column(name = "order_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private long orderId;
private long user_id;
@Column(name = "databegin")
private Date dateBegin;
@Column(name = "dataend")
private Date dateEnd;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", insertable = false, updatable = false)
private User user;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "order_product", joinColumns = { @JoinColumn(name = "order_id") }, inverseJoinColumns = { @JoinColumn(name = "product_id") })
private List<Product> products;
}
产品实体
@Entity
@Table(name = "product")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int product_id;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(name = "order_product", joinColumns = { @JoinColumn(name = "product_id") }, inverseJoinColumns = { @JoinColumn(name = "order_id") })
private List<Order> order;
public List<Order> getOrder() {
return order;
}
public void setOrder(List<Order> order) {
this.order = order;
}
@Column
@NotBlank
private String name;
@Column
@Max(value = 250)
private String descr;
@Column
@Max(value = 250)
private String manufacturer;
@Column
private Double price;
@Column
private Byte[] barcode;
@Column
private Byte[] picture;
@ForeignKey(name = "category_id")
private int category_id;