java - 如何将 spring-boot 下拉列表获取的数据保存在数据库中?

标签 java hibernate spring-boot

启动。在我的系统中,有两个名为 subject、course 的模型(表名 subject、courses 并通过外键 course_id 连接)。addSubject 表单中有一个下拉列表,我们可以在其中选择类(class)名称。但是当我尝试将其保存在数据库中时(当我单击保存按钮时)我收到错误无法添加或更新子行:外键约束失败(sampledb.subjects,CONSTRAINT courseId_fk FOREIGN KEY (course_code) REFERENCES courses (code) ON DELETE CASCADE ON UPDATE CASCADE)。请帮我解决 htis。

主题 Controller

@Entity
@Table(name="subjects")
@EntityListeners(AuditingEntityListener.class)

public class Subject {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String subject_code;
    private String name;
    private String hall_code;
    private String course_code;

    public Subject() {
    }

    public Subject(String subject_code, String name, String hall_code, String course_code) {
        this.subject_code = subject_code;
        this.name = name;
        this.hall_code = hall_code;
        this.course_code = course_code;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSubject_code() {
        return subject_code;
    }

    public void setSubject_code(String subject_code) {
        this.subject_code = subject_code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHall_code() {
        return hall_code;
    }

    public void setHall_code(String hall_code) {
        this.hall_code = hall_code;
    }

    public String getCourse_code() {
        return course_code;
    }

    public void setCourse_code(String course_code) {
        this.course_code = course_code;
    }
}

类(class) DAO

@Service
public class CourseDAO {

    @Autowired
    CourseRepository courseRepository;

    //to save a course
    public Course save(Course course){
        return courseRepository.save(course);
    }

    //to search all courses
    public List<Course> findAll(){
        return courseRepository.findAll();
    }

    //get a course by id
    public Course findById(Long id){
        return courseRepository.findById(id).orElse(null);
    }


    //delete a course
    public void delete(Long id){
        courseRepository.deleteById(id);
    }


}

类(class)模型

@Entity
@Table(name="courses")
@EntityListeners(AuditingEntityListener.class)

public class Course {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @NotNull
    private int code;
    private String name;

    public Course() {
    }

    public Course(@NotNull int code, String name) {
        this.code = code;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

添加主题表单

  <!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Adding a Subject</title>
</head>
<body>
<div align="center">
    <h1>Add a new Subject</h1>
    <br/>
    <form action="#" th:action="@{/subject/save}" th:object="${subject}" method="post">
        <table border="0" cell[adding="10">
            <tr>
                <td>Subject code:</td>
                <td><input type="text" th:field="*{subject_code}" /></td>
            </tr>
            <tr>
                <td>Subject Name:</td>
                <td><input type="text" th:field="*{name}" /></td>
            </tr>
            <tr>
                <td>Course:</td>
                <td>
                    <select th:field="*{course_code}">
                        <option value="">Choose..</option>
                        <option th:each="course: ${courses}" th:value="${course.id}" th:text="${course.name}" />
                    </select>
                </td>
            </tr>
            <tr>
                <td colspan="2"><button type="submit">Save</button></td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>

主题模型

@Entity
@Table(name="subjects")
@EntityListeners(AuditingEntityListener.class)

public class Subject {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String subject_code;
    private String name;
    private String hall_code;
    private String course_code;

    public Subject() {
    }

    public Subject(String subject_code, String name, String hall_code, String course_code) {
        this.subject_code = subject_code;
        this.name = name;
        this.hall_code = hall_code;
        this.course_code = course_code;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSubject_code() {
        return subject_code;
    }

    public void setSubject_code(String subject_code) {
        this.subject_code = subject_code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getHall_code() {
        return hall_code;
    }

    public void setHall_code(String hall_code) {
        this.hall_code = hall_code;
    }

    public String getCourse_code() {
        return course_code;
    }

    public void setCourse_code(String course_code) {
        this.course_code = course_code;
    }
}

主题 DAO

@Service
public class SubjectDAO {

    @Autowired
    SubjectRepository subjectRepository;

    //to save a subject
    public Subject save(Subject subject){
        return subjectRepository.save(subject);
    }

    //to search all subjects
    public List<Subject> findAll(){
        return subjectRepository.findAll();
    }

    //get a subject by id
    public Subject findById(Long id){
        return subjectRepository.findById(id).orElse(null);
    }


    //delete a subject
    public void delete(Long id){
        subjectRepository.deleteById(id);
    }


}

错误 引起:org.springframework.beans.NotReadablePropertyException:bean类[com.project.attendance.model.Subject]的无效属性“course”:Bean属性“course”不可读或具有无效的getter方法:返回类型是否getter 的参数类型与 setter 的参数类型匹配吗?

最佳答案

我必须将 course.id 更改为 course.code,这是我犯的错误。thx

 <tr>
                <td>Course:</td>
                <td>
                    <select th:field="*{course_code}">
                        <option value="">Choose..</option>
                        <option th:each="course: ${courses}" th:value="${course.code}" th:text="${course.name}" />
                    </select>
                </td>
            </tr>

关于java - 如何将 spring-boot 下拉列表获取的数据保存在数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950605/

相关文章:

java - 迁移到 JDK 11 后 Spring Boot 测试中的 Mockito 错误

java - JOptionPane 有几个输入字段?

java - 两个微服务之间的通信

java - Nexus 7 上的广播接收器

java - hibernate 错误: HHH000091

java - 有没有办法使用 "GREATEST(field1, field2)"进行 SELECT,其中 field1 和 field2 是同一选择中的聚合和?

hibernate - HSQLDB 和 Hibernate : Unit Test raises org. hsqldb.HsqlException: 用户缺少权限或找不到对象

java - application.properties 被忽略,取自别处

java - 即使在覆盖同一对象的哈希码和等于之后,HashMap 也不会检索这两个值

java - 应用程序无法与另一台计算机上的 solr 引擎连接