需要从自动生成的 table 中获取数据,没有模型也没有存储库
It is necessary to take data from a table that is generated automatically and there is no model and no repository for it
我有一个学生模型
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany
@JoinTable (name="student_university",
joinColumns=@JoinColumn (name="student_id"),
inverseJoinColumns=@JoinColumn(name="university_id"))
private List<University> universities;
和大学模型
@Entity
public class University {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
@ManyToMany
@JoinTable(name="student_university",
joinColumns=@JoinColumn(name="university_id"),
inverseJoinColumns=@JoinColumn(name="student_id"))
private List<Student> students;
我有 2 个模型和 2 个存储库。由于连接是 ManyToMany,因此自动生成 3 table。如果没有模型和存储库,我如何从这个新 table 获取数据?也许您可以从控制器以某种方式编写请求并将结果传递给模板?
SQL 学生
大学SQL
student_university SQL
假设因为您正在使用 spring-boot,您已经为您的实体创建了相关的 JpaRepositories,例如studentRepository
和 universityRepository
.
从数据库中加载选定的(在下拉列表中)学生和大学,例如
假设从下拉列表中选择的学生有#id: 10,而从另一个下拉列表中选择的大学有#id: 45
Long studentId = 10L; //retrieve the real value from your controller's request
Student student = studentRepository.findById(studentId);
Long univerityId = 45L; //retrieve the real value from your controller's request
University univeristy = universityRepository.findById(univerityId);
然后将大学添加到学生的大学列表并保存到数据库
student.getUniversities().add(university); //make sure your university list is not null else you will get a null pointer exception here
university.getStudents().add(student); // you can also add the student to the university's students list
studentRepository.save(student);
保存后 student_university
table 应该有一个包含学生 ID 和大学 ID 的新记录。
我有一个学生模型
@Entity
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@ManyToMany
@JoinTable (name="student_university",
joinColumns=@JoinColumn (name="student_id"),
inverseJoinColumns=@JoinColumn(name="university_id"))
private List<University> universities;
和大学模型
@Entity
public class University {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column
private String name;
@ManyToMany
@JoinTable(name="student_university",
joinColumns=@JoinColumn(name="university_id"),
inverseJoinColumns=@JoinColumn(name="student_id"))
private List<Student> students;
我有 2 个模型和 2 个存储库。由于连接是 ManyToMany,因此自动生成 3 table。如果没有模型和存储库,我如何从这个新 table 获取数据?也许您可以从控制器以某种方式编写请求并将结果传递给模板?
SQL 学生
大学SQL
student_university SQL
假设因为您正在使用 spring-boot,您已经为您的实体创建了相关的 JpaRepositories,例如studentRepository
和 universityRepository
.
从数据库中加载选定的(在下拉列表中)学生和大学,例如 假设从下拉列表中选择的学生有#id: 10,而从另一个下拉列表中选择的大学有#id: 45
Long studentId = 10L; //retrieve the real value from your controller's request
Student student = studentRepository.findById(studentId);
Long univerityId = 45L; //retrieve the real value from your controller's request
University univeristy = universityRepository.findById(univerityId);
然后将大学添加到学生的大学列表并保存到数据库
student.getUniversities().add(university); //make sure your university list is not null else you will get a null pointer exception here
university.getStudents().add(student); // you can also add the student to the university's students list
studentRepository.save(student);
保存后 student_university
table 应该有一个包含学生 ID 和大学 ID 的新记录。