Hi there. I'm having what I hope is a very simple problem, but
I can't figure out the right hql syntax.
I'm trying to use count in a many-to-many relationship.
The problem is I can't figure out the right HQL syntax to name
the mapping table.
I have a many-to-many relationship between Companies and Categories.
There is the relevent set defintion:
Code:
<set name="categories" lazy="true" table="category_company" cascade="all" >
<key column="company_id" />
<many-to-many column="category_id" class="Category"
outer-join="true"
>
<column name="category_id" index="in_category_company_category" />
</many-to-many>
</set>
I'm using the automatic schema generator in hbm2ddl; it generated this mapping table:
Code:
create table category_company (
company_id BIGINT not null,
category_id BIGINT not null,
primary key (company_id, category_id)
);
So far, so good.
I'm trying to count how many companies are in each category. This
sql does what I want, when run directly against the database:
Code:
select count(*), cc.category_id
from company c, category_company cc
where c.id = cc.company_id
group by cc.category_id;
I tried running this find code:
Code:
list = session.find("select count(*), cc.category_id from Company c, company_category cc where c.id = cc.company_id group by cc.category_id");
I get a QueryException: in expected: cc
From the code it seems to want an "in" token instead of cc,
so I'm obvously confused as to the proper syntax.
So, here's my question: how do I name the mapping
table for doing HQL operations?
Thanks in advance...
(This is using hibernate 2.1.2, and mysql 4.0.16)