0

这个查询,它曾经有效,现在不再有效。(我正在切换到注释,这是剩下的两个损坏的测试之一。)

我有三张桌子:

+---------------+
| 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();

以下是来自 Hibernate 的一些前面的 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 >();

我吹了什么?

4

1 回答 1

1

这可能是一个小的复制粘贴错误 - 您构造的查询是“查询”,但您执行的查询是“其他查询”?

但是,我认为问题在于 targetEntity 值。从规范:

作为关联目标的实体类。仅当使用 Java 泛型定义集合值关系属性时才可选。必须另外指定。

在您的情况下,您必须指定Competitor.class或忽略它。您必须将JoinTable注释与joinColumn和值一起指定为要搜索inverseJoinColumn的默认值is 。JoinTableCompetitor_Competition

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

如果您所做的只是查找这些值,我不确定为什么要执行这些更新语句。

于 2012-08-27T01:22:02.893 回答