用Hibernate执行Oracle存储过程(Executing Oracle Stored Procedure with Hibernate)
我一直在尝试使用Hibernate来执行Oracle存储过程。 这不是为了生产 - 而是为了我正在追求的Java源代码解析项目。 简而言之,我无法从Oracle存储过程中返回一个值。
我已经搜索并阅读了SO,Hibernate社区/文档(Native SQL章节)链接中的所有相关链接并尝试了这些建议,但无法让它们工作。 以下是我的来源 - 我只包括相关部分。
我的实体类。 我已经为PL / SQL OUT参数保留了第一个参数。
Login.java
@NamedNativeQuery( name = "getLoginDet", query = "call GET_LOGIN_DET(?,:userId)", resultClass = Login.class) @Entity @Table(name = "T_LOGIN_DET") public class Login {
Oracle存储过程:
GET_LOGIN_DET.sql
。 第一个参数是按照Hibernate Spec的OUT REFCURSOR
create or replace PROCEDURE GET_LOGIN_DET(listLogin OUT SYS_REFCURSOR,userId IN VARCHAR2) AS BEGIN OPEN listLogin FOR SELECT * FROM T_LOGIN_DET WHERE USER_ID = userId; END GET_LOGIN_DET;
我的DAO类:我只绑定了命名参数,忽略了第一个? 在指定的查询中。
Session session = sessionFactory.openSession(); List results = session.getNamedQuery("getLoginDet").setParameter("userId", u.getUserId()).list();
我的休眠配置
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="annotatedClasses"> <list> <value>com.cogn.gto.sea.employee.entity.Employee</value> <value>com.cogn.gto.sea.employee.entity.Department</value> <value>com.cogn.gto.sea.login.entity.User</value> <value>com.cogn.gto.sea.login.entity.Login</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
但是,我总是以Hibernate QueryException / Java SQLException结束
Expected positional parameter count: 1, actual parameters: [] [{call GET_LOGIN_DET(?,:userId)}]
我尝试过
{? = call GET_LOGIN_DET(:userId)}
{? = call GET_LOGIN_DET(:userId)}
,call GET_LOGIN_DET(?,:userId)
无效。 我的要求是调用我已列出的过程,并将结果返回到DAO类。 有人能让我知道我错过了什么吗?I have been trying to execute an Oracle Stored Procedure using Hibernate. This is not for production - but for a Java source parsing project, that I'm pursuing. To put it in simple terms, I'm not able to return a value from an Oracle Stored Proc.
I have searched and read all relevant links from SO, Hibernate community/documentation(Native SQL chapter) links and tried out the suggestions, but somehow couldn't get them to work. Below are my sources - I'm including only the relevant parts.
My Entity Class. I have reserved the first parameter for a PL/SQL OUT parameter.
Login.java
@NamedNativeQuery( name = "getLoginDet", query = "call GET_LOGIN_DET(?,:userId)", resultClass = Login.class) @Entity @Table(name = "T_LOGIN_DET") public class Login {
Oracle Stored Proc :
GET_LOGIN_DET.sql
. The first parameter isOUT REFCURSOR
as per Hibernate Speccreate or replace PROCEDURE GET_LOGIN_DET(listLogin OUT SYS_REFCURSOR,userId IN VARCHAR2) AS BEGIN OPEN listLogin FOR SELECT * FROM T_LOGIN_DET WHERE USER_ID = userId; END GET_LOGIN_DET;
My DAO Class : I'm binding only the named parameter, ignoring the first ? in the named Query.
Session session = sessionFactory.openSession(); List results = session.getNamedQuery("getLoginDet").setParameter("userId", u.getUserId()).list();
My Hibernate Config
<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="configurationClass"> <value>org.hibernate.cfg.AnnotationConfiguration</value> </property> <property name="annotatedClasses"> <list> <value>com.cogn.gto.sea.employee.entity.Employee</value> <value>com.cogn.gto.sea.employee.entity.Department</value> <value>com.cogn.gto.sea.login.entity.User</value> <value>com.cogn.gto.sea.login.entity.Login</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> <!-- <prop key="hibernate.hbm2ddl.auto">update</prop> --> <prop key="hibernate.show_sql">true</prop> </props> </property> </bean>
However, I always end up with Hibernate QueryException / Java SQLException
Expected positional parameter count: 1, actual parameters: [] [{call GET_LOGIN_DET(?,:userId)}]
I have tried variations of
{? = call GET_LOGIN_DET(:userId)}
,call GET_LOGIN_DET(?,:userId)
to no avail. My requirement is to call the procedure that I have listed and get the result back in the DAO class. Can someone lead me to what exactly I'm missing here ?
原文:https://stackoverflow.com/questions/41166999
最满意答案
尝试Google Protocol Buffers 。 它有一堆.NET 实现 。
Try Google Protocol Buffers. There are a bunch of .NET implementations of it.
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
这两个都是C函数: atoi在stdlib.h sprintf在stdio.h 由于这些都包含在C中,因此既不需要C ++流。 Both of these are C functions: atoi is in stdlib.h sprintf is in stdio.h And since these are included as part of C, neither require C++ streams.
-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
// loading bytes from a file is very easy in C#. The built in System.IO.File.ReadAll* methods take care of making sure every byte is read properly. byte[] bytes = System.IO.File.ReadAllBytes("myfile.pdf"); // munge bytes with whatever pdf software you wan ...
-
尝试Google Protocol Buffers 。 它有一堆.NET 实现 。 Try Google Protocol Buffers. There are a bunch of .NET implementations of it.
-
XML到JSON和反之亦然(XML To JSON and Vice Versa)[2021-07-29]
好的,我没有收到有关此事的任何更新。 所以,我自己写了。 我只是将XML转换为JSON。 Xml2Json将XML字符串表示为JSON对象。 如果设置flags =“attributes”,则元素属性将打包在JSONobject的XmlAttributes子级中。 在XML中,没有办法说,您将要遇到一个元素数组或单个元素。 因此,默认情况下,解析器会从元素中创建一个JSON对象,除非它对另一个具有相同名称的xml元素进行计数,然后将Object转换为Array。 这是Xml2Json项目密钥:18Aji4g ... -
我会提供第三个选项。 您可以使用[JsonProperty]属性标记您的类属性,以将它们与不同名称的JSON属性进行匹配。 这样你就不需要改变你的JSON,也不需要改变你的类的命名规则。 这里有一个简单的例子: JSON: { "item-identifier": 1234, "item-name": "foo" } 类定义: public class Item { [JsonProperty("item-identifier")] public int Id { get; ...
-
你会想使用SetVariable和CallFunction来实现这一点。 这是一个关于Flash和C#interop的很好的简短教程 。 You'll want to use SetVariable and CallFunction to achieve this. Here is a good, short tutorial on Flash and C# interop.
-
我发现http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=Json&ReleaseId=6987这是Json.NET的打火机(和旧版本),也只需要.Net Framework 2.0它只有60K。 I found http://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=Json&ReleaseId=6987 which is a lighter (and o ...
-
有没有办法将.net C#翻译成PHP,反之亦然?(Is thare any way to translate .net C# into PHP and vice versa?)[2021-09-20]
Phalanger是一款.NET PHP编译器。 如果你用它编译php,你可以在Reflector中打开程序集来获取C#或VB.NET。 不知道任何事情会反过来做。 There is Phalanger, which is a .NET php compiler. If you compile php with it, you can then open the assembly in Reflector to get the C# or VB.NET. Don't know about anything ...