所以我花了几个小时尝试为多对多关系编写HQL查询。有了我试过的所有查询,即使在阅读了hibernate文档之后也没有用。有人可以帮助我吗?提前谢谢。
我尝试使用或不使用fetch进行内连接,连接和外连接。我认为这个问题可能与香水类中的一对多关系有关。
@Query("FROM Category c INNER JOIN FETCH c.fragrances f WITH c.referencedId = 1")
List<Category> getCatalog();
没有get / setters的类别类
@Entity
public class Category extends AbstractEntity {
@Column(name = "name", nullable = false)
private String name;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "Category_has_Fragrance",
joinColumns = @JoinColumn(name = "category_id"),
inverseJoinColumns = @JoinColumn(name = "fragrance_id")
)
@OrderBy("name")
private List<Fragrance> fragrances;
@Column(name = "referenced_id", nullable = false)
private int referencedId;
]
没有get / setters的香水类
@Entity
public class Fragrance extends AbstractEntity {
@Column(name = "name", nullable = false)
private String name;
@Column(name = "description", nullable = false)
private String description;
@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "fragrances")
private Set<Category> categories;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "fragrance")
private List<FragrancedProduct> fragrancedProducts;
@Column(name = "image_path")
private String imagePath;
}
答案 0 :(得分:1)
试试这个:
select c from Category c join c.fragrances f where c.referencedId = :id
您可以在此处查看类似的问题:How do I do with HQL, many to many?