Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何调用从JPA返回值的自定义Oracle函数_Java_Oracle_Jpa - Fatal编程技术网

Java 如何调用从JPA返回值的自定义Oracle函数

Java 如何调用从JPA返回值的自定义Oracle函数,java,oracle,jpa,Java,Oracle,Jpa,我在oracle中有一个自定义函数,在删除记录后返回一个数字。我可以在sql\u plus中得到一个值,比如 call my_function(4) into :out_number; 其中out_number是定义为number的变量 当我执行“打印输出编号”时,我可以验证输出编号是否有值 我的问题是如何从JPA调用这个函数 我已经试过了 Query query = em.createNativeQuery("{call my_function(?)}"); query.serParamet

我在oracle中有一个自定义函数,在删除记录后返回一个数字。我可以在sql\u plus中得到一个值,比如

call my_function(4) into :out_number;
其中out_number是定义为number的变量

当我执行“打印输出编号”时,我可以验证输出编号是否有值

我的问题是如何从JPA调用这个函数

我已经试过了

Query query = em.createNativeQuery("{call my_function(?)}");
query.serParameter(1, 4);
return query.executeUpdate();
得到了一个错误,基本上我的函数没有定义。如何在SQL\u PLUS上获取调用…中的返回值

如果这种方法不可取,有人能推荐一些建议吗?JPA是我目前唯一的选择。我可以创建一个存储过程,但我不确定是否可以从中获取返回值,因为不支持OUT参数


提前谢谢你的帮助

我用它在oracle中执行本机函数:

Query query = em.createNativeQuery("select my_function(:param) from dual");
query.setParameter("param", 4);
return query.getSingleResult();

如果您使用的是EclipseLink,那么可以使用StoredFunctionCall对象来调用这样的存储函数


您还可以使用@NamedStoredFunctionQuery将其定义为命名查询。

这是一个相当老的问题,但我在这里没有找到正确的解决方案。。。 以下是在我的项目中起作用的代码:

    EntityManager em;
    String functionName = "NameOfStoredFunction";
    Map<String, Object> arguments = new LinkedHashMap();
    arguments.put("nameOfArgument", "value");
    StoredFunctionCall functionCall = new StoredFunctionCall();
    functionCall.setProcedureName(functionName);
    functionCall.setResult("RESULT", String.class);

    for(String key : arguments.keySet()){
        functionCall.addNamedArgumentValue(key, arguments.get(key));
    }

    ValueReadQuery valQuery = new ValueReadQuery();
    valQuery.setCall(functionCall);

    Query query = ((JpaEntityManager)em.getDelegate()).createQuery(valQuery);

    String call_result = (String)query.getSingleResult();
EntityManager-em;
字符串functionName=“NameOfStoredFunction”;
Map arguments=new LinkedHashMap();
参数put(“参数名称”、“值”);
StoredFunctionCall functionCall=新的StoredFunctionCall();
functionCall.setProcedureName(functionName);
functionCall.setResult(“RESULT”,String.class);
for(字符串键:arguments.keySet()){
functionCall.addNamedArgumentValue(key,arguments.get(key));
}
ValueReadQuery valQuery=新的ValueReadQuery();
setCall(functionCall);
Query Query=((JpaEntityManager)em.getDelegate()).createQuery(valQuery);
字符串调用_result=(字符串)query.getSingleResult();
来自:

首先,我们可以简单地调用Oracle函数,就像调用任何其他SQL查询一样:

BigDecimal commentCount = (BigDecimal) entityManager
    .createNativeQuery(
        "SELECT fn_count_comments(:postId) FROM DUAL"
    )
    .setParameter("postId", 1L)
    .getSingleResult();
另一种方法是使用普通JDBC API调用数据库函数:

Session session = entityManager.unwrap( Session.class );

final AtomicReference<Integer> commentCount = 
    new AtomicReference<>();

session.doWork( connection -> {
    try (CallableStatement function = connection
            .prepareCall(
                "{ ? = call fn_count_comments(?) }"
            )
        ) {
        function.registerOutParameter( 1, Types.INTEGER );
        function.setInt( 2, 1 );
        function.execute();
        commentCount.set( function.getInt( 1 ) );
    }
} );
Session Session=entityManager.unwrap(Session.class);
最终原子引用注释计数=
新原子引用();
会话.工作(连接->{
try(CallableStatement函数=连接
.准备呼叫(
“{?=调用fn\u count\u注释(?)”
)
) {
function.registerOutParameter(1,Types.INTEGER);
函数setInt(2,1);
函数execute();
commentCount.set(function.getInt(1));
}
} );

使用Spring数据JPA/Spring JDBC时,可以使用
SimpleJDCCall
调用函数和过程

对于此类函数头:

FUNCTION get_text (p_param IN VARCHAR2) RETURN VARCHAR2
使用此呼叫:

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 MyRepository {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public String callGetTextFunction(String param) {
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withCatalogName("PKG_PACKAGE") //package name
                .withFunctionName("GET_TEXT"); //function name
        SqlParameterSource paramMap = new MapSqlParameterSource()
                .addValue("p_param", param));
        //First parameter is function output parameter type.
        return jdbcCall.executeFunction(String.class, paramMap));
    }

}

图尔斯基,我不能用“选择。。。因为my_函数执行delete语句(DML)。我已尝试使用BEGIN my_函数(?);结束;但是,我没有运气得到一个返回值。上面的代码适用于修改数据库中数据的函数(使用DML语句)。我想这只与EclipseLink有关。这个StoredFunctionCall对象是从哪里得到的?