1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Yugesh
  • 4,030
  • 9
  • 57
  • 97

2 Answers2

1

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;
kondu
  • 410
  • 3
  • 11
  • Sorry am do like this only. when type the code, wrongly typed. – Yugesh Apr 24 '15 at 10:25
  • 1
    Try removing the explicit mention of fetch type. Everything else looks fine. Or use @MapsId so that you mention that the primary key of this class is being mapped to the parent. – kondu Apr 24 '15 at 10:35
  • refer [http://stackoverflow.com/questions/4324839/java-hibernate-mapping-exception-could-not-determine-type-for-java-util-map] – kondu Apr 27 '15 at 05:44
0

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;  
}  
stackUser44
  • 409
  • 4
  • 14
  • 25
  • it shows error like `Could not determine type for: hibernate.dao.manager.Order_headers, at table: branch, for columns: [org.hibernate.mapping.Column(order_headers)] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:336)` – Yugesh Apr 24 '15 at 11:40
  • Please try changing the name of the properties (follow the naming conventions). As I see the error may be caused due to the non-resolvence of order_headers is a property name or a class name. – kondu Apr 27 '15 at 05:41