2012-06-21 38 views
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] 
+0

告诉我们Bondetal类 –

回答

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 documentation中解释。如果你如此着急,你应该阅读它而不是尝试随机查询。

+0

它的工作原理。你帮了我很多!我真的很感激它。 :)我试图从该文档中读取,但从那里我不太了解。无论如何,你是我的英雄! – alin