当我有文件夹:id和记录:refId(命名参数)时,如果文件夹中存在记录,我将如何创建 JPQL 查询来获取记录。我希望从查询中返回记录,因为需要记录的其他数据。
以下是实体:
@Entity
public class Folder {
@Id
@Column(name = "id")
private Long id;
@OneToMany(targetEntity = Record.class, cascade = { CascadeType.ALL })
@JoinColumn(name = "RECORD_FOLDER_0")
@OrderBy("id ASC")
private List<Record> records;
...
}
@Entity
public class Record {
@Id
@Column(name = "id")
private Long id;
@Basic
@Column(name = "refId")
private Long refId;
@Basic
@Column(name = "data", length = 255)
private String data;
...
}
常规 SQL 看起来像:
.createNativeQuery(String.format("SELECT * FROM record r WHERE r.refId=%d AND r.RECORD_FOLDER_0=%d", refId, folderId));
我正在努力研究如何使用 JPQL 查询来做到这一点。