java - Oracle 存储过程和 Hibernate

标签 java oracle hibernate

我必须从内存中的数据库调用 pl sql 中的存储过程。 我将脚本放在我的 sql 文件中

create or replace PROCEDURE delete_from_db
(
abi IN ASSEGNO.ABI_ASSEGNO%type,
cab IN ASSEGNO.CAB_ASSEGNO%type,
nAss IN ASSEGNO.NUMERO_ASSEGNO%type
)
IS
BEGIN
delete FROM ASSEGNO WHERE ABI_ASSEGNO = abi AND CAB_ASSEGNO= cab AND NUMERO_ASSEGNO = nAss;
COMMIT;
END delete_from_db;

我尝试使用 Hibernate 从我的实现中调用它,如下所示:

@覆盖 公共(public)无效deleteAssegno(AssegnoId idAssegno){

Session s = getHibernateTemplate().getSessionFactory().openSession();
s.getNamedQuery("delete_from_db").setString(0, idAssegno.getAbiAssegno()).setString(1, idAssegno.getCabAssegno()).setLong(2, idAssegno.getNumeroAssegno());
s.close();

}

但是当我进行测试时,我遇到了这个错误:

org.hibernate.MappingException: Named query not known: delete_from_db
    at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:93)
    at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1407)
    at it.bnl.btre.orchass.busin.dao.impl.AssegnoDaoServiceImpl.deleteAssegno(AssegnoDaoServiceImpl.java:75)
    at it.bnl.btre.orchass.busin.facade.service.impl.CancellazioneDocumentaleServiceImpl.deleteFromDb(CancellazioneDocumentaleServiceImpl.java:109)
    at it.bnl.btre.orchass.busin.facade.service.impl.CancellazioneDocumentaleServiceImpl.deleteFromDoc(CancellazioneDocumentaleServiceImpl.java:70)
    at it.bnl.btre.orchass.busin.facade.CancellazioneDocumentaleFacade.handle(CancellazioneDocumentaleFacade.java:32)
    at it.bnl.btre.orchass.busin.facade.test.CancellazioneDocumentaleFacadeTest.testOk(CancellazioneDocumentaleFacadeTest.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:95)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:56)
    at java.lang.reflect.Method.invoke(Method.java:620)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:255)
    at junit.framework.TestSuite.run(TestSuite.java:250)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我在互联网上搜索,但所有教程都是关于 MySql db 或其他。

谢谢大家!

最佳答案

您需要直接调用存储过程,例如通过创建一个org.springframework.jdbc.object.StoredProcedure:

StoredProcedure proc = new StoredProcedure(jdbcTemplate, "delete_from_db");

定义您的参数:

proc.declareParameter(new SqlParameter(Types.VARCHAR));
...

最后调用过程

proc.execute(<params>);

有关完整示例,请参见Spring JDBC Template for calling Stored Procedures

关于java - Oracle 存储过程和 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44903912/

相关文章:

java - 在 Java DAO 上使用同步是否会导致问题?

oracle - ORA 01400 和 ORA 02296 : Cannot insert null or modify added column properties to NOT NULL

java - validator 测试期间出现 NoSuchMethodError (javax.el 3.0.0)

java - Hibernate插入行外键错误

java - 用于在 NetBeans 中对指向错误域的 session bean 进行单元测试的嵌入式 GlassFish 容器

java - 警报通知未出现

java - 是否有 Linux 命令来打印正在运行的 Java 进程的当前堆栈

java - UFT/QTP : Count child objects in Java Internal Frame

c# - 如何列出数据库中的表名?

java - hibernate 在二级缓存和各种缓存模式的数据库之间执行同步时?