Hi,
1.Does anyone know how to recreate a left join query in HQL (the exact syntax using the following setup)?
2. and the necessary entries in the hibernate mapping files for each class
to make it work (using the following setup)?
Any help is greatly appreciated. Thank you in advance.
Using Hibernate: v3,
DB: MySQL 4.1
-------------------------------------------------------------------------------------------
The following HQL equivalent is needed:
SQL:
Code:
"SELECT g.name AS name, count(mg.movie_UID) as count,
FROM Genre g LEFT JOIN MovieGenre mg ON g.uid = mg.genre_UID
GROUP BY g.name;
"
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>
My question is what do I need to add to either of those mapping files and how should i formulate my HQL query
to make the qbove SQL equivalent run?* I would rather not add anything to the Movie file, as the assumption is that movies know nothing about the genres
(unless there is no other way of ahieving the requried result).
==========================================================
TRIED DOING:
==========================================================
I have tried the following HQL query:
"SELECT count(mg.genre) AS count, g
FROM Genre g LEFT JOIN MovieGenre mg
GROUP BY g;"
but hibernate complains that
***** ERROR: path expected for join!
***** ERROR: Invalid path: 'mg.genre'
org.hibernate.hql.ast.QuerySyntaxError: Path expected for join! [SELECT count(mg.genre) AS count, g FROM Genre g LEFT JOIN MovieGenre mg
GROUP BY g ASC]I know that the following HQL will work but it omits genres which have no entries in the MovieGenre Table so I need
the equivalent of SQL "LEFT JOIN ON"
Quote:
"SELECT count(mg.genre) AS count, g
FROM Genre g, MovieGenre mg
WHERE mg.theme = g
GROUP BY g;"
Any help is greatly appreciated.