[英]Establishing relationship between three tables
I am building Demo application in which we have three tables and i am using spring-boot data Jpa with mysql.我正在构建演示应用程序,其中我们有三个表,并且我正在使用 spring-boot 数据 Jpa 和 mysql。 I have following requirement
我有以下要求
Table1 name->Student表1名称->学生
sid sname srole //columns name sid sname srole //列名
Table2 name->Courses表2名称->课程
cid cname ctime //columns name cid cname ctime //列名
Table3 name-> Tutioncenter表3名称-> Tutioncenter
id Name sid(student table sid) cid(Courses table cid) id 名称 sid(学生表 sid) cid(课程表 cid)
@Entity
public class Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long sid;
private String sname;
private String srole;
//getter and setter
}
@Entity
public class Courses{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cid;
private String cname;
private String ctimme;
//getter and setter
}
@Entity
public class Tutioncenter{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "sid", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Student student;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "cid", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
private Courses courses;
//getter ans setter
}
@Repository
public interface CoursesRepository extends JpaRepository<Courses,Long>{}
@Repository
public interface StudentRepository extends JpaRepostiroy<Student,Long>{}
@Repository
public interface TutionRepository extends JpaRepository<Tutioncenter,Long>{
Page<SecretManager> findBySIdAndCId(Long sId, Pageable pageable);
Optional<SecretManager> findBySecretIdAndSIdAndCId(Long sid, Long cid);
}
Now I can design controller for Student and Courses but how should i design controller of Tutioncenter for crud operations??现在我可以为学生和课程设计 controller 但是我应该如何设计 Tutioncenter 的 controller 来进行 crud 操作?
The create, read, update
operations of both student
and courses
table should be no relationship with tutioncenter table. student
表和courses
表的create, read, update
操作都应该与 tutioncenter 表没有关系。 Then we only need to consider how to deal with delete
operation.那么我们只需要考虑如何处理
delete
操作。 Considering data in tutioncenter table, there would be 2 opotions:考虑到 tutioncenter 表中的数据,将有 2 个选项:
The following are the SQL of delete both table together:以下是同时删除两个表的 SQL:
delete c, t from `courses` c join `tutioncenter` t on c.cid = t.cid where id = 1;
For tutioncenter table, I think there may be no update operation according to your business.对于tutioncenter表,我认为根据您的业务可能没有更新操作。 Therefore we only need to focus on
create, read, delete
operations.因此我们只需要关注
create, read, delete
操作。
read operation is as following:读操作如下:
select s.sname, c.cname, c.ctime from `tutioncenter` t join `student` s on s.sid = t.sid join `courses` c on c.cid = t.cid;
delete operation is as following:删除操作如下:
delete t from `tutioncenter` t join `student` s on s.sid = t.sid join `courses` c on c.cid = t.cid where t.sid = 1 and t.cid = 2;
insert operation as following:插入操作如下:
insert into `tutioncenter` (name, sid, cid)
select 'test3', sid, cid from `student` s, `courses` c where s.sid = 1 and c.cid = 1;
Back to how to design your controller for TutionRepository
, I think that should base on how to design your business.回到如何为 TutionRepository 设计您的
TutionRepository
,我认为这应该基于如何设计您的业务。 For example, If this demo system is designed only for student and administrator, then you need to consider both situations.例如,如果这个演示系统是为学生和管理员设计的,那么你需要考虑这两种情况。
The following are what I could give till now how a student use your system:以下是到目前为止我可以提供的学生如何使用您的系统的内容:
To sum up, choosing courses system for student need the following interface:综上所述,学生选课系统需要如下界面:
GET /students/<sid>
or GET /students/<sname>
. GET /students/<sid>
或GET /students/<sname>
。 If you have login interface, you could use it instead.GET /students/<sid>/courses
GET /courses
POST /students/<sid>/courses
body is cid list like [cid1, cid2]
POST /students/<sid>/courses
正文是 cid 列表,如[cid1, cid2]
DELETE /students/<sid>/courses/<cid>
The same to analysis what interfaces needed if you want to add a administrator role, who can manage courses and students.如果要添加管理员角色,谁可以管理课程和学生,则分析需要哪些接口也是如此。
Anyway, all should begin from business requirements.无论如何,一切都应该从业务需求开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.