我是jpa hibernate的新手,我在sql中有一个内连接查询但不确定如何在jpa-hibernate语法中创建相同的查询:
基本上我有3张桌子:
ORDERTBL(ORDERID(PK),ORDERDATE,ORDERSTATUS), 产品(PRODUCTID(PK),PRODUCT_NAME) ORDERLINKPRODUCTS(ORDERID(PK),PRODUCTID(FK),数量,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;
}
}
请在下面找到我的订单类:
@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();
}
有任何建议怎么办?
提前致谢。
答案 0 :(得分:0)
尝试将您的实体更改为:
订单
@Entity
@Table(name = "ORDER")
public class Order {
@Id
@Column(name = "ORDERID")
private Long id;
@Column(name = "ORDERDATE")
private Date orderdate;
@Column(name = "ORDERSTATUS")
private String orderStatus;
@OneToMany(mappedBy = "order", fetch = FetchType.EAGER)
private List<OrderItem> items;
}
OrderItem的
@Entity
@Table(name = "ORDERITEM")
public class OrderItem {
@Id
@Column(name = "ORDERITEMID")
private Long id;
@ManyToOne
@JoinColumn(name = "ORDERID")
private Order order;
@ManyToOne
@JoinColumn(name = "PRODUCTID")
private Product product;
@Column(name = "QUANTITY")
private int quantity;
}
产品
@Entity
@Table(name = "PRODUCT")
public class Product {
@Id
@Column(name = "PRODUCTID")
private Long id;
@Column(name = "NAME")
private String name;
}
查询:
public List<OrderItem> getOrderDetailsById(Long id) {
Product product = entityManager.find(Product.class, id);
return product.getOrderItems();
}
public void showProductsInOrder(Long id) {
List<OrderItem> items = getOrderDetailsById(id);
for (OrderItem item: items) {
Product product = item.getProduct();
System.out.println("Product: " + product.getName() + ", Quantity: " + product.getQuantity());
}
数据库:
ORDER (ORDERID (PK), ORDERDATE, ORDERSTATUS)
ORDERITEM (ORDERITEMID (PK), ORDERID (FK), PRODUCTID (FK), QUANTITY)
PRODUCT (PRODUCTID (PK), NAME)
这是未经测试的,但我希望它会起作用。