简体   繁体   English

Spring JPA乘以复杂关系M:N和1:N的嵌套表

[英]Spring JPA multiply nested tables with complicated relationship M:N and 1:N

I am trying to build a simple API which provides some data to the frontend.我正在尝试构建一个简单的 API,它为前端提供一些数据。

I am struggling with database and association to the entities in Spring.我正在努力处理数据库和与 Spring 中实体的关联。 Let's go step by step so you understand clearly what I need.让我们一步一步来,让你清楚地了解我需要什么。

My example is very similar to this course: https://www.baeldung.com/jpa-many-to-many .我的示例与本课程非常相似: https : //www.baeldung.com/jpa-many-to-many Look at the 3.1 section.3.1节。

1) Let's say, we have a student table and course table. 1) 假设我们有一个student表和course表。 Between these, there is a join table (let's call it for example studentCourseJoin ).在这些之间,有一个连接表(我们称之为studentCourseJoin )。 Ok, this seems to be nearly same as presented on Baeldung site.好吧,这似乎与 Baeldung 网站上呈现的几乎相同。 But in my case I need to have additional table ratings which is separated from join table.但在我的情况下,我需要有额外的表ratings ,它与连接表分开。 And assume that Student can rate course multiple times (let's say once a year)并假设学生可以多次评价课程(假设一年一次)

2) My goal is to have data structure like this: 2)我的目标是拥有这样的数据结构:

{
  students: [
    {
      studentID: 1,
      courses: [
        {
          courseID: 1,
          ratings: [{
            ratingID: 1,
            studentID: 1
          }]
        }
      ]
    }
  ]
}

So, if I have a student with ID 1, I want to list all his courses and all his ratings he made to the given course (as presented in the example).因此,如果我有一个 ID 为 1 的学生,我想列出他的所有课程以及他对给定课程的所有评分(如示例中所示)。

I am having problem with ratings.我的评分有问题。 Because I always get all ratings to the given course no matter who made the rating.因为无论是谁评分,我总是会获得给定课程的所有评分。 Basically I am missing just one WHERE clause.基本上我只缺少一个WHERE子句。

Example result:结果示例:

{
  students: [
    {
      studentID: 1,
      courses: [
        {
          courseID: 1,
          ratings: [{
            ratingID: 1,
            studentID: 1
          },{
            ratingID: 2,
            studentID: 5
          }]
        }
      ]
    }
  ]

Making a custom query isn't hard.. It's just about 3 JOIN s.进行自定义查询并不难。它只是大约 3 JOIN s。 But that is not what I want.但这不是我想要的。 I want to set up all entities right, so Hibernate can automatically make queries, delete orphans etc...我想正确设置所有实体,以便 Hibernate 可以自动进行查询、删除孤儿等...

3) Input data: studentID: 1 (and another scenario: studentID: 1, yearRated: 2020 ) 3) 输入数据: studentID: 1 (以及另一个场景: studentID: 1, yearRated: 2020

4) My question? 4)我的问题? How can I integrate ratings table so it applies filter WHERE studentId == 1 .我如何整合ratings表,以便它应用过滤器WHERE studentId == 1 In seconds scenario WHERE studentId == 1 AND yearRated == 2020在几秒钟内, WHERE studentId == 1 AND yearRated == 2020

What schema would I choose between these 2 ?我会在这两个之间选择什么模式? Or do you propose different?或者你提出不同的建议?

Schema 1模式 1

在此处输入图片说明

Schema 2模式 2

在此处输入图片说明

I am missing FK definition in rating table in Schema 2 (just ignore it)我在模式 2 的rating表中缺少 FK 定义(忽略它)

I am a beginner with Spring JPA so I am missing some experience and this could be an easy question.我是 Spring JPA 的初学者,所以我缺少一些经验,这可能是一个简单的问题。 Locally my database has quite different names and much more columns and absolutely different purpose.在本地,我的数据库具有完全不同的名称和更多的列以及完全不同的用途。 But I wanted to make this example as easy as possible.但我想让这个例子尽可能简单。

For now, let's assume I have entity for each table (except join table) and I have correctly configured M:N relationship with @ManyToMany and @Jointable decorators现在,让我们假设每个表都有实体(连接表除外),并且我已经正确配置了与 @ManyToMany 和 @Jointable 装饰器的 M:N 关系

Edit编辑

Let's say, that I would like to use Schema 2. Question is, am I able to create a method name which will automatically create query with required result?假设我想使用模式 2。问题是,我是否能够创建一个方法名称,该名称将自动创建具有所需结果的查询? (Paragraph 3). (第 3 段)。 If so, what are the requirements?如果有,有什么要求? Do I have to create entity for join table and also embeddable entity for composite key ?我是否必须为连接表创建实体以及为复合键创建可嵌入实体? and how exactly do I join rating table with this join table?以及如何将rating表与此连接表连接起来?

EDIT 2编辑 2

I created a new question which is simplified and should be more understandable.我创建了一个简化的新问题,应该更容易理解。

If you choose Schema 2 Than you can not get the desired result, because a student must not visit the course to rate the course.如果您选择Schema 2 Than,则无法获得所需的结果,因为学生不得访问课程来评价课程。 You expect the ratings under the courses, but there are independent.你期望根据课​​程的评分,但有独立的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM