I have a complex query crossing 7 tables and want to know how to implement it within Hibernate.
My current attempt is to make the query using session.createSQLQuery and I would map the result to a particular entity.
I am not sure how to do that as in the past I have only worked with one table to one entity. Where would I need to specify that I would like to use a complex query that could span multiple tables? Does that go only in my code? My hbm.xml file? I can't think of anything else beyond my current attempt.
Here is an example of my query:
String stringQuery =
"select WI.Customer_Id, CU.Card, CU.Code, "+
"PI.Identity_Card, PI.Name, PI.Surname, PI.Gender, "+
"AD.Zip, AD.Geo_Lat, AD.Geo_Long, "+
"CO.City_Geo_Level, "+
"CU.Address_id, CA.Name, "+
"CU.Category_Id, "+
"CU.Status, "+
"Sum(MO.Charged_Points) as Charged_Points, "+
"Sum(MO.Total_Money) as Total_Money, "+
"Count(MO.id) as AmountTransWinner "+
"from Promotions_Winner WI "+
"join Customers CU "+
"on WI.Customer_id = CU.id "+
"join Personal_Info PI "+
"on CU.Personal_Info_Id = PI.id "+
"join Address AD "+
"on CU.Address_Id = AD.id "+
"join Countries CO "+
"on AD.country_id = CO.id "+
"join Campaigns CA "+
"on CU.Campaign_Id = CA.id "+
"join Movements MO "+
"on WI.Movement_Id = MO.id "+
"where WI.Promotion_Id = :pPromotionID "+
"group by "+
"WI.Customer_Id, CU.Card, CU.Fidely_Code, "+
"PI.Identity_Card, PI.Name, PI.Surname, PI.Gender, "+
"AD.Zip, AD.Geo_Lat, AD.Geo_Long, "+
"CO.City_Geo_Level, "+
"CU.Address_id, CA.Name, "+
"CU.Category_Id, "+
"CU.Status";