这个查询:
Query query = HibernateUtil.getSessionFactory().getCurrentSession().createQuery("SELECT substring(a.account, 1, 6), a.ref, b.event_id, substring(a.admin, 1, :adminLength) FROM Parent a, Child b WHERE b.joinString = a.joinString AND b.event_id=:eventId AND substring(a.admin, 1, :adminLength) LIKE :admin AND a.date BETWEEN :from AND :to");
生成可在 MySQL 中运行的 SQL 语句。
但是,当我循环遍历 Child List
我得到 java.lang.ClassCastException
在线for (int j = 0; j < allParent.get(i).getAllChild().size(); j++) {
这个循环的:
for (int i = 0; i < allParent.size(); i++) {
System.out.println(allParent.get(i));
for (int j = 0; j < allParent.get(i).getAllChild().size(); j++) {
System.out.println("Child: " + allParent.get(i).getAllChild().get(j).getID());
}
HibernateUtil.getSessionFactory().getCurrentSession().flush();
HibernateUtil.getSessionFactory().getCurrentSession().clear();
}
Parent.hbm.xml
<bag name="allChild" table="child" inverse="true" lazy="true" fetch="select">
<key property-ref="surname">
<column name="surname" not-null="true" />
</key>
<one-to-many class="com.test.Child" />
</bag>
有没有一种方法可以使用 Criteria
运行此语句? (或其他东西)代替并让它同时填充 allParent
List
和 allChild
List
链接到每个 Parent
在 allParent
内?
最佳答案
在没有看到模型类的情况下,我最好的猜测是您想将“AND child_id = :childId”替换为“AND child.id = :childId”,或者您的子类中标记的任何 id 列。
关于java - HQL 从多个表中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21263170/