我猜测下面的查询如何通过JPQL编写:
选择最后一个孩子的所有父母(1-n关系的第1侧) (最新的id)具有列(属性)的特定值。我的实体看起来像
Parent.java
@Entity
class Parent {
@Id
@Column(name="ID_PARENT")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy="parent")
private List<Child> children;
}
Child.java
@Entity
class Child {
@Id
@Column(name="ID_CHILD")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne()
@JoinColumn(name="ID_PARENT")
private Parent parent;
private String attribute;
}
JPQL会是什么样子? 谢谢大家
答案 0 :(得分:0)
它与SQL不应该有很大不同。我在这里使用类似的DB工作。
SELECT *
FROM Parent p
INNER JOIN Child c
ON c.ID_PARENT = p.ID_PARENT
WHERE c.ID_CHILD =
(SELECT c2.ID_CHILD
FROM Child c2
WHERE c.ID_PARENT = c2.ID_PARENT
AND c2.attribute = <parameter>
AND c2.ID_CHILD =
(SELECT MAX(c3.ID_CHILD)
FROM apuracao_fator_risco c3
WHERE c2.ID_PARENT = c3.ID_PARENT
)
)
这些帖子也很有用:
JPA Select latest instance for each item
JPA sorting query by property of last element in child collection