使用连接表创建HQL的麻烦(@ManyToMany)

时间:2014-06-29 16:15:07

标签: java hibernate many-to-many hql jointable

我正在尝试使用连接表生成HQL,但我没有。

以下是我的注释类(在此处发布之前删除不是有用的部分):

@Entity
@Table(name = "tab_usuarios")
public class Usuario {

    @ManyToMany(fetch=FetchType.LAZY, targetEntity=Cliente.class)
    @JoinTable(name = "tab_usuarios_clientes", joinColumns = { @JoinColumn(name = "fk_id_usuario") }, inverseJoinColumns = { @JoinColumn(name = "fk_id_cliente") })
    private List<Cliente> clientes;
}



@Entity
@Table(name = "tab_pontos")
public class Ponto {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "GENERIC_SEQ_GEN")
    private Integer id;

    @Column(name = "datahora")  
    private Date dataHora;

    @ManyToOne()
    @JoinColumn(name = "fk_id_usuario")
    private Usuario usuario;
}



@Entity
@Table(name = "tab_clientes")
public class Cliente {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "GENERIC_SEQ_GEN")
    @Column(name = "id")
    private Integer id;

    @Column(name = "nome")
    private String nome;
}

然后我做了以下SQL查询:

SELECT * FROM tab_pontos p
INNER JOIN tab_usuarios_clientes uc ON p.fk_id_usuario = uc.fk_id_usuario
INNER JOIN tab_clientes c ON uc.fk_id_cliente = c.id
INNER JOIN tab_usuarios u ON uc.fk_id_usuario = u.id
WHERE c.id = ? AND uc.fk_id_usuario = ?

我试图将它“翻译”为这个HQL:

SELECT p FROM Ponto p
INNER JOIN Usuario.clientes uc INNER JOIN Cliente c
INNER JOIN Usuario u
WHERE c = u.clientes.id
AND u.nome = ?
ORDER BY p.dataHora DESC

所以它给了我以下错误:

Caused by: org.hibernate.hql.ast.QuerySyntaxException: Invalid path: 'null.clientes' [SELECT p FROM br.mgx.ponto.dominio.Ponto p INNER JOIN Usuario.clientes uc INNER JOIN Cliente c INNER JOIN Usuario u WHERE c = Usuario.clientes.id AND u.nome = ? ORDER BY p.dataHora DESC]

我错过了什么?我只是无法找出我的HQL有什么问题。

提前致谢。

0 个答案:

没有答案