JPQL pour interroger l'entité connectée à un autre par @onetomany
Question
A:
class Container {
@Id
@Column(name="id")
protected long id;
@OneToMany
@JoinColumn(name="container_id", nullable=false)
protected Collection<Content> contents = new ArrayList<Content>();
}
et
class Content {
@Id
@Column(name="id")
protected long id;
@Column(name="link_id")
protected long linkId;
}
Quelle requête JPQL obtiendra des entités de contenu qui sont dans un conteneur avec certains ID et avec certains linkID?
La solution
Sans faire de l'association bidirectionnelle, vous pourriez faire:
SELECT c
FROM Content c, Container container
WHERE c MEMBER OF container.contents AND c.linkId = :linkId AND container.id = :containerId
Mais il serait plus facile de rendre l'association bidirectionnelle:
SELECT c from Content c
WHERE c.container.id = :containerId AND c.linkId = :linkId
Et le SQL généré est meilleur (et plus efficace).
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow