JPQL内连接

时间:2013-04-07 16:44:42

标签: mysql database hql jpql

我正在尝试将mysql代码转换为JPQL。我有两张桌子。第一个表名为Product。它包含以下字段:id和code。第二个表称为Descriptor,它有3个字段:id(来自产品表),代码(来自产品表)和Description,它是特定于Descriptor表的字段。我想加入表格,以便为产品ID和产品代码的每个独特组合添加我想要添加的描述。

    The Sql is as follows ( this is correct): 
    Select Product.id, Product.code, Descriptor.DESCRIPTION
    from Product Inner join Descriptor where 
    Product.id = Descriptor.id and 
    Product.code = Descriptor.code

(The error is in here)
The JPQL I am trying is as follows which results in a "path" error
select p from Product p INNER JOIN Descriptor d where 
p.id = d.id and p.code = d.code;

非常欢迎所有建议,谢谢

1 个答案:

答案 0 :(得分:0)

阅读documentation about HQL。在HQL中,使用实体之间的关联来完成连接:

select p from Product p 
inner join p.descriptor d 
where p.code = d.code;

(假设产品和描述符之间存在OneToOne关联,使用各自的ID)。