NamedStoredProcedureQuery

NamedStoredProcedureQuery

我正在尝试使用休眠的实体管理器从db2数据库中调用存储过程,并将结果作为对象返回。我正在使用@NamedStoredProcedureQuery批注,它似乎没有被休眠找到。我收到错误消息:“找不到具有该名称的@NamedStoredProcedureQuery:Create_Division”。任何想法都很棒。

豆子:

<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的限制吗?

最佳答案

@NamedStoredProcedureQuery应该应用于Entity类或映射类

似乎您的'CreateDivisionResult'中缺少@Entity

09-06 00:58