HQL查询:'where子句'中的未知列'id'

时间:2014-10-21 13:25:25

标签: hibernate hql alias

我有以下两个对象:

@Entity
@Table(name = "B")
@NamedQueries({
@NamedQuery(name = "A.searchById", query = "SELECT myCollection FROM A WHERE id = :id"),
... })
public class B{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "B_ID")
    int id;

    @Column(name = "NAME")
    String name;
}

@Entity
@Table(name = "A")
@NamedQueries({
@NamedQuery({several named queries...})
public class A{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "A_ID")
    int id;

    @Column(name = "NAME")
    String name;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "MAPPING_B_A", joinColumns = { @JoinColumn(name = "A_ID") },    inverseJoinColumns = { @JoinColumn(name = "B_ID") })
    List<B> myCollection;
}

我发出以下查询:

SELECT myCollection FROM A WHERE id = :id

Hibernate返回:未知列&#39; id&#39;在&#39; where子句&#39;

我将查询改为

SELECT myCollection FROM A as a WHERE a.id = :id

一切正常。但是第一个查询的问题是什么?好的,两个类中都有一个id列,但FROM子句中只有一个类!我还尝试在其中一个类中更改id字段的名称,但没有帮助。

1 个答案:

答案 0 :(得分:0)

根据documentation,它应该没有别名

from Cat where name='Fritz'

我想到的另一件事是id是保留字,导致问题。您可以尝试使用其他文件,以检查是否属实。