我是 jpa hibernate 的新手,我在 sql 中有一个内部连接查询,但不知道如何在 jpa-hibernate 语法中创建相同的查询:
基本上我有3张桌子:
ORDERTBL(ORDERID(PK),ORDERDATE,ORDERSTATUS), PRODUCT(PRODUCTID(PK),PRODUCT_NAME), ORDERLINKPRODUCTS(ORDERID(PK),PRODUCTID(FK),QUANTITY,ORDERLINKPRODUCTID(FK))
我想从产品表中获取特定订单的所有产品名称,我应该从 ORDERLINKPRODUCT 查询,因为它包含所有订单。
请在下面找到我的产品类:
@Table(name = "PRODUCTS")
@Entity
public class Products { /**
*
*/
private static final long serialVersionUID = -3882913429956818022L;
@Column(name = "PRODUCTID")
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "PRODUCT_NAME")
private String productName;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
请在下面找到我的 Orderclass:
@Table(name = "ORDERTBL")
@Entity
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "ORDERID")
private Long id;
@Column(name = "ORDERDATE")
private Date orderdate;
@Column(name = "ORDERSTATUS")
private String orderSatus;
public String getOrderSatus() {
return orderSatus;
}
public void setOrderSatus(String orderSatus) {
this.orderSatus = orderSatus;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getOrderdate() {
return orderdate;
}
public void setOrderdate(Date orderdate) {
this.orderdate = orderdate;
}
}
请在下面找到我的 ProductOder 课程:
@Entity
@Table(name = "ORDERLINKPRODUCTS")
public class ProductOder implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8420487251416906739L;
@Column(name = "ORDERLINKID")
@Id
private int orderLinkId;
@Column(name = "ORDERID")
@GeneratedValue(strategy = GenerationType.AUTO)
private int orderId;
@Column(name = "PRODUCTID")
private int productId;
@Column(name = "QUANTITY")
private int quantity;
public int getOrderLinkId() {
return orderLinkId;
}
public void setOrderLinkId(int orderLinkId) {
this.orderLinkId = orderLinkId;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getProductId() {
return productId;
}
public void setProductId(int productId) {
this.productId = productId;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
请在下面找到我想在 JPA-Hibernate 中创建的 sql 查询:
SELECT prod.PRODUCT_NAME
FROM ORDERTBL ord
INNER JOIN ORDERLINKPRODUCTS link ON ord.ORDERID = link.ORDERID
INNER JOIN products prod ON link.PRODUCTID = prod.PRODUCTID
WHERE ord.ORDERID = 1
请在下面找到我的方法,其中没有包含 jpa-hibernate 查询的实现:
@Override
@SuppressWarnings("unchecked")
public List<Order> getOrderDetailsById(int id) {
// TODO Auto-generated method stub
//String hql = "from Person p, where p.firstName = :firsName and p.password //password";
//return entityManager.createQuery("select o from Products o")
//.getResultList();
}
有什么建议吗?
提前致谢。