Hibernate Join查询以获取Java中的数据?

时间:2015-03-18 10:09:23

标签: java mysql hibernate

我是Hibernate和Java服务器端的新手。我使用javax.persistence注释在Java中映射MySQL表。我有两张桌子:

order_header
在此表格中,我有order_number(primary_key),total_order_value,delivery_charge,order_time列。

order_details
在这张表中,我有 order_number,product_code,price列。

order_header中, order_number 是主键。

对于每个 order_number order_details 表中的产品数量为n
如何将这两个表组合起来?
什么是获取值的查询,如最终输出

例如:

订单标题表

Order_number     total_order_value     delivery_charge      order_time
----------------------------------------------------------------------
   25                2550                     50             03:20:50
   36                350                      50             03:20:50
----------------------------------------------------------------------

Order_headers.java

@Entity
@Table(name = "order_headers")
public class Order_headers {

@Id @GeneratedValue
@Column(name = "order_number")
private int order_number;

@Column(name = "total_order_value")
private double order_value;

@Column(name = "delivery_charge")
private String delivery_charge;

@Column(name = "order_time")
private String order_time;

//here getter and setter methods
}

ORDER_DETAILS

Order_number     product_code     price
---------------------------------------
    25               235          1500
    25               240          1000
    36               50           40
    36               96           60
    36               150          200    

Order_details.java

@Entity
@Table(name = "order_details")
public class Order_details {

@Column(name = "Order_number")
private int Order_number;

@Column(name = "product_code")
private String product_code;

@Column(name = "price")
private String price;

//here getter and setter methods
}

我不知道如何在Hibernate中编写组合查询以获取最终输出等详细信息。

最终输出

Order_number     total_order_value     product_value   delivery_charge      order_time
---------------------------------------------------------------------------------------
   25                2550                   2500             50             03:20:50
   36                350                    300              50             03:20:50
---------------------------------------------------------------------------------------

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:2)

你必须在hibernate中添加一对多的Order_headers和Order_details之间的关系,你可以通过在Order_headers.java中执行以下代码来实现这一点

@OneToMany(mappedBy = "Order_number", fetch = FetchType.LAZY)
private List<Order_details> orderDetailList;

//add getter and setter methods for orderDetailList.

现在添加此代码后,您可以编写标准,如

Criteria criteria = session.createCriteria(Order_headers.class);
criteria.setFetchMode("Order_details",FetchMode.JOIN);
List<Order_headers> list = criteria.list();

或者你可以像写作HQL一样

session.createQuery("from Order_headers cont join cont.Order_details where cont.id=1");

或者您也可以编写SQL Query以防您想要编写SQL Query而不需要在Order_headers.java中添加属性orderDetailList这里是SQL

session.createSQLQuery("SELECT * FROM Order_headers order JOIN Order_details details ON order.order_number = details.order_number");