无法解析 @OneToMany 子 table 的 属性 (@JoinColumn)

Could not resolve property of @OneToMany child table (@JoinColumn)

我在尝试 运行 自定义查询时遇到上述错误。我知道使用休眠时,您需要映射到 entity 名称(而不是 column 名称)。但是,在 @OneToMany 的情况下,我没有子项中的列。让我用一个简单的例子来解释(我已经删除了所有其他列和方法):

@Query("SELECT ch.randomColumnHere FROM Parent pa INNER JOIN Child ch ON pa.id = ch.parent_id")

Parent.class

@Entity(name="Parent")
@Table(name="parent")
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@NoArgsConstructor
@Setter
@Getter
public class Parent {

    @Id
    @SequenceGenerator(name = "parent_id_seq", sequenceName = "parent_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "parent_id_seq")
    private Long id;
    @OneToMany (cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "parent_id", nullable = false, updatable = false)
    private List<Child> children;

}

Child.class

@Entity(name="Child")
@Table(name="child")
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@NoArgsConstructor
@Setter
@Getter
public class Child {

    @Id
    @SequenceGenerator(name = "child_id_seq", sequenceName = "child_id_seq", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "child_id_seq")
    private Long id;

    @Column(name="randomcolumnhere")
    private Double randomColumnHere;
}

我得到以下异常:

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: could not resolve property: parent_id

我了解到 Child entity 中没有名为 parent_idfield。我该如何解决这个问题?由于 parent_id 列的唯一 'reference' 是 Parent class 的 @JoinColumn

有什么建议吗?

在子 class 中添加关系如下,并相应地更改查询。

    @ManyToOne
    private Parent p;

    @Query("SELECT ch.randomColumnHere FROM Parent pa INNER JOIN Child ch ON pa.id = ch.p.id")

HQL 查询使用实体及其关联。关联是否使用连接 table 的事实对于 HQL 并不重要:您在关联中导航,Hibernate 将适当的转换为 SQL.

SELECT ch.randomColumnHere FROM Parent pa INNER JOIN pa.children;