Hibernate 如何为多对多关联编写HQL查询
所以我花了几个小时试图为多对多关系编写一个HQL查询。对于我尝试过的所有查询,即使在阅读hibernate文档之后,也没有任何效果。有人能帮我吗?先谢谢你 我已经尝试过内部连接、连接和外部连接(带或不带提取)。我认为这个问题可能与香水类中的一对多关系有关Hibernate 如何为多对多关联编写HQL查询,hibernate,hql,Hibernate,Hql,所以我花了几个小时试图为多对多关系编写一个HQL查询。对于我尝试过的所有查询,即使在阅读hibernate文档之后,也没有任何效果。有人能帮我吗?先谢谢你 我已经尝试过内部连接、连接和外部连接(带或不带提取)。我认为这个问题可能与香水类中的一对多关系有关 @Query("FROM Category c INNER JOIN FETCH c.fragrances f WITH c.referencedId = 1") List<Category> getCatalog(); 不带ge
@Query("FROM Category c INNER JOIN FETCH c.fragrances f WITH c.referencedId = 1")
List<Category> getCatalog();
不带get/setter的Category类
@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;
]
@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;
}
香水类不含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;
]
@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;
}
试试这个:
select c from Category c join c.fragrances f where c.referencedId = :id
你可以在这里检查一个类似的问题:可能是重复的谢谢,我昨晚可能遇到了一些可能有用的问题。你的询问帮助我找到了根本问题。问题是,我有一个名为Set的表名,在我拥有的所有关系中,hibernate试图从表集中进行选择。我再也不能在列或表名中使用关键字了