无法在 Spring 启动时连接两个表

Couldn't join two tables on Spring boot

我是 spring 启动应用程序的初学者。我想加入课程 table 和学生 table 一起。到目前为止我尝试了什么,我在下面附上了代码。我没有收到任何错误。加载学生页面时,我只显示课程 ID 我需要显示名称而不是 ID。我附上了下面的截图。

上面的截图图片只显示了课程id我需要显示课程名称。

学生管理员

@RequestMapping(value = "/student", method = RequestMethod.GET)
public String viewStudentPage(Model model) {
     List<Student> liststudent = services.listAll();
     model.addAttribute("liststudent", liststudent);
     System.out.print("Get / ");    
     return "Student";
}

我做了下面的关系。到目前为止我尝试了什么。

Course.java

@Entity
public class Course {
    
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String coursename;
    private int duration;
    
    @ManyToOne
    private Student student;

    public Course()
    {       
        
    }
 
    public Course(Long id, String coursename, int duration) {
        
        this.id = id;
        this.coursename = coursename;
        this.duration = duration;
    }
    

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getCoursename() {
        return coursename;
    }
    public void setCoursename(String coursename) {
        this.coursename = coursename;
    }
    public int getDuration() {
        return duration;
    }
    public void setDuration(int duration) {
        this.duration = duration;
    }


    @Override
    public String toString() {
        return "Course [id=" + id + ", coursename=" + coursename + ", duration=" + duration + "]";
    }
}

Student.java

@Entity
@Table(name="student")
public class Student {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Long id;
    private String stname;
    private int course;
    private int fee;
    
    @OneToMany(mappedBy = "course")
    private List<Student> student;

    public Student() {
        
        
    }
   
    public Student(Long id, String stname, int course, int fee) {
    
        this.id = id;
        this.stname = stname;
        this.course = course;
        this.fee = fee;
    }

    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public String getStname() {
        return stname;
    }
    public void setStname(String stname) {
        this.stname = stname;
    }
    public int getCourse() {
        return course;
    }
    public void setCourse(int course) {
        this.course = course;
    }
    public int getFee() {
        return fee;
    }
    public void setFee(int fee) {
        this.fee = fee;
    }


    @Override
    public String toString() {
        return "Student [id=" + id + ", stname=" + stname + ", course=" + course + ", fee=" + fee + "]";
    }
}

StudentRepository

@Repository
public interface StudentRepository extends JpaRepository<Student, Long>{ }

Student.html

<table class="table">
  <thead class="thead-dark">
    <tr>
            <th>Student ID</th>
            <th>Student Name</th>
            <th>Course Name</th>
             <th>Fee</th>
             <th>edit</th>
             <th>delete</th>
    </tr>
  </thead>
  <tbody>
      <tr  th:each="student : ${liststudent}">
        <td th:text="${student.id}">Student ID</td>
        <td th:text="${student.stname}">Student Name</td>
        <td th:text="${student.course}">Course</td>
         <td th:text="${student.fee}">Fee</td>
        <td>
            <a th:href="@{'/Student/edit/' + ${student.id}}">Edit</a>
        </td>                               
        <td>
            <a th:href="@{'/Student/delete/' + ${student.id}}">Delete</a>
        </td>           
        </tr> 
   
  </tbody>
</table>

我写给加入的自定义代码

@存储库

public interface StudentRepository extends JpaRepository<Student, Long>{

    @Query(value="select student.id, student.stname, course.coursename from student Inner JOIN course ON  student.course= course.id", nativeQuery=true)
    List<Object[]> findStudent();
    
    
}

您必须添加自定义查询才能获取课程名称。您的 listAll() return 所有没有课程的学生对象,有效载荷没有任何变量,例如 name 并且您的实体中有 course id 这就是 ID 出现在您的 [=35= 中的原因].

你的学生对象有课程对象,你也可以像下面这样得到。

您的实体关系有误,如下所示。 它应该属于 ManyToMany 关系,因为一个用户有很多课程,一门课程属于很多学生,无论如何你开始时是 oneToMany 然后按照下面的方式进行。

在学生实体内。

 @OneToMany(fetch = FetchType.LAZY,
            cascade = CascadeType.ALL,mappedBy = "student")
    private List<Course> course;

在课程实体内

 @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JoinColumn(name = "id", nullable = false)
    private Student student;

然后尝试访问课程对象,如下所示。

th:field="*{student.course.name}"

如果您想尝试自定义查询,请尝试如下操作。

@Query(value="select s.id, s.name, c.name from Student s left JOIN Course c on student.course_id= c.id", nativeQuery=true)
List<Object[]> findStudent();