樣品Oracle函數:
CREATE OR REPLACE FUNCTION f_get_random_number
RETURN NUMBER
AS
BEGIN
RETURN TRUNC(dbms_random.value(1,100));
END;
解決方案1:使用SQL查詢
BigDecimal val = (BigDecimal)entityManager
.createNativeQuery("SELECT f_get_random_number FROM DUAL")
.getSingleResult();
解決方案2:使用JDBC API
Session session = entityManager.unwrap(Session.class);
Integer out = session.doReturningWork(
connection -> {
try (CallableStatement function = connection.prepareCall("{ ? = call f_get_random_number }")) {
function.registerOutParameter(1, Types.INTEGER);
function.execute();
return function.getInt(1);
}
});
的可能重複[從JPA調用oracle函數](http://stackoverflow.com/questions/5688152/calling-an-oracle-function-from-jpa) – 2014-09-03 19:20:00
試試[this](http://stackoverflow.com/a/14722588)/599528) – user75ponic 2014-09-04 07:01:59