Hibernate没有找到具有该名称的@NamedStoredProcedureQuery

时间:2016-11-10 21:13:08

标签: java spring hibernate

我正在尝试使用hibernate的实体管理器从db2数据库调用存储过程,并将结果作为对象返回。我正在使用@NamedStoredProcedureQuery注释,而hibernate似乎找不到它。我收到错误:“没有找到具有该名称的@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的限制吗?

1 个答案:

答案 0 :(得分:7)

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

似乎你错过了'CreateDivisionResult'中的@Entity