반응형
Java Spring Boot Project의 저장 프로시저가 null을 출력으로 반환합니다.
Spring Boot 프로젝트에서 저장 프로시저를 사용하여 출력 값을 가져오려고 하지만 프로젝트에서는 항상 null을 반환합니다.하지만 하이디SQL을 통해 절차를 호출하면 작동하고 올바른 값을 얻을 수 있습니다.
그래서 제 자바 코드와 관련이 있습니다.영향을 받는 메서드를 디버깅했지만 null을 반환하는 이유를 찾을 수 없습니다.
이미 다른 게시물을 찾아보았지만 특정 이슈와 일치하는 내용을 찾을 수 없었습니다.
다음은 저장 프로시저를 사용하는 방법입니다.
회사 리소스 서비스 Impl
@Service
public class CompanyResourceServiceImpl implements CompanyResourceService {
@PersistenceContext
private EntityManager entityManager;
...
private int getMetalResourceByPlayerId(int theId) {
StoredProcedureQuery theQuery = entityManager.createStoredProcedureQuery("getAllMetalFromCompaniesByPlayerId");
theQuery.registerStoredProcedureParameter(1, Integer.class, ParameterMode.IN);
theQuery.registerStoredProcedureParameter(2, BigDecimal.class, ParameterMode.OUT);
theQuery.setParameter(1, theId);
theQuery.execute();
BigDecimal outAmount = (BigDecimal) theQuery.getOutputParameterValue(2);
return outAmount.intValue();
}
...
}
다음은 저장 절차입니다.
회사로부터 모든 금속을 얻는 방법플레이어 ID
CREATE DEFINER=`root`@`localhost` PROCEDURE `getAllMetalFromCompaniesByPlayerId`(
IN `playerId` INT,
OUT `metalSum` DECIMAL(19,2)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
SELECT sum(cr.amount) as metalSum
FROM company_resource cr
JOIN company c ON (c.id = cr.company_id) WHERE c.player_id = playerId and cr.resource_id = 1;
END
나의 목표는 출력값을 얻어 그것을 사용하는 것입니다.@Scheduled
방법.그리고 제가 말했듯이, 하이디SQL에서는 저장 절차가 작동합니다.
몇 시간 동안 노력한 끝에 저는 그것을 작동시킬 방법을 찾았습니다.
먼저 추가합니다.@NamedStoredProcedureQuery
나에게CompanyResource
엔티티 클래스:
회사 리소스.java
@Entity
@Table(name = "company_resource")
@NamedStoredProcedureQueries({
@NamedStoredProcedureQuery(name = "getAllMetalFromCompaniesByPlayerId",
procedureName = "getAllMetalFromCompaniesByPlayerId",
parameters = {
@StoredProcedureParameter(mode = ParameterMode.IN, name = "playerId", type = Integer.class),
@StoredProcedureParameter(mode = ParameterMode.OUT, name = "metalSum", type = BigDecimal.class)
})
})
@IdClass(CompanyResourcePK.class)
public class CompanyResource {
...
}
그리고 나서 나는 나의 것을 바꿨습니다.getMetalResourceByPlayerId()
의 방법.CompanyResourceServiceImpl
다음과 같이:
회사 리소스 서비스 Impl.java
@Service
public class CompanyResourceServiceImpl implements CompanyResourceService {
@PersistenceContext
private EntityManager entityManager;
...
private int getMetalResourceByPlayerId(int theId) {
StoredProcedureQuery theQuery = entityManager.createNamedStoredProcedureQuery("getAllMetalFromCompaniesByPlayerId");
theQuery.setParameter("Param1", theId);
BigDecimal outAmount = (BigDecimal) theQuery.getSingleResult();
return outAmount.intValue();
}
...
}
언급URL : https://stackoverflow.com/questions/57527592/stored-procedure-in-java-spring-boot-project-returns-null-as-output
반응형
'source' 카테고리의 다른 글
Git 태그의 시간 및 날짜 가져오기 (0) | 2023.08.07 |
---|---|
입력 크기 대 폭 (0) | 2023.08.07 |
Spring-Boot 시작을 중단하려면 어떻게 해야 합니까? (0) | 2023.08.02 |
Oracle TO_DATE NOT throw 오류 (0) | 2023.08.02 |
Mac OS X에서 Android SDK를 설치하는 위치는 무엇입니까? (0) | 2023.08.02 |