jinxed
jinxed

Reputation: 35

No property .. found for type .. in spring boot

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

Upvotes: 1

Views: 1250

Answers (2)

S. Anushan
S. Anushan

Reputation: 788

There is no field call questionId in you entity and you have id only. That's you got error. You can use that findyById(). That's only enough.

If you would like write JPA repository method like findBy..., getBy..., deleteBy...., countBy..., After this you need append exact field name from entity. For example if you entity have name then can write below methods. findByName(); deleteByName(); countByName();

So try as below. findBycourseTestQuestion(Object o); Pass questions object.

Upvotes: 0

Aditya
Aditya

Reputation: 237

Since,This is a query on nested Object. You need to update your query as this.

Optional<CourseTestCompleteField> findByCourseTestQuestion_Id(Long questionId);

This works even without "_"

 Optional<CourseTestCompleteField> findByCourseTestQuestionId(Long questionId);

But better to put "_" while accessing nested fields for better readability.

Upvotes: 1

Related Questions