It might look weird but the query works in HB. Here is the example I did:
A person can have one business car and can belong to multiple business areas:
Code:
<hibernate-mapping>
<class name="test.model.data.Person" table="Person" mutable="true" >
<id name="id" type="long" unsaved-value="null">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column name="Name"/>
</property>
<set name="areas" table="PersonArea">
<key column="PersonID"/>
<many-to-many column="AreaID" class="test.model.data.Area"/>
</set>
<many-to-one name="businessCar" column="BusinessCarID" class="test.model.data.Car"/>
</class>
</hibernate-mapping>
and for Area:
Code:
<hibernate-mapping>
<class name="test.model.data.Area" table="Area" mutable="true">
<id name="id" type="long" unsaved-value="null">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column name="Name"/>
</property>
</class>
</hibernate-mapping>
and for Car:
Code:
<hibernate-mapping>
<class name="test.model.data.Car" table="CAR" mutable="true" >
<id name="id" type="long" unsaved-value="null">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="name" type="string">
<column name="Name"/>
</property>
</class>
</hibernate-mapping>
and here is the test code:
Code:
public class Driver18
{
public static void main(String[] args)
{
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session session = sf.getCurrentSession();
session.beginTransaction();
Query query = session.createQuery("select distinct p.businessCar.id, p.businessCar.name, (select count(*) from p.areas ar where ar.name like '%First%') from test.model.data.Person p where p.businessCar.id = 1");
List<Object[]> results = query.list();
for (Object[] res : results)
{
print(res);
}
session.getTransaction().commit();
sf.close();
}
private static void print(Object[] res)
{
System.out.print(">>> [");
for (int i = 0; i < res.length; i++)
{
System.out.print(res[i]);
if (i < res.length - 1)
System.out.print(", ");
}
System.out.println("]");
}
}
and here is the generated sql:
Code:
select
distinct person0_.BusinessCarID as col_0_0_,
car1_.Name as col_1_0_,
(select
count(*)
from
PersonArea areas2_,
Area area3_
where
person0_.ID=areas2_.PersonID
and areas2_.AreaID=area3_.ID
and (
area3_.Name like '%First%'
)) as col_2_0_
from
Person person0_,
CAR car1_
where
person0_.BusinessCarID=car1_.ID
and person0_.BusinessCarID=1
I am testing this against SQL server. I am wondering if the difference could be the dialect. See if you see the dialect class in the stack trace of the exception you get. It might be simply your db does not support this kind of things. Also try to switch to another database to see if the error goes away. I am using the same hibernate version by the way.
Ah I was going to forget. I don't get your comments mapping. A <one-to-many> with a join table is not usually like that, as per my understanding. I do believe that table attribute is going to be ignored but I doubt if it will result in the error you get. That should give you SQL error when it talks to db.
Farzad-