简体   繁体   中英

Hibernate @OneToOne Mapping not Join the column?

Am new to Java Hibernate, using hibernate annotation to join and fetch the column from MySQL DB.I have two tables Order_headers table and Branch table.I have branch code in Order_headers table i have branch_code ,in branch table i have branch_code,branch_name,branch_desc . i want to combine Branch details table columns with order_headers table.

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 

}

Branch

@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     

}

Fetch Query

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

It shows only data from order_headers table.Not able to get the data from Branch table.Help me to solve this issue.

The mappedBy in the parent entity should refer to the property of child where you have your parent association. Make the mappedBy attribute in the parent to be changed as below.

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

Try this

Branch entity:

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

Order_header entity :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM