I'm a beginner with spring and I have this little issue. "No property questionId found for type CourseTestCompleteField!" I have 2 model classes that are connected via a one to one join. That 2 model class are:
package com.example.springboot.models; import javax.persistence.*; import javax.validation.constraints.NotBlank; @Entity @Table(name = "questions") public class CourseTestQuestion { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name="question_id") private Long id; @NotBlank @Column(name = "question_course") private String questionCourse; @NotBlank @Column(name = "question_type") private String questionType; public CourseTestQuestion(){ } public CourseTestQuestion(String questionCourse, String questionType) { this.questionCourse = questionCourse; this.questionType = questionType; } // public getters and setters for all fields here } And:
package com.example.springboot.models; import javax.persistence.*; import javax.validation.constraints.NotBlank; @Entity @Table(name = "quiz_complete_field_questions", uniqueConstraints = { @UniqueConstraint(columnNames = "question_id") } ) public class CourseTestCompleteField { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @NotBlank @Column(name = "question_content") private String questionContent; @NotBlank @Column(name = "answer") private String answer; @NotBlank @Column(name = "points") private String points; @NotBlank @Column(name = "course") private String course; @NotBlank @Column(name = "teacher_username") private String teacher; @OneToOne(cascade = CascadeType.ALL) @JoinColumn(name = "question_id", referencedColumnName = "question_id") private CourseTestQuestion courseTestQuestion; public CourseTestCompleteField(){ } public CourseTestCompleteField(CourseTestQuestion courseTestQuestion, String question, String answer, String points, String course, String teacher) { this.courseTestQuestion = courseTestQuestion; this.questionContent = question; this.answer = answer; this.points = points; this.course = course; this.teacher = teacher; } // public getters and setters for all fields here } My repo for both:
package com.example.springboot.repository; import com.example.springboot.models.Course; import com.example.springboot.models.CourseTestQuestion; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.Optional; @Repository public interface CourseTestQuestionRepository extends JpaRepository<CourseTestQuestion, Long> { Optional<CourseTestQuestion> findById(Long id); Optional<CourseTestQuestion> findByQuestionCourse(String questionCourse); } And:
package com.example.springboot.repository; import com.example.springboot.models.CourseTestCompleteField; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import java.util.List; import java.util.Optional; @Repository public interface CourseTestCompleteFieldRepository extends JpaRepository<CourseTestCompleteField, Long> { Optional<CourseTestCompleteField> findById(Long id); Optional<CourseTestCompleteField> findByQuestionId(Long questionId); Optional<CourseTestCompleteField> findByCourse(String course); List<CourseTestCompleteField> findByQuestionContent(String questionContent); List<CourseTestCompleteField> findByTeacher(String teacher); Boolean existsByQuestionContent(String questionContent); } The problem is with Optional<CourseTestCompleteField> findByQuestionId(Long questionId);but I don't get it why, because in database I have the table for CourseTestCompleteFieldModel with question_id column, and in CourseTestCompleteField I have CourseTestQuestion object. Tho, the table for CourseTestCompleteField has a different name, could be this a problem? I should rename the table to course_test_complete_field?
Can someone help me please? Thank you