Hibernate Join query to fetch the data in Java?

Yugesh

I am new to Hibernate and Java server side. I have mapped MySQL table in Java using javax.persistence annotation. I have two tables:

order_header
In this table, I have
order_number(primary_key),total_order_value,delivery_charge,order_time columns.

order_details
In this table, I have
order_number,product_code,price columns.

In order_header the order_number is primary key.

For Each order_number there are n number of products in order_details table.
How can I combine both these tables?
What is the Query to get the value like Final Output

For Example :

Order Headers Table

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
}

I don't know how to write the combined query in Hibernate to get the details like final output.

Final Output

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

Please help me to solve this issue.

Mitul Sanghani

you have to add relationship between Order_headers and Order_details one to many in hibernate you can achieve this by following code in your Order_headers.java

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

//add getter and setter methods for orderDetailList.

once you add this code now you can write Criteria like

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

or you can Writer HQL like

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

or you can also write SQL Query in case you want to writer SQL Query no need of adding property orderDetailList in Order_headers.java here is SQL

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JPA JOIN FETCH query is not loading data

From Dev

NullPointerException with Hibernate FETCH JOIN

From Dev

Spring Data 'Left Join Fetch' query returning null

From Dev

Hibernate Fetch Mode Join Not working

From Dev

Alternative to fetch join with "ON clause" in Hibernate

From Dev

Nested fetch join with JPQL and Hibernate

From Dev

Fetch result SQL query data to Table JAVA

From Dev

Join Query not working in Hibernate

From Dev

Sql fetch data with join

From Dev

Search query to fetch data

From Dev

Hibernate HQL join fetch not recursively fetching

From Dev

How to join fetch two associations with JPA/Hibernate

From Dev

Hibernate fetch=join/select is not applicable for HQL?

From Dev

Hibernate Fetch is not longer sub-type of Join

From Dev

Hibernate Named Query Outer Join

From Dev

Hibernate Query for unmapped join table

From Dev

Error with Inner Join query in Hibernate

From Dev

JPA & Hibernate: Eager loading performing subsequent queries to fetch all data, instead of doing it in just one query

From Java

What is the difference between JOIN and JOIN FETCH when using JPA and Hibernate

From Dev

Join hibernate search query with criteria query restriction

From Dev

How to convert hibernate join query to ormlite query

From Dev

how to use fetch in Hibernate Query Language

From Dev

how to use fetch in Hibernate Query Language

From Dev

how to fetch this data in one query?

From Dev

fetch data and update vs saveOrUpdate in hibernate

From Dev

how to fetch data from database in Hibernate

From Dev

how to fetch data of remote side database in hibernate

From Dev

how to fetch data from database in Hibernate

From Dev

HIbernate many to one join criteria query

Related Related

HotTag

Archive