Hibernate @OneToOne映射不加入列?

时间:2015-04-24 10:06:44

标签: java mysql hibernate

是Java Hibernate的新手,使用hibernate注释来连接并从MySQL DB中获取列。我有两个表 Order_headers 表和 Branch 表。我有分支代码在 Order_headers 表中我有branch_code,在分支表中我有 branch_code,branch_name,branch_desc 。我想将分支详细信息表格列与 order_headers 表合并。

Order_headers.class

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

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

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

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

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

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

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

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

@Column(name = "order_datetime")
private Timestamp order_datetime;

//format YYYY-mm-dd
@Column(name = "order_date")
private String order_date;

//format HH:mm:ss
@Column(name = "order_time")
private String order_time;

@Column(name = "sub_total")
private double sub_total;

@Column(name = "VAT")
private double VAT;

@Column(name = "grand_total")
private double grand_total;

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

@OneToOne(mappedBy = "order_headers", cascade=CascadeType.ALL)
@Fetch(FetchMode.JOIN)
private Branch branch;

@Column(name = "discount")
private double discount;

//Getter and setters 

}

@Entity
@Table(name = "branch")
public class Branch {

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

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

@OneToOne
@JoinColumn(name = "branch_code")
private Order_headers order_headers;

//Getter and setters     

}

获取查询

ArrayList<Order_headers> order_details = (ArrayList<Order_headers>) session.createQuery("FROM Order_headers").list();

它仅显示 order_headers 表中的数据。无法从分支表中获取数据。请帮助我解决此问题。

2 个答案:

答案 0 :(得分:1)

父实体中的mappedBy应该引用您拥有父关联的子属性。使父对象中的mappedBy属性更改如下。

@OneToOne(mappedBy = "order_headers", cascade=CascadeType.ALL)
private Branch branch;

答案 1 :(得分:0)

试试这个

分支实体:

@OneToOne(cascade=CascadeType.ALL)  
@JoinColumn(name="branch_code")  
public Order_headers  getOrder_headers()  
{  
    return order_headers ;
}  

Order_header实体:

 @OneToOne(cascade=CascadeType.ALL, mappedBy="order_headers")  
 public Branch  getBranch()  
{  
    return branch;  
}