작성
·
815
1
안녕하세요!
querydsl과 jparepository 강의를 듣고 참고하여 개인 코딩 공부를 하고 있는 학생입니다.
다름이 아니라 좋아요 기능 구현 중 customrepository를 사용하였는데 하단과 같은 오류가 발생해서 문의 드립니다.
확인해보니 패키지를 분리하라고 하신 분도 있어서 패키지 분리를 했는데도 동일한 오류가 발생합니다ㅠ.ㅠ이유가 뭘까요..
오류
Error creating bean with name 'businessLikeService' defined in file [/Users/gimnayeon/Desktop/GreenProject/GrinGreen/out/production/classes/com/grin/GrinGreen/BusinessLike/service/BusinessLikeService.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'businessLikeRepository' defined in com.grin.GrinGreen.BusinessLike.repository.BusinessLikeRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Invocation of init method failed; nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract java.util.Optional com.grin.GrinGreen.BusinessLike.repository.BusinessLikeRepositoryCustom.exist(java.lang.String,java.lang.Long)! Reason: Failed to create query for method public abstract java.util.Optional com.grin.GrinGreen.BusinessLike.repository.BusinessLikeRepositoryCustom.exist(java.lang.String,java.lang.Long)! No property 'exist' found for type 'BusinessLike'!; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.Optional com.grin.GrinGreen.BusinessLike.repository.BusinessLikeRepositoryCustom.exist(java.lang.String,java.lang.Long)! No property 'exist' found for type 'BusinessLike'!
BusinessLike Entity
@Entity
@Getter
@NoArgsConstructor
public class BusinessLike {
@Id
@GeneratedValue
@Column(name = "like_id")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "mail")
private Member member;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "business_id")
private Business business;
@QueryProjection
public BusinessLike(Member member, Business business) {
this.member = member;
this.business = business;
}
}
BusinessLike DTO
@Data
public class BusinessLikeDto {
private Member member;
private Long businessId;
@QueryProjection
public BusinessLikeDto(Member member, Long businessId) {
this.member = member;
this.businessId = businessId;
}
}
BusinessLike Response DTO
@Data
public class BusinessLikeResponseDto {
private Long BusinessLikeNum;
private Boolean check;
@QueryProjection
public BusinessLikeResponseDto(Long BusinessLikeNum, Boolean check) {
this.BusinessLikeNum = BusinessLikeNum;
this.check = check;
}
}
BusinessLikeCustomRepository
public class BusinessLikeCustomRepositoryImpl implements BusinessLikeRepositoryCustom{
private final JPAQueryFactory queryFactory;
public BusinessLikeCustomRepositoryImpl(EntityManager em) {
this.queryFactory = new JPAQueryFactory(em);
}
@Override
public Optional<BusinessLike> exist(String mail, Long businessId) {
BusinessLike lList = queryFactory
.selectFrom(businessLike)
.where(businessLike.member.mail.eq(mail),
businessLike.business.id.eq(businessId))
.fetchFirst();
return Optional.ofNullable(lList);
}
public long BusinessLikeNumfind(Long businessId) {
return queryFactory
.selectFrom(businessLike)
.where(businessLike.business.id.eq(businessId))
.fetchCount();
}
}
BusinessLikeRepository
@Transactional(readOnly = true)
public interface BusinessLikeRepository extends JpaRepository<BusinessLike, Long>, BusinessLikeRepositoryCustom {
}
BusinessLikeRepositoryImpl
public interface BusinessLikeRepositoryCustom {
Optional<BusinessLike> exist(String mail, Long businessId);
long BusinessLikeNumfind(Long businessId);
}
BusinessLikeService
@Slf4j
@Service
@Transactional
@RequiredArgsConstructor
public class BusinessLikeService {
private final BusinessLikeRepository businessLikeRepository;
private final BusinessRepository businessRepository;
//좋아요 및 취소
public Boolean pushBusinessLike(BusinessLikeDto businessLikeDto) {
businessLikeRepository.exist(businessLikeDto.getMember().getMail(),
businessLikeDto.getBusinessId())
.ifPresentOrElse(businessLike -> businessLikeRepository.deleteById(businessLikeDto.getBusinessId()),
()-> {
Business business = getBusiness(businessLikeDto);
businessLikeRepository.save(new BusinessLike(businessLikeDto.getMember(), business));
});
return true;
}
@Transactional(readOnly = true)
private Business getBusiness(BusinessLikeDto businessLikeDto) {
return businessRepository.findById(businessLikeDto.getBusinessId())
.orElseThrow(() -> new IllegalArgumentException("해당 게시글은 존재하지 않습니다."));
}
// 좋아요 개수
@Transactional(readOnly = true)
public BusinessLikeResponseDto getBusinessLikeInfo(BusinessLikeDto businessLikeDto) {
long businessLikeNum = getBusinessLikeNum(businessLikeDto);
boolean check = checkPushedLike(businessLikeDto);
return new BusinessLikeResponseDto(businessLikeNum, check);
}
@Transactional(readOnly = true)
public boolean checkPushedLike(BusinessLikeDto businessLikeDto) {
return businessLikeRepository.exist(businessLikeDto.getMember().getMail(), businessLikeDto.getBusinessId())
.isPresent();
}
@Transactional(readOnly = true)
public long getBusinessLikeNum(BusinessLikeDto businessLikeDto) {
return businessLikeRepository.BusinessLikeNumfind(businessLikeDto.getBusinessId());
}
}
BusinessLikeServiceTest
@SpringBootTest
@Transactional
public class BusinessLikeServiceTest {
@Autowired
BusinessLikeService businessLikeService;
@Autowired
MemberService memberService;
@Autowired
BusinessService businessService;
@Test
public void 좋아요() throws Exception{
//given
Member member = new Member();
member.setNickname("testMember");
member.setMail("testMember@test.com");
member.setPassword("test1234!");
member.setMember_type("B");
member.setMember_status("J");
member.setHint_password("hint_01");
member.setAnswer_password("answer");
member.setUpdated_at(now());
member.setCreated_at(now());
memberService.join(member);
Member findMember = memberService.findOne(member.getMail());
Business business = new Business();
business.setBusinessName("테스트밥집");
business.setHomepage("test.com");
business.setPhone("010-1234-5678");
business.setAddress("제주특별자치도 제주시 첨단로 242");
business.setLng((float) 33.450701);
business.setLat((float) 126.570667);
business.setCreated_at(now());
business.setUpdated_at(now());
businessService.join(business);
Business findBusiness = businessService.findOne(business.getId());
//when
BusinessLikeDto businessLikeDto = new BusinessLikeDto(findMember,findBusiness.getId());
businessLikeService.pushBusinessLike(businessLikeDto);
System.out.println("businessLikeDto = " + businessLikeDto.getBusinessId());;
//then
assertEquals(1,businessLikeService.getBusinessLikeInfo(businessLikeDto).getBusinessLikeNum());
}
}
안녕하세요 강사님!
처리했습니다. 우선 예약어로 걸릴 exist나 like는 뺐습니다.
그리고 스프링 데이터 JPA 강의 다시 들어보니
제가 Impl을 repositoryImpl로 해야하는데 customrepositoryImpl로 구현했네요ㅠㅠㅠㅠ수정하고 처리 완료했습니다. 감사합니다!!!!!!