gpt4 book ai didi

java - 构建标准 API 查询以避免 MultipleBagFetchException

转载 作者:行者123 更新时间:2023-12-02 02:22:32 25 4
gpt4 key购买 nike

我们有一个 Lesson 实体,每个 Lesson 都有参加类(class)的学生和客人的列表:

public class Lesson {
@Id
private Long id;
// ...other properties
@OneToMany(mappedBy = "lesson", cascade = CascadeType.ALL)
private List<Student> students;
@OneToMany(mappedBy = "lesson", , cascade = CascadeType.ALL)
private List<Guest> guests;
// ...constructors, getters and setters
}
// --------------------------------------------------------------------------------------------
public class Student {
@Id
private Long id;
// ...
@ManyToOne
@JoinColumn(name = "lesson_id")
private Lesson lesson;
// ...
}
// -------------------------------------------------------------------------------------------
public class Guest {
@Id
private Long id;
// ...
@ManyToOne
@JoinColumn(name = "lesson_id")
private Lesson lesson;
// ...
}

我想获取所有包含学生和 guest 的类(class),因此我使用标准 API 构建了以下查询:

@Repository
public class LessonCriteriaRepositoryImpl implements LessonCriteriaRepository {

@PersistenceContext
private EntityManager em;

@Override
public List<Lesson> findAll() {
CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Lesson> criteriaQuery = builder.createQuery(Lesson.class);
Root<Lesson> lesson = criteriaQuery.from(Lesson.class);
lesson.fetch(Lesson_.students, JoinType.LEFT);
lesson.fetch(Lesson_.guests, JoinType.LEFT);
criteriaQuery.select(lesson).distinct(true);
TypedQuery<Lesson> query = em.createQuery(criteriaQuery);
return query.getResultList();
}
}

我得到了 MultipleBagFetchException,因为我无法一次获取多个集合。根据 Vlad Mihalcea(https://twitter.com/vlad_mihalcea)的帖子(Hibernate throws MultipleBagFetchException - cannot simultaneously fetch multiple bags),击败 MultipleBagFetchException 的正确方法是进行两个单独的查询并一个接一个地获取集合。

但我无法理解如何使用条件 API 构建这样的两个查询,一个接一个地获取集合。(我需要使用标准 API,因为我在这里给出了一些非常简化的代码作为示例,在实际应用程序中我有复杂的过滤器并且我使用许多谓词来构建查询)。

最佳答案

基于文章的推荐https://vladmihalcea.com/hibernate-multiplebagfetchexception/ ,我们可以像这样构建两个查询:

@Repository
public class LessonCriteriaRepositoryImpl implements LessonCriteriaRepository {

@PersistenceContext
private EntityManager entityManager;

public List<Lesson> findAll() {
//build first query for fetching students
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Lesson> criteriaQuery = builder.createQuery(Lesson.class);
Root<Lesson> lesson = criteriaQuery.from(Lesson.class);
lesson.fetch("students", JoinType.LEFT);
criteriaQuery.select(lesson).distinct(true);
TypedQuery<Lesson> query1 = entityManager.createQuery(criteriaQuery);
List<Lesson> lessons = query1.getResultList();

//build second query for fetching guests
builder = entityManager.getCriteriaBuilder();
criteriaQuery = builder.createQuery(Lesson.class);
lesson = criteriaQuery.from(Lesson.class);
lesson.fetch("guests", JoinType.LEFT);
criteriaQuery.select(lesson).distinct(true).where(lesson.in(lessons));
TypedQuery<Lesson> query2 = entityManager.createQuery(criteriaQuery);
return query2.getResultList();
}
}

关于java - 构建标准 API 查询以避免 MultipleBagFetchException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66196630/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com