0

我在两个表之间进行了内部连接,但它不起作用。如果有人能尽快帮助我,这将对我有很大帮助。

提前致谢

它看起来像这样:

List<Bondetal> bondetals = session.createQuery("from Bondetal bd inner join bd.Bon b where bd.idbon = b.idbon and idprodus = " + idprodus +" and Bon.suma >=" + suma).list();

我收到此错误:

Exception in thread "AWT-EventQueue-0" org.hibernate.QueryException: could not resolve property: Bon of: sakila.entity.Bondetal [from sakila.entity.Bondetal bd inner join bd.Bon b where bd.idbon = b.idbon and idprodus = 2 and Bon.suma >=1]
4

1 回答 1

0

为了能够进行连接,您需要实体之间的关联。因此,BonDetal 实体应该有类似的东西

@ManyToOne
@JoinColumn(name = "idbon")
private Bon bon;

它不能有任何idbon属性,因为 idbon 列是由关联映射的。

并且查询不需要任何where bd.idbon = b.idbon子句,因为 Hibernate 知道实体是如何相互关联的。因此,查询应该是:

select bd from Bondetal bd 
inner join bd.bon b 
where bd.idprodus = :idprodus 
and b.suma >= :suma

您还应该使用命名参数而不是将值连接到查询中,以避免 SQL 注入攻击和转义问题。

所有这些都在Hibernate 文档中进行了解释。如果你这么着急,你应该阅读它而不是尝试随机查询。

于 2012-06-21T21:34:54.970 回答