TroubleShooting

Class com.querydsl.core.types.QBean can not access a member of class "entity" with modifiers "protected"

에디개발자 2020. 11. 6. 16:40
반응형

JPA, Querydsl을 학습하는 도중 com.querydsl.core.types.QBean com.example.queyrdsl.entity.Staff with modifiers "protected" 라는 Exception이 발생했다. 

querydsl 사용중 QBean에서 Entity객체인 Staff를 생성하려한다. 하지만 Staff 클래스는 Access = Protected였다. 그래서 위와같은 에러가 발생하였다.

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)	// 이 부분이 문제
public class Staff {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String name;
    private Integer age;

    @ManyToOne(targetEntity = Store.class, fetch = FetchType.LAZY)
    @JoinColumn(name = "store_id", foreignKey = @ForeignKey(name = "fk_staff_store_id"))
    private Store store;

    @Builder
    public Staff(Long id, String name, Integer age, Store store) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.store = store;
    }
}

 

AccessLevel을 public으로 수정하여서 정상작동을 하였다.

Entity는 일관성의 이유로 protected로 선언하는게 좋으나 지금은 어쩔수 없는 것 같다. 다른방법이 있으면 댓글에 남겨주시면 감사드립니다 :) 

 

2일 뒤 문제를 찾았습니다.

public List<Staff> findStaffsByName(String name) {
    return jpaQueryFactory
            .select(Projections.fields(Staff.class,
                    staff.id
                    , staff.age
                    , staff.name
            ))
            .from(store)
            .join(staff)
                .on(store.id.eq(staff.storeId))
            .where(store.name.eq(name))
            .fetch();
}

querydsl을 사용하여 조회하여 필드값을 세팅하는 부분에 StaffEntity를 사용하여서 문제가 되었습니다. 조회된 값을 Staff Entity에 접근하여 세팅하려는데 접근자가 protected여서 접근 실패되었기 때문에 문제가 되었습니다.

 

해결 방법으로는 Entity 클래스가 아닌 Vo, Dto로 받자! Vo, Dto는 접근자를 public으로 해서 받자!

반응형