【发布时间】:2014-11-14 09:14:50
【问题描述】:
我正在使用 jpa 2.1 调用 postgresql 过程,并希望将结果集转换为名为 StoreAndCategoryID 的非实体类,其中包含两个整数字段:storeid、categoryid。这两个字段是过程返回的字段。
@NamedStoredProcedureQuery(
name = "Category.func_getcategorybytextsearchid",
procedureName = "func_getcategorybytextsearchid",
parameters = {@StoredProcedureParameter(name = "textsearchid", type = Integer.class,
mode = javax.persistence.ParameterMode.IN ),
@StoredProcedureParameter(name = "mycursor", type = void.class,
mode = ParameterMode.REF_CURSOR )}
)
下面的代码是在 Postgresql 上执行的过程
CREATE OR REPLACE FUNCTION func_getcategorybytextsearchid(textsearchid integer )
RETURNS refcursor AS
$BODY$
declare mycursor refcursor ;
BEGIN
mycursor = 'mycursor';
OPEN mycursor FOR (
select storeid, categoryid
from item_full_text_search
where itemfulltextsearchid = $1;
RETURN mycursor ;
end;
下面的 java 代码显示了我是如何调用该过程的
StoredProcedureQuery q =
em.createNamedStoredProcedureQuery("Category.func_getcategorybytextsearchid");
q.setParameter("textsearchid", textsearchid);
if (q.execute())
{
//the result set needs to convert to StoreAndCategoryID class if possible.
StoreAndCategoryID storeAndCategoryID = q.getOutputParameterValue("mycursor");
}
public class StoreAndCategoryID
{
int storeid;
int categoryid;
}
如何更改 @NamedStoredProcedureQuery 以返回/转换非实体类 StoreAndCategoryID?
谢谢,
【问题讨论】:
标签: java postgresql jpa