2012-08-27 210 views
0

此查詢不再用於工作。 (我切換到註釋,這是兩個破剩餘試驗之一。)帶連接表的休眠HQL

我有三個表:

+---------------+ 
| Competition | 
+---------------+ 
| competitionId | 
| . . .   | 
+---------------+ 

+---------------+ 
| Competitor | 
+---------------+ 
| competitorId | 
| . . .   | 
+---------------+ 

+------------------------+ 
| CompetitionCompetitors | 
+------------------------+ 
| competitionId   | 
| competitorId   | 
+------------------------+ 

和代碼/查詢使用工作是

Query query = session.createQuery(
    "select c.id from Competition c join c.competitors co where co.id = :competitorId"); 
query.setParameter("competitorId", competitor.getCompetitorId()); 
List list = query.list(); 

下面是一些前面的SQL日誌記錄從休眠到來,再加上錯誤信息:

Hibernate: update db.competition set competitorId=? where competitionId=? 
Hibernate: update db.competition set competitorId=? where competitionId=? 
Hibernate: update db.competition set competitorId=? where competitionId=? 
17:12:49,232 WARN JDBCExceptionReporter:233 - SQL Error: 1054, SQLState: 42S22 
17:12:49,240 ERROR JDBCExceptionReporter:234 - Unknown column 'competitorId' in 'field list' 

(得承認,我很驚訝地看到發生在一個查詢更新)。

這裏的註釋在競爭:

@OneToMany(targetEntity = Competition.class) 
@JoinColumn(name = "competitorId") 
private Set<Competitor> competitors = new TreeSet<Competitor>(); 

我是怎麼了效果也很糟糕?

回答

1

這可能是次要的複製粘貼錯誤 - 您構建的查詢是'查詢',但是您執行的查詢是'otherQuery'?

但是,我相信的問題是在目標實體價值。從規格:

作爲關聯的目標的實體類。僅當使用Java泛型定義集合值關係屬性時纔可選。必須另外指定。

在您的情況下,您必須指定Competitor.class或將其忽略。您必須指定JoinTable註釋以及joinColumninverseJoinColumn值作爲將要搜索的默認JoinTableCompetitor_Competition

@OneToMany 
@JoinTable(name = "CompetitionCompetitors", inverseJoinColumns={@JoinColumn(name="competitorId")}, joinColumns={@JoinColumn(name="competitionid")} 
private Set<Competitor> competitors = new TreeSet<Competitor>(); 

我不知道爲什麼這些更新語句正在執行,如果你所做的只是查看這些值。

+0

非常瞭解映射和JPA。謝謝!這使查詢工作。 – Marvo