Spring Data JPA calling Oracle Function
You can call your function via native query and get result from dual.
public interface HelloWorldRepository extends JpaRepository<HelloWorld, Long> {
@Query(nativeQuery = true, value = "SELECT PKG_TEST.HELLO_WORLD(:text) FROM dual")
String callHelloWorld(@Param("text") String text);
}
Note that it won't work if your function is using DML statements. In this case you'll need to use @Modyfing
annotation over query, but then the function itself must return number due to @Modyfing
return type restrictions.
You can also implement your CustomRepository
and use SimpleJdbcCall
:
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Repository;
@Repository
public class HelloWorldRepositoryImpl implements HelloWorldRepositoryCustom {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public String callHelloWorld() {
SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
.withCatalogName("PKG_TEST") //package name
.withFunctionName("HELLO_WORLD");
SqlParameterSource paramMap = new MapSqlParameterSource()
.addValue("param", "value"));
//First parameter is function output parameter type.
return jdbcCall.executeFunction(String.class, paramMap));
}
}