使用JPA 2.1调用存储过程并执行Sql Result SetMapping到Pojo

时间:2016-09-02 14:50:51

标签: java hibernate jpa stored-procedures sqlresultsetmapping

我试图调用一个使用JPA返回非实体对象的存储过程。这就是我的工作。

StoredProcedureQuery query = entityManager.createStoredProcedureQuery(
            "DepAndTerm", "CalendarsMapping");

query.registerStoredProcedureParameter(0, String.class, ParameterMode.IN);
query.registerStoredProcedureParameter(1, String.class, ParameterMode.IN);
query.registerStoredProcedureParameter(2, String.class, ParameterMode.IN);
query.registerStoredProcedureParameter(3, String.class, ParameterMode.IN);
query.registerStoredProcedureParameter(4, void.class, ParameterMode.REF_CURSOR);

query.setParameter(0, startDate);
query.setParameter(1, endDate);
query.setParameter(2, eventType);
query.setParameter(3, tType);

query.execute();

List<TCalendar> tCalendars = (List<TCalendar>) query.getOutputParameterValue(4);

@SqlResultSetMapping(
  name = "CalendarsMapping",
  classes = {
    @ConstructorResult(
      targetClass = TCalendar.class,
      columns = {
        @ColumnResult(name="BOutDate"),                 
        @ColumnResult(name="CategoryID"),
        @ColumnResult(name="CategoryName")
      }
    )
  }
)

我遇到了异常

java.lang.UnsupportedOperationException: org.hibernate.dialect.Oracle10gDialect does not support resultsets via stored procedures
at org.hibernate.dialect.Dialect.getResultSet(Dialect.java:1645) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.engine.jdbc.cursor.internal.StandardRefCursorSupport.getResultSet(StandardRefCursorSupport.java:125) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.procedure.internal.ProcedureOutputsImpl$ProcedureCurrentReturnState.buildExtendedReturn(ProcedureOutputsImpl.java:106) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.result.internal.OutputsImpl$CurrentReturnState.buildOutput(OutputsImpl.java:209) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.result.internal.OutputsImpl$CurrentReturnState.getOutput(OutputsImpl.java:187) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.result.internal.OutputsImpl.getCurrent(OutputsImpl.java:108) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.internal.StoredProcedureQueryImpl.execute(StoredProcedureQueryImpl.java:234) ~[hibernate-entitymanager-4.3.11.Final.jar:4.3.11.Final]

我不知道我做错了什么。

1 个答案:

答案 0 :(得分:0)

在Hibernate 5.x中更新为12c方言解决了这个问题。我正在使用hibernate 4.2和10g方言。