...

[JAVA/Spring/Jpa/Criteria] Unable to locate Attribute with the the given name [isOnline] on this ManagedType 오류 해결 본문

이것저것/오류 해결

[JAVA/Spring/Jpa/Criteria] Unable to locate Attribute with the the given name [isOnline] on this ManagedType 오류 해결

gi2 2022. 6. 14. 00:25

study 안에 studyInfo를 embeddable로 넣어둬서 당연히 s.get("isOnline")만 해도 불러와질줄 알았는데

s.get("studyInfo").get("isOnline")으로 받아왔어야 했던 거였다...

 

public List<Study> findStudyListByCondition(String dayOfWeek, String isOnline, String categories){

        //Criteria 동적 쿼리 생성
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<Study> cq = cb.createQuery(Study.class);

        Root<Study> s = cq.from(Study.class);

        List<Predicate> criteria = new ArrayList<Predicate>();

        if(dayOfWeek != null) criteria.add(cb.equal(s.get("dayOfWeek"),
                cb.parameter(String.class, "dayOfWeek")));
        if(isOnline != null) criteria.add(cb.equal(s.get("studyInfo").get("isOnline"),
                cb.parameter(String.class, "isOnline")));
        if(categories != null) criteria.add(cb.equal(s.get("studyInfo").get("categories"),
                cb.parameter(String.class, "categories")));

        cq.where(cb.and(criteria.toArray(new Predicate[0])));

        TypedQuery<Study> query = em.createQuery(cq);
        if(dayOfWeek != null) query.setParameter("dayOfWeek",dayOfWeek);
        if(isOnline != null) query.setParameter("isOnline",isOnline);
        if(categories != null) query.setParameter("categories",categories);

        return query.getResultList();
    }
}
@Entity
@Getter @Setter
public class Study{

    @Id @GeneratedValue
    @Column(name = "study_id")
    private Long id;

    private StudyInfo studyInfo;

    @OneToMany(mappedBy = "study", cascade = CascadeType.ALL)
    private List<Question> questionList = new ArrayList<>();

    @Enumerated(EnumType.STRING)
    private RecruitStatus status = RecruitStatus.recruit;
    private LocalDateTime postTime = LocalDateTime.now();

    public void modifyPersonLimit(int amount){
        studyInfo.modifyPersonLimit(amount);
    }

    /*연관관계 메서드*/
    public void setQuestion(Question question){
        questionList.add(question);
        question.setStudy(this);
    }
}
@Embeddable
@Getter
@AllArgsConstructor
public class StudyInfo {

    private String title; //스터디제목
    private String region; //지역
    private String dayOfWeek; //요일
    private String isOnline; //대면비대면여부
    private String categories; //공부카테고리
    private int personLimit; //인원제한
    private String content; //소개줄글

    protected StudyInfo() {
    }

    public void modifyAll(String title, String region, String dayOfWeek,
                          String isOnline, String categories, int personLimit, String content){
        this.title = title;
        this.region = region;
        this.dayOfWeek = dayOfWeek;
        this.isOnline = isOnline;
        this.categories = categories;
        this.personLimit = personLimit;
        this.content = content;
    }

    public void modifyPersonLimit(int amount){
        personLimit += amount;
    }

}
Comments