【问题标题】:how to map a non-entity result set coming from a NameStoredProcedure in JPA 2.1?如何映射来自 JPA 2.1 中名称存储过程的非实体结果集?
【发布时间】: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


    【解决方案1】:

    您不能使用StoredProcedureQuery 将存储过程结果集映射到非实体类。 但是,如果您将使用(如果您可以使用 JPQL 查询而不是存储过程调用)TypedQuey,您可以使用JPQL constructor expression@SqlResultSetMappingNativeQuery

    【讨论】:

      【解决方案2】:

      您可以使用@SqlResultSetMapping 和@ConstructorResult 来实现

      @SqlResultSetMapping(name = "CalendarsMapping", classes = {
          @ConstructorResult(targetClass = Calendar.class, columns = { @ColumnResult(name = "OutDate"),
                  @ColumnResult(name = "CategoryID", type = Long.class), @ColumnResult(name = "CategoryName"),
                  @ColumnResult(name = "DepositDate"), @ColumnResult(name = "EventDate"),
                  @ColumnResult(name = "EventType"), @ColumnResult(name = "Status"),
                  @ColumnResult(name = "Effective", type = String.class),
                  @ColumnResult(name = "Indicator", type = String.class), @ColumnResult(name = "TermDate"),
                  @ColumnResult(name = "TID"), @ColumnResult(name = "TName"), @ColumnResult(name = "Series"),
                  @ColumnResult(name = "Symbol"), @ColumnResult(name = "TType", type = String.class) }) })
      
      
      
      StoredProcedureQuery procedureQuery = entityManager.createStoredProcedureQuery(
                  "GetDepAndTerm", "CalendarsMapping");
      procedureQuery.getResultList();
      

      【讨论】:

      • 我已经关注了这个,但仍然是运行时异常,未知实体。有什么想法吗?
      • 嗨@GowthamiReddy,您找到解决方案了吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-05
      • 1970-01-01
      • 2017-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多