HQL 无法在公共外键列上连接两个表

Posted

技术标签:

【中文标题】HQL 无法在公共外键列上连接两个表【英文标题】:HQL fails on joining two tables on a common foreign key column 【发布时间】:2015-12-23 11:27:26 【问题描述】:

我有两个实体

@Entity
@Table(name="one")
public class One implements Serializable 
  @Id
    @NotNull
    @Column(name = "id")
    private String id;

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

    @Column(name = "title")
    private String title;

    @ManyToOne
    @JoinColumn(name = "three_id")
    private Three three;


@Entity
@Table(name="two")
public class Two implements Serializable 
    @Id
    @NotNull
    @Column(name = "id")
    private String id;

     @Column(name = "order")
    private String order;

    @ManyToOne
    @JoinColumn(name = "three_id")
    private Three three;


@Entity
@Table(name="three")
public class Three implements Serializable 
  @Id
    @NotNull
    @Column(name = "id")
    private String id;

     @Column(name = "quantity")
    private String quantity;

一个命名查询为 select o.name from one o, two tw, three th where tw.order="test" and o.three.id = tw.three.id;

部署此org.hibernate.HibernateException: Errors in named queries时,Jboss 抛出以下错误

目的是我想要基于公共列three_id 的两个表中的行,该列是表“三”中的 id 字段 为什么会失败,我该如何解决。

【问题讨论】:

【参考方案1】:

好吧,您似乎没有在查询中的任何地方使用 th,这可能是问题所在。

这应该可行:

从一个o,两个tw中选择o.name,其中tw.order="test" and o.three.id = tw.three.id;

【讨论】:

以上是关于HQL 无法在公共外键列上连接两个表的主要内容,如果未能解决你的问题,请参考以下文章