如何在JDBC中实现一对多关系?

huangapple go评论68阅读模式
英文:

How to do one to many relationships in JDBC?

问题

我有两个类
我想执行一对多的关系

public class Teacher {
    private int id;
    private String name;
    private List<Class> classes;
}

public class Class {
    private int id;
    private String className;
}
我需要从数据库中检索数据并打印数据

<pre>
教师     班级们
Kumar  A1,B3,B4
Deepa  A1,A2,C1
Alex     B2,D1,D2
</pre>

我不知道如何在JDBC中检索一对多的关系数据
请建议我该怎么办?
英文:

I have class two classes
i want to perform one to many relationship

public class Teacher {
private int id;
private String name;
private List&lt;Class&gt; classes;
 }

public class Class {
private int id;
private String className;
}

I need to retrieve data and print data from the database

<pre>Teacher Classes<br>
Kumar A1,B3,B4<br>
Deepa A1,A2,C1<br>
Alex B2,D1,D2<br></pre>

I don't Know how to retrive one to many relationship data in JDBC <br>
please suggest me what should i do?

答案1

得分: 2

我确实知道用jdbc有两种方法来做这件事

  1. 首先你需要先选择所有的老师,然后对于每个老师选择他的课程。

    select * from teacher

然后将结果映射到你的teacher类,然后在Java代码中使用一个for each循环,通过teacher_id为每个老师获取课程。

select * from class where teacher_id = :teacher_id
  1. 你可以这样选择所有的老师和课程:

    select * from teacher t
    left join class c on c.teacher_id = t.teacher_id

但是你会得到重复的老师数据,因为在每一行中你都会获取到老师的数据,你需要在Java代码中进行整理。

英文:

I do know about 2 ways to do it with jdbc

  1. You need to select all teachers first, then for each teacher select his classes.

    select * from teacher

then map results to your teacher class then on java side make a for each loop and fetch class by teacher_id for each teacher

select * from class where teacher_id = :teacher_id
  1. You can select all teachers and classes like that:

    select * from teacher t
    left join class c on c.teacher_id = t.teacher_id

but you will get duplicate data of teacher becouse in each row you will fetch data for teacher also and you will need to organize it on Java side.

答案2

得分: 0

以下是翻译的内容:

我最近一直在使用Spring JDBC进行工作。这与我在这里和那里阅读后想出的内容类似。我非常欣赏JDBC与领域驱动方法相结合的简单性:

public class Teacher {
    @Id private int id;
    private String name;
    @MappedCollection(idColumn = "teacher_id", keyColumn = "teacher_key")
    private List<Class> classes;
     
    //setters and getters
}

public class Class {
    private int id;
    private String className;
    
    //setters and getters
}

然后您需要通过扩展Spring JDBC的CrudRepository来创建存储库接口,您不需要创建额外的存储库来通过Teacher保存Class数据:

interface TeacherRepository extends CrudRepository<Teacher, Integer> {}

TeacherRepository将具有典型的CRUD方法,允许您从数据库中读取或写入数据。

SQL代码(这类似于PostgreSQL,根据您的特定方言进行更改)可能如下所示:

create table teachers(
    id serial primary key,
    name text
 )

 create table classes(
    id serial primary key,
    class_name text,
    teacher_id int references teachers(id),
    teacher_key int
 )

其中teacher_key列用于返回有序的课程列表。如果您不关心课程的顺序,可以返回一个集合而不是列表:

public class Teacher {
    @Id private int id;
    private String name;
    @MappedCollection(idColumn = "teacher_id")
    private Set<Class> classes;
     
    //setters and getters
}

public class Class {
    private int id;
    private String className;
    
    //setters and getters
}

SQL代码如下:

create table teachers(
    id serial primary key,
    name text
 )

 create table classes(
    id serial primary key,
    class_name text,
    teacher_id int references teachers(id)
 )

以下是我的信息来源:

英文:

So I've been working recently with Spring JDBC myself. This is something similar with what I've come up with by reading here and there. I really appreciate the simplicity of JDBC combined with the domain driven approach:

public class Teacher {
    @Id private int id;
    private String name;
    @MappedCollection(idColumn = &quot;teacher_id&quot;, keyColumn = &quot;teacher_key&quot;)
    private List&lt;Class&gt; classes;
     
    //setters and getters
}

public class Class {
    private int id;
    private String className;
    
    //setters and getters
}

Then you need to create the repository interface by extending CrudRepository from Spring JDBC, you don't need to create an extra repository to e.g. save Class data through Teacher :

interface TeacherRepository extends CrudRepository&lt;Teacher, Integer&gt; {}

The TeacherRepository will have the typical CRUD methods that allow you to read or write data from/to the database.

The sql code (this is postgres like, change to your specific dialect) would be something like this:

create table teachers(
    id serial primary key,
    name text
 )

 create table classes(
    id serial primary key,
    class_name text,
    teacher_id int references teachers(id),
    teacher_key int
 )

Where the teacher_key column is used to return an ordered list of classes. If you don't care about the classes ordering you can just return a set instead of a list:

public class Teacher {
    @Id private int id;
    private String name;
    @MappedCollection(idColumn = &quot;teacher_id&quot;)
    private Set&lt;Class&gt; classes;
     
    //setters and getters
}

public class Class {
    private int id;
    private String className;
    
    //setters and getters
}

the sql code:

create table teachers(
    id serial primary key,
    name text
 )

 create table classes(
    id serial primary key,
    class_name text,
    teacher_id int references teachers(id)
 )

Here are my sources:

huangapple
  • 本文由 发表于 2020年9月10日 14:54:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63824271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定