Hi,
I am having a difficulty sorting the resultset by count.
Any ideas/solutions would be greatly appreciated.
I've had a look around this forum, google, the hibernate reference docos and some books including "java persistence with hibernate".
So far no luck,
Using Hibernate: v3.0.5
DB: MySQL 4.1
What I need is to return each Category and the number of movies it is applied to.
something like
"select count(mg.genre), g
from MovieGenre mg RIGHT OUTER JOIN mg.genre g
group by g
order by count(mg.genre)
"
What this gives is
- General error, message from server: "Invalid use of group function"
org.hibernate.exception.GenericJDBCException: could not execute query
According to
http://www.hibernate.org/hib_docs/refer ... ryhql.html
The following query should be possible:
Code:
select cat
from eg.Cat cat
join cat.kittens kitten
group by cat
having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc
Also, I cannot do this in SQL or return the entire result set and then sort it, because of the framework and security requirememnts for the project.
I can succesfuly execute the following query:
"select count(mg.genre), g
from MovieGenre mg RIGHT OUTER JOIN mg.genre g
group by g
order by g.name"
but this does not seem to work for count() also
using an alias for a count, which is the SQL way
does not seem to work at all.
-------------------------------------------------------------------------------------------
The setup is this:
==========================================================
CLASSES:
==========================================================There are three classes
Movie, Genre, and MovieGenre
for simplicity say, (the actual entities are quiet different but the principle is the same)
Code:
class Genre() {
long UID;
String name;
}
class MovieGenre () {
long UID;
Genre genre;
Movie movie;
}
class Movie() {
long UID;
String title;
}
* A Movie knows nothing about its Genre(s) and it should not.
* A Movie can be tagged with a Genre. Each time a Movie is tagged an entry is created
in the MovieGenre table.
* A Movie can be tagged with multiple Genres.
TABLES:
==========================================================For the following tables UID (Unique ID) is the primary key.
For the MovieGenre table assume that the tuple {movie_UID, genre_UID} is unique
Quote:
------------------------------------------------
| MovieGenre |
------------------------------------------------
| UID | movie_UID | genre_UID |
------------------------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 2 | 2 |
| 5 | 2 | 4 |
------------------------------------------------
------------------------
| Genre |
------------------------
| UID | name |
------------------------
| 1 | Thriller |
| 2 | Detective |
| 3 | Drama |
| 4 | Horror |
------------------------
--------------------------------
| Movie |
--------------------------------
| UID | title |
--------------------------------
| 1 | movieA |
| 2 | movieB |
| 3 | movieC |
| 4 | movieD |
| 5 | movieE |
--------------------------------
RESULT NEEDED:
==========================================================What I need is for each Genre to have a count of how many movies are under that Genre
For example, if I execute the following simple SQL query:
Code:
"SELECT g.name AS genre, count(mg.movie_UID) as count,
FROM Genre g LEFT JOIN MovieGenre mg ON g.uid = mg.genre_UID
GROUP BY genre ORDER BY count;"
The result will be
Quote:
---------------------------------------
| Genre | count |
---------------------------------------
| Thriller | 3 |
| Drama | 1 |
| Horror | 1 |
| Detective | 0 |
---------------------------------------
HIBERNATE MAPPINGS:
==========================================================I have the following Hibernate mapping classes:
1. MovieGenre.hbm.xml:
Code:
<class name="MovieGenre" table="MovieGenre" lazy="true">
<id name="UID" type="long" unsaved-value="0">
<generator class="native">
</id>
<many-to-one name="movie" access="field" column="movie_UID" class="Movie"/>
<many-to-one name="genre" access="field" column="genre_UID" class="Genre"/>
</class>
2. Genre.hbm.xml:
Code:
<class name="Genre" table="Genre" lazy="true">
<id name="UID" type="long" unsaved-value="0">
<generator class="native">
</id>
<property name="name" column="name" type="string" not-null="true"/>
</class>
3. Movie.hbm.xml:
Code:
<class name="Movie" table="Movie" lazy="true">
<id name="UID" type="long" unsaved-value="0">
<generator class="native">
</id>
<property name="title" column="title" type="string" not-null="true"/>
</class>