如何映射来自 JPA 2.1 中 NameStoredProcedure 的非实体结果集?

fs2*_*050 5 java postgresql jpa

我正在使用 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 )}
)
Run Code Online (Sandbox Code Playgroud)

下面的代码是在 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;
Run Code Online (Sandbox Code Playgroud)

下面的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;
}
Run Code Online (Sandbox Code Playgroud)

如何更改 @NamedStoredProcedureQuery 以返回/转换非实体类 StoreAndCategoryID?

谢谢,

Jav*_*ick 1

您无法使用 .NET 将存储过程结果集映射到非实体类StoredProcedureQuery。但是,如果您要使用(如果您可以使用 JPQL 查询而不是存储过程调用),您可以在中TypedQuey使用JPQL 构造函数表达式@SqlResultSetMappingNativeQuery