用于3个表之间连接的Hibernate查询条件
我有一个SQL查询:
select * from A INNER JOIN B ON A.id = B.id INNER JOIN C ON B.id = C.id INNER JOIN D ON C.id = D.id where D.name = 'XYZ' and D.Sex = 'M'
我一直试图为上面的sql提供hibernate查询条件,但是遇到了问题。 任何人都可以帮忙吗?
Criteria c = session.createCriteria(A.class, "a"); .createAlias("ab", "b") .createAlias("bc", "c") .createAlias("cd", "d") .add(Restrictions.eq("d.sex", "M")) .add(Restrictions.eq("d.name", "XYZ"));
在您的问题上,您想要执行笛卡尔联接 ,但Criteria不支持此function,尽管您可以使用HQL执行此操作,如下所示。 这里有一个类似的问题
使用HQL查询,您可以执行以下操作:
select a from A a, B b, C c where a.id = b.id and c.id = b.id and d.id = c.id and d.name = 'XYZ' and d.sex = 'M'
该查询用于常规的hibernate查询:
Query query = session.createQuery(query); // <-- here you use the query above List results = query.list();
- 提供了错误类型hibernate的id
- 如何在Hibernate中映射一组枚举类型?
- 如何使用Hibernate / JPA2实现Spring Security用户/权限?
- 如何将嵌套SQL转换为HQL
- HiLo发电机策略不起作用
- NoClassDefFoundError:org / hibernate / annotations / common / reflection / MetadataProvider
- 如何使用hibernate条件实现使用内部联接对象选择查询
- 线程“main”中的exceptionorg.hibernate.HibernateException:访问stax流时出错
- Hibernate(JPA)inheritance抽象超类的映射