如何从多到许多关系的参考方面检索数据?

发布于 2025-01-19 13:37:13 字数 1315 浏览 0 评论 0原文

我有一个名为“问题”的实体课程,它与考试类有关系。为了允许用户访问属于特定考试ID的问题列表,我试图使用内部联接来检索数据。但是,我的尝试失败了。我有疑问要和你们澄清。

我的问题

  1. 是否可以通过使用内部联接的查询来检索数据的参考端?

我尝试使用问题存储库来检索数据

public interface QuestionRepository  extends JpaRepository<Question,Long> {
    @Query("SELECT q FROM Question q INNER JOIN q.exam_added_question eq WHERE eq.exam_id = :id")
    List<Question> findQuestionListByExamId(Long id);
}

quesiton

public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToMany(mappedBy = "addedQuestion") 
    private List<Exam> addedToExam = new ArrayList<Exam>();
}

public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToMany
    @JoinTable(
            name = "exam_added_question",
            joinColumns = @JoinColumn(name="exam_id"),
            inverseJoinColumns = @JoinColumn(name = "question_id")
    )
    private List<Question> addedQuestion = new ArrayList<Question>();
}

I have a entity class named Question and it have the relationship with the exam class. In order to allow the user access to the list of question that belonged to the particular exam id, I have tried to use the inner join to retrieve the data. However, my attempt is failed. And I have some doubt to clarify with you guys.

My question

  1. Is the referencing side of the many-to-many relationship allow to retrieve the data with the query that use the inner join?

My attempt to retrieve data using the question repository

public interface QuestionRepository  extends JpaRepository<Question,Long> {
    @Query("SELECT q FROM Question q INNER JOIN q.exam_added_question eq WHERE eq.exam_id = :id")
    List<Question> findQuestionListByExamId(Long id);
}

Quesiton class

public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToMany(mappedBy = "addedQuestion") 
    private List<Exam> addedToExam = new ArrayList<Exam>();
}

Exam Class

public class Question {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @ManyToMany
    @JoinTable(
            name = "exam_added_question",
            joinColumns = @JoinColumn(name="exam_id"),
            inverseJoinColumns = @JoinColumn(name = "question_id")
    )
    private List<Question> addedQuestion = new ArrayList<Question>();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

稀香 2025-01-26 13:37:13

我认为问题在于您如何在类中命名字段并在查询中使用它。检查其合规性。

首先查看您的类和字段的名称。 Exam.class 在哪里,名为 exam_added_question 的字段是什么?另外,类中 id 字段的名称是 id,而不是 exam_id
尝试在查询中将 eq.exam_id 更改为 eq.id,也许这个解决方案会有所帮助。在JPQL(HQL)中,我们使用类的字段名称(实体字段)进行操作,而不是使用数据库表中的列。

@Query("SELECT q FROM Question q INNER JOIN q.addedToExam eq WHERE eq.id = :id")
List<Question> findQuestionListByExamId(@Param("id") Long id);

另一种方式:

@Query("select q from Question q join Exam eq where eq.id = :id")
 List<Question> findQuestionListByExamId(@Param("id") Long id);

INNER关键字是可选的(即INNER JOIN相当于JOIN)。

或者按 ID 选择您的问题。 findById() 在您的服务 bean 中。

@Autowired 
QuestionRepository questionRepository;

Question question = questionRepository.findById(ID);

然后在单个事务中访问相关实体的集合。 Hibernate 将为您做一切。

List<Exam> examList = question.getAddedToExam();

下一步 - 过滤您的考试列出您想要的内容。只需记住,您需要在同一个事务中遍历集合,因为多对多关系默认具有惰性策略。否则,您可能会收到延迟初始化异常。

I think the problem is how you named your fields in the classes and using it in the query. check it compliance.

First of All look at name of your classes and fields. Where is Exam.class, what is the field named exam_added_question? And Also what named your id field in classes it's id, not exam_id
Try to change eq.exam_id to eq.id in the query, Perhaps this solution will help. In JPQL(HQL), we operate with the names of the fields of the classes(Entity fields), and not with the columns in the database table.

@Query("SELECT q FROM Question q INNER JOIN q.addedToExam eq WHERE eq.id = :id")
List<Question> findQuestionListByExamId(@Param("id") Long id);

Another way:

@Query("select q from Question q join Exam eq where eq.id = :id")
 List<Question> findQuestionListByExamId(@Param("id") Long id);

The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).

Or select your Question by id. findById() in yours service bean.

@Autowired 
QuestionRepository questionRepository;

Question question = questionRepository.findById(ID);

Then access the collection of the related entity within a single transaction. Hibernate will do everything for you.

List<Exam> examList = question.getAddedToExam();

Next step - filtering your examList what you want. Just remember that you need to traverse the collection in the same transaction, because many-to-many relationships have a Lazy strategy by default. Otherwise, you may get a Lazy Initialization Exeption.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文