2016-11-10 58 views
2

我想從使用休眠的實體管理器的db2數據庫調用存儲過程,並返回結果作爲對象。我正在使用@NamedStoredProcedureQuery註釋,它似乎不是由hibernate發現的。我收到錯誤消息:「找不到@NamedStoredProcedureQuery,並使用該名稱:Create_Division」。任何想法都會很棒。休眠沒有@NamedStoredProcedureQuery被找到與該名稱

豆:

<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="persistenceUnitName" value="demoJPAUnit" /> 
    <property name="packagesToScan"> 
     <list> 
      <value>com.merchantBoarding.db2.queryObjects</value> 
     </list> 
    </property> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" /> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
      <prop key="hibernate.archive.autodetection">class,hbm</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.dialect">org.hibernate.dialect.DB2Dialect</prop> 
     </props> 
    </property> 
</bean> 

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
    <property name="entityManagerFactory" ref="entityManagerFactoryBean" /> 
</bean> 

查詢輸出對象:

@NamedStoredProcedureQuery(
     name="Create_Division", 
     procedureName="MD.PMDBD017", 
     resultClasses = {CreateDivisionResult.class}, 
     parameters={ 
       @StoredProcedureParameter(name="IN_ENTY",mode=ParameterMode.IN,type=Integer.class), 
       @StoredProcedureParameter(name="IN_PARNT_ENTY",mode=ParameterMode.IN,type=Integer.class), 
       @StoredProcedureParameter(name="IN_ACTION",mode=ParameterMode.IN,type=String.class), 
       @StoredProcedureParameter(name="IN_ENTY_XREF",mode=ParameterMode.IN,type=String.class), 
       @StoredProcedureParameter(name="OUT_ENTY",mode=ParameterMode.OUT,type=Integer.class), 
       @StoredProcedureParameter(name="OUT_ID",mode=ParameterMode.OUT,type=String.class), 
       @StoredProcedureParameter(name="OUT_ENTY_XREF",mode=ParameterMode.OUT,type=String.class), 
       @StoredProcedureParameter(name="OUT_SQLCODE",mode=ParameterMode.OUT,type=Integer.class), 
       @StoredProcedureParameter(name="OUT_STATUS",mode=ParameterMode.OUT,type=String.class), 
       @StoredProcedureParameter(name="OUT_ERRORTEXT",mode=ParameterMode.OUT,type=String.class), 
     } 
) 
public class CreateDivisionResult implements Serializable { 

    @Id 
    public String OUT_ENTY; 
    public String OUT_ID; 
    public String OUT_ENTY_XREF; 
    public String OUT_SQLCODE; 
    public String OUT_STATUS; 
    public String OUT_ERRORTEXT; 

} 

DAO代碼:

StoredProcedureQuery query = manager.createNamedStoredProcedureQuery("Create_Division"); 
query.setParameter("IN_ENTY", 111); 
query.setParameter("IN_PARNT_ENTY", 11111); 
query.setParameter("IN_ACTION", "CREATE"); 
query.setParameter("IN_ENTY_XREF", "ED"); 
List<CreateDivisionResult> results = query.getResultList(); 

編輯: 所以我添加@Entity並沒有解決原來的錯誤,但輸出參數仍然沒有映射到CreateDivisionResult對象。我只能用query.getOutputParameterValue獲取每個單獨的字段。這僅僅是JPA的限制嗎?

回答

2

@NamedStoredProcedureQuery應該應用到實體類或映射類

看來你缺少@Entity在你的「CreateDivisionResult」

+0

感謝您的答覆。所以我的理解是@Entity將類綁定到數據庫中的特定表。在這種情況下,輸出字段不是來自任何表格。它們只是從存儲過程本身生成的。這仍然是我使用@Entity的情況嗎? – Lezorte

+0

是的,你仍然必須 – kuhajeyan