Ok, I will post some code because the HQL didn't work but it seems right, maybe it's and SQL generation problem.
My test classes:
public class A {
private int id;
private String name;
private Set as;
...
}
public class B extends A {
private String strB;
...
}
public class C extends A {
private String strC;
...
}
My mappings:
<hibernate-mapping package="test">
<class name="A">
<id name="id">
<generator class="native"/>
</id>
<property name="name" not-null="true"/>
<set name="as" table="COMPOE" lazy="true" cascade="all">
<key/>
<many-to-many class="A"/>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="test">
<joined-subclass name="B" extends="test.A">
<key/>
<property name="strB" not-null="true"/>
</joined-subclass>
</hibernate-mapping>
<hibernate-mapping package="test">
<joined-subclass name="C" extends="test.A">
<key/>
<property name="strC" not-null="true"/>
</joined-subclass>
</hibernate-mapping>
I populated my DB using the following code:
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
B parent = new B();
parent.setName("parent name");
parent.setStrB("parent str");
for (int i = 0; i < 10; i++) {
B child = new B();
child.setName("b name");
child.setStrB("b str");
session.save(child);
parent.getAs().add(child);
}
for (int i = 0; i < 10; i++) {
C child = new C();
child.setName("c name");
child.setStrC("c str");
session.save(child);
parent.getAs().add(child);
}
session.save(parent);
tx.commit();
And test code:
Session session = sessions.openSession();
List list = session.createQuery("from A").list();
for (int i = 0; i < list.size(); i++) {
A a = (A) list.get(i);
Set childs = a.getAs();
if (childs.size() == 0) { continue; }
Query q = session.createQuery("select c from C c, A a where c in elements(a.as) and a.id = " + a.getId());
List cs = q.list();
System.out.println(cs.size());
}
The println should return 10, the number of Cs.
This is the SQL generated:
select c0_.C as id, c0_.strC as strC3_, c0__1_.name as name0_ from C c0_ inner join A c0__1_ on c0_.C=c0__1_.id, A a1_ where (c0_.C in(select as2_.elt from COMPOE as2_ where a1_.id=as2_.id))and(a1_.id=22 )
And I get the following error message:
13:54:00,630 WARN JDBCExceptionReporter:38 - SQL Error: 1064, SQLState: 42000
13:54:00,630 ERROR JDBCExceptionReporter:46 - Syntax error or access violation, message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select as2_.elt from COMPOE as2_ where a1_.id=as2_.id))and(a1_."
13:54:00,690 WARN JDBCExceptionReporter:38 - SQL Error: 1064, SQLState: 42000
13:54:00,690 ERROR JDBCExceptionReporter:46 - Syntax error or access violation, message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select as2_.elt from COMPOE as2_ where a1_.id=as2_.id))and(a1_."
13:54:00,690 ERROR JDBCExceptionReporter:38 - Could not execute query
java.sql.SQLException: Syntax error or access violation, message from server: "You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'select as2_.elt from COMPOE as2_ where a1_.id=as2_.id))and(a1_."
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:1876)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1098)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1192)
Any thought on this one?
|