如何使用JPA和Spring Data执行存储过程?

2022-09-03 07:06:55

我正在尝试调用数据库中的存储过程,但不断收到以下异常:Terminal_GetTicket

PropertyReferenceException: No property getTicket found for type TicketInfo

我已经用一个非常简单的测试实体交叉验证了我的配置,一切似乎都运行良好,但是对于实际情况,有些地方不对劲。

这是我的域实体 ():TicketInfo

@Entity
@NamedStoredProcedureQuery(name = "TicketInfo.getTicket", procedureName = "Terminal_GetTicket", resultClasses = TicketInfo.class, parameters = { 
    @StoredProcedureParameter(mode = ParameterMode.IN, name = "sys_id_game", type = Integer.class)})
public class TicketInfo {

    @Id @GeneratedValue
    private Long id;
    private String idTicket;
    private Integer externalTicketCode;
    private Short sequenseAlert;
    private Integer dlTimeStamp;

所有实例变量都正确定义了其 getter 和 setter,并且存储过程总共有 5 个输出参数与 的属性匹配。TicketInfo

此外,这是我的存储库接口:

public interface TicketInfoRepository extends CrudRepository<TicketInfo, Long> {
    @Transactional(timeout = 5)
    @Procedure
    TicketInfo getTicket(Integer sys_id_game);
}

另外,这是我的文件(春季):context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:repository="http://www.springframework.org/schema/data/repository"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.8.xsd
        http://www.springframework.org/schema/data/repository
        http://www.springframework.org/schema/data/repository/spring-repository-1.5.xsd">

    <context:component-scan base-package="ar.com.boldt.godzilla" />
    <jpa:repositories base-package="xx.xxx.xxx.godzilla.business.dao" />

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="showSql" value="${dataSource.show.sql}" />
        <property name="generateDdl" value="false" />
        <property name="database" value="SQL_SERVER" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <!-- spring based scanning for entity classes -->
        <property name="packagesToScan" value="xx.xxx.xxx.godzilla.business.dao" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache" />
    </bean>

    <bean id="ehcache"
        class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml" />
    </bean>
</beans>

最后是存储过程本身的淡化版本:

ALTER PROCEDURE [Terminal_GetTicket](
 @arg int 
,@res int output
,@res2 int output
)
as
Declare @error int

select 0, 1, 2

RETURN @error

现在,每当我尝试设置注释时,我都会遇到上面提到的异常。@Autowired


答案 1

我记得我一直在为MS SQL存储过程和spring-data-jpa而苦苦挣扎。这就是我能够成功运行它的方式:

型:

@NamedNativeQueries({
    @NamedNativeQuery(
            name = "yourInternalName",
            query = "EXEC [procedure_name] :param1, :param2",
            resultClass = Foo.class
    )
 })
 @Entity
 public class Foo{

     /* Fields, getters, setters*/
}

这很简单。这种方法是不同的,但是,您不是直接声明过程(这也是如果您决定更改RDBS,则不必工作的原因)。

然后,您必须扩展存储库:

public interface FooRepositoryCustom {

     Foo fancyMethodName(arg1, arg2);
}

并直接实现它:

public class FooRepositoryImpl implements FooRepositoryCustom {


@PersistenceContext
EntityManager entityManager;

@Override
public Foo fancyMethodName(arg1, arg2) {

    Query query = entityManager.createNamedQuery("yourInternalName");
    query.setParameter("param1", arg1);
    query.setParameter("param2", arg2);
    return query.getResultList();
}

让我们把它们放在一起:

public interface FooRepository extends CrudRepository<Foo, Long>, FooRepositoryCustom {

}

请注意,如果您决定返回对象列表,则只能在自定义存储库中编辑返回值。Foo


答案 2

我遵循了SirKometas的建议,但我无法让它工作,所以我想出了一些对我有用的东西,我认为从语法的角度来看更好。首先创建如下所示的实体类。

@NamedStoredProcedureQueries({//
    @NamedStoredProcedureQuery(//
            name = "MySP"//
            , procedureName = "my_sp"//
            , parameters = { //
                    @StoredProcedureParameter(mode = ParameterMode.IN, name = "arg", type = String.class)}//
            , resultClasses = Foo.class)//})
@Entity
public class Foo {

然后,存储库的实现类将是:

@Component
public class FooRepositoryImpl implements FooCustomRepository {

    @PersistenceContext
    EntityManager entityManager;

    @Override
    public List<Foo> foo(String arg) {
        Query query = entityManager.createNamedStoredProcedureQuery("MySP");
        query.setParameter("arg", arg);
        return query.getResultList();
    }
}

实现的其余部分就像上面SirKometa的答案一样。还要考虑您必须在应用程序中创建一个 EntityManager Bean 才能使其正常工作。


推荐