JPA 저장소의 다른 테이블에서 데이터 선택
JPA 저장소에 다음 쿼리가 있습니다.
@Repository
public interface PaymentTransactionsDailyFactsRepository extends JpaRepository<PaymentTransactionsDailyFacts, Integer> {
@Query(value = "SELECT " +
" COUNT(*) count, " +
" SUM(amount) volume, " +
" DATE(created_at) date, " +
" YEAR(created_at) year, " +
" MONTH(created_at) month, " +
" WEEK(created_at) week, " +
" DAY(created_at) day, " +
" type transaction_type, " +
" contract_id, merchant_id, terminal_id, " +
" status, card_brand, currency " +
" FROM payment_transactions " +
" WHERE status NOT IN ('pending_async','pending','pending_review','in_progress','new') AND created_at BETWEEN :start_date AND :end_date " +
" GROUP by date, contract_id, merchant_id, terminal_id, transaction_type, status, card_brand, currency", nativeQuery = true)
List<PaymentTransactionsDailyFacts> generateDailyFacts(@Param("start_date") LocalDate start_date, @Param("end_date") LocalDate end_date);
}
그러나 Spring Scheduler에서 실행하면 다음과 같은 오류가 발생합니다.
SQL Error: 1054, SQLState: 42S22
Caused by: java.sql.SQLSyntaxErrorException: No such column: id
MariaDB에서 이 쿼리를 실행하면 정상적으로 동작합니다.
SELECT COUNT(*) count, SUM(amount) volume, DATE(created_at) date, YEAR(created_at) year, MONTH(created_at) month, WEEK(created_at) week, DAY(created_at) day, type transaction_type, contract_id, merchant_id, terminal_id, status, card_brand, currency FROM payment_transactions WHERE status NOT IN ('pending_async','pending','pending_review','in_progress','new') AND created_at BETWEEN '2011-04-11 00:00:01' AND '2029-04-11 00:00:00' GROUP by date, contract_id, merchant_id, terminal_id, transaction_type, status, card_brand, currency
보시다시피 테이블에 대한 네이티브 쿼리를 실행하고 있습니다.payment_transactions하지만 나는 테이블 결과를 기대하고 있다.payment_transactions_daily_facts이 쿼리를 올바르게 구현할 수 있는 방법이 있습니까?
모든 것을 얻고 싶다면PaymentTransactionsDailyFacts주어진 것 사이에 있는 실체start_date그리고.end_date및 그들의status는 ('snowledge_c', 'snow', 'snow') 중 하나가 아닙니다.다음 쿼리를 사용할 수 있습니다.
@Query(value = "SELECT * "
" FROM payment_transactions " +
" WHERE status NOT IN ('pending_async','pending','pending_review','in_progress','new') AND created_at BETWEEN :start_date AND :end_date " +
" GROUP by date, contract_id, merchant_id, terminal_id, transaction_type, status, card_brand, currency", nativeQuery = true)
하지만, 당신이 사용했기 때문에.group by당신의 쿼리에서, 당신은 단지 목록뿐만 아니라 집계 데이터를 얻으려고 하는 것처럼 보입니다.PaymentTransactionsDailyFacts조건을 만족시키는 실체만약 그렇다면 당신은 모든 것을 잘못하고 있는 것입니다.프로젝션을 사용해야 합니다.
다음은 스프링 데이터 투영을 사용하는 방법에 대한 간단한 튜토리얼입니다.https://www.baeldung.com/spring-data-jpa-projections
또는 여기서 공식 문서를 읽을 수 있습니다.https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#프로젝트
업데이트: 프로젝션 사용
먼저 프로젝트 내 어딘가에 다음과 같은 인터페이스를 만듭니다.필요에 따라 반환 유형을 조정해야 합니다.
public interface PtdsDTO { Integer getCount(); BigDecimal getVolume(); LocalDate getDate(); Short getYear(); Short getMonth(); Short getWeek(); Short getDay(); String getTransactionType(); Integer getContactId(); Integer getMerchantId(); Integer getTerminalId(); String getStatus(); String getCardBrand(); String getCurrency(); }그런 다음 해당 저장소 맨 위에 있는 인터페이스를 가져옵니다.
저장소의 반환 유형을 다음으로 변경합니다.
List<PtdsDTO>.List<PtdsDTO> generateDailyFacts(@Param("start_date") LocalDate start_date, @Param("end_date") LocalDate end_date);
바로 그거야.
추가 시도id쿼리 중
@Query(value = "SELECT " +
" id," +
" COUNT(*) count, " +
" SUM(amount) volume, " +
" DATE(created_at) date, " +
" YEAR(created_at) year, " +
" MONTH(created_at) month, " +
" WEEK(created_at) week, " +
" DAY(created_at) day, " +
" type transaction_type, " +
" contract_id, merchant_id, terminal_id, " +
" status, card_brand, currency " +
" FROM payment_transactions " +
" WHERE status NOT IN ('pending_async','pending','pending_review','in_progress','new') AND created_at BETWEEN :start_date AND :end_date " +
" GROUP by date, contract_id, merchant_id, terminal_id, transaction_type, status, card_brand, currency", nativeQuery = true)
List<PaymentTransactionsDailyFacts> generateDailyFacts(@Param("start_date") LocalDate start_date, @Param("end_date") LocalDate end_date);
언급URL : https://stackoverflow.com/questions/56926667/select-data-from-another-table-from-jpa-repository
'source' 카테고리의 다른 글
| MariaDB 설치로 Mysql 데이터베이스가 이동되었습니다. (0) | 2023.01.26 |
|---|---|
| bs4.FeatureNotFound: 요청한 기능을 가진 트리 빌더를 찾을 수 없습니다.lxml파서 라이브러리를 설치해야 합니까? (0) | 2023.01.26 |
| .whl 파일이 있는 Python 패키지를 설치하려면 어떻게 해야 하나요? (0) | 2023.01.26 |
| MySQL 데이터베이스의 실제 크기를 얻는 방법 (0) | 2023.01.26 |
| 라라벨:속성별 컬렉션에서 개체 가져오기 (0) | 2023.01.26 |