如何从多到许多关系的参考方面检索数据?
我有一个名为“问题”的实体课程,它与考试类有关系。为了允许用户访问属于特定考试ID的问题列表,我试图使用内部联接来检索数据。但是,我的尝试失败了。我有疑问要和你们澄清。
我的问题
- 是否可以通过使用内部联接的查询来检索数据的参考端?
我尝试使用问题存储库来检索数据
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
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于您如何在类中命名字段并在查询中使用它。检查其合规性。
首先查看您的类和字段的名称。 Exam.class 在哪里,名为 exam_added_question 的字段是什么?另外,类中 id 字段的名称是 id,而不是 exam_id
尝试在查询中将 eq.exam_id 更改为 eq.id,也许这个解决方案会有所帮助。在JPQL(HQL)中,我们使用类的字段名称(实体字段)进行操作,而不是使用数据库表中的列。
另一种方式:
INNER关键字是可选的(即INNER JOIN相当于JOIN)。
或者按 ID 选择您的问题。 findById() 在您的服务 bean 中。
然后在单个事务中访问相关实体的集合。 Hibernate 将为您做一切。
下一步 - 过滤您的考试列出您想要的内容。只需记住,您需要在同一个事务中遍历集合,因为多对多关系默认具有惰性策略。否则,您可能会收到延迟初始化异常。
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.
Another way:
The INNER keyword is optional (i.e. INNER JOIN is equivalent to JOIN).
Or select your Question by id. findById() in yours service bean.
Then access the collection of the related entity within a single transaction. Hibernate will do everything for you.
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.