[英]How can I execute a stored procedure with JPA & Spring Data?
我試圖在我的數據庫中調用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
文件(對於Spring):
<?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
注釋時,我都會得到上面提到的異常。
我記得我一直在努力學習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
對象列表,則只編輯自定義存儲庫中的返回值。
我遵循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 {
那么存儲庫的Implementation類將是:
@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才能使其工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.