用于根据外键过滤 table 的 NamedQuery

NamedQuery for filtering a table based on its foreign key

我有下面两个表

@Entity
@Table(name = "userRequests")
@NamedQueries({

    @NamedQuery(
            name = "User.getByFK",
            query = "SELECT  u FROM User u WHERE u.FK_ID =:FK_ID")
})


public class UserR implements Comparable<User>, Serializable {

    @Id
    @JsonProperty
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    long id;

    @JsonProperty
    @OneToOne(cascade= CascadeType.ALL)
    @JoinColumn(name="FK_ID")
    FK fk;
}

这给我一个错误"could not resolve property: FK_ID"

如何使该字段可访问?

在用户对象中创建 FK_ID 文件时出现重复映射异常

您收到该错误是因为用户 class 中没有字段名称 FK_ID 您将需要使用连接 fk

获取文件

试试这样用

 @NamedQueries({
    @NamedQuery(
            name = "User.getByFK",
            query = "SELECT  u FROM User u WHERE u.fk.FK_ID =:FK_ID")
})