在多个方面定义联接表,但不要在一侧再次定义它。这将创建映射到同一表的两个单向关联,而不是一个双向关联。
双向关联始终具有所有者侧(您可以在其中指定要使用的连接列或连接表),以及通过使用mapledBy属性可以说是另一侧的反向的反向侧:
public class Activity { @ManyToOne // owner side: it doesn't have mappedBy, and can decide how the association is mapped: with a join table @JoinTable(name="category_activity", joinColumns={@JoinColumn(name="activities_id")}, inverseJoinColumns={@JoinColumn(name="Category_id")}) private Category category;}public class Category { @oneToMany(mappedBy = "category") // inverse side: it has a mappedBy attribute, and can't decide how the association is mapped, since the other side already decided it. @Fetch(FetchMode.JOIN) @JsonIgnore private Collection<Activity> activities;}编辑:
此外,您的查询应仅通过添加select子句来选择活动,而不是查询所连接的所有实体:
select a from Activity as a where a.category.id= :categoryId order by a.key



