Hibernate Query, how to group different tables?
Im pretty new to databases, and programming in general, i tried everything but could not come up with a solution, my problem is: I have 2 tables in my database:
in my Cars table i have 3 columns: *modelID *carName *carsSold
and the Model table has 3 columns: *modelID *modelName *modelYear
Now my program has a button that says Group by Model, when the user clicks it has to show the information grouped by the Model, like:
Model ID : 1022
- Model Name: Audi
Car Name: A3
Cars sold: 120
Everything is ready, im just lost at the HQL query
It seems like you have some problems with your database schema.
What is the primary key for car? What is the mapping relationship between car and model?
The way it reads, you have two tables with info such as:
CAR modelID | carName | carsSold ---------------------------- 1022 | A3 | 120 MODEL modelID | modelName | modelYear ------------------------------- 1022 | Audi | 2001
This does not seem like a logical arrangement for the data you are trying to represent.
I'm not exactly sure what you are trying to accomplish. I'm also not sure that "group by" is what you are looking for.
If this is really your entire database structure, and you are not planning anything else, why not have one table? table SALE(id, modelName, carName, modelYear, carsSold) or something like that. With this table, the HQL is simply a select and then display the information from the model object.
Maybe if you clarified what the requirements are for your data I could give you some better direction on where to go with your database.