我是 Hibernate 的初学者,我不想弄清楚一些机制。 我有实体:
@Entity
@Table(name = "dish")
public class Dish implements Serializable {
@ManyToMany(fetch = FetchType.LAZY)
private List<Ingredient> ingredients;
@ManyToOne(fetch = FetchType.LAZY)
private Category category;
}
以及具有这种方法的存储库:
@Query("select d from Dish d join fetch d.ingredients")
Set<Dish> getDishesWithIngredientsAndCategory();
我注意到,我通过这种方法仅检索具有相关成分的菜肴。我不知道如何获得所有菜肴,即使没有配料? 第二个问题是:是否可以在一个 @Query 中合并获取两列?像这样的东西:
@Query("select d from Dish d join fetch d.ingredients, d.category")
我尝试使用此类查询,但收到QuerySelectionException:“d.category 未映射”。
最佳答案
That I'm retrieving by this method only Dishes, that have associated ingredients.
使用Left Join
而不是join:@Query("select d from Dish d left join fetch d.ingredients")
And second question is: is it possible to combine in one @Query fetch two columns? You can try this:
@Query("从 Dish d 中选择 d join fetch d.ingredients join fetch d.category")
关于java - Spring data jpa未选择所有记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51180978/