0

我是 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();
}

有什么建议吗?

提前致谢。

4

1 回答 1

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;
}

订单项

@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)

它未经测试,但我希望它会起作用。

于 2013-01-28T15:54:55.737 回答