使用Spring@Procedure调用StoredProcedure而不绑定到表

使用Spring@Procedure调用StoredProcedure而不绑定到表,spring,stored-procedures,spring-data-jpa,spring-repositories,Spring,Stored Procedures,Spring Data Jpa,Spring Repositories,我想知道是否可以调用存储过程而不必将其绑定到表/模型 在我的例子中,数据库中有一个发送邮件的存储过程。我想从春季开始称之为: public interface SendEmail extends org.springframework.data.repository.Repository<Email, Long> { @Procedure(procedureName = "send_email") void sendEmail(String sender, Strin

我想知道是否可以调用存储过程而不必将其绑定到表/模型

在我的例子中,数据库中有一个发送邮件的存储过程。我想从春季开始称之为:

public interface SendEmail extends org.springframework.data.repository.Repository<Email, Long> {
    @Procedure(procedureName = "send_email")
    void sendEmail(String sender, String recipient, String ccRecipient, String subject, String message);
}
然后我得到以下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve id type of interface x.x.x.x.SendEmail!
    at org.springframework.data.repository.core.support.DefaultRepositoryMetadata.resolveIdType(DefaultRepositoryMetadata.java:81) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.DefaultRepositoryMetadata.<init>(DefaultRepositoryMetadata.java:52) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.AbstractRepositoryMetadata.getMetadata(AbstractRepositoryMetadata.java:71) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryMetadata(RepositoryFactorySupport.java:233) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:260) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 41 common frames omitted
Description:

Field sendEmail in x.x.x.EmailService required a bean of type 'x.x.x.SendEmail' that could not be found.


Action:

Consider defining a bean of type 'x.x.x.SendEmail' in your configuration.
然后我得到以下错误:

Caused by: java.lang.IllegalArgumentException: Could not resolve id type of interface x.x.x.x.SendEmail!
    at org.springframework.data.repository.core.support.DefaultRepositoryMetadata.resolveIdType(DefaultRepositoryMetadata.java:81) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.DefaultRepositoryMetadata.<init>(DefaultRepositoryMetadata.java:52) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.AbstractRepositoryMetadata.getMetadata(AbstractRepositoryMetadata.java:71) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepositoryMetadata(RepositoryFactorySupport.java:233) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:260) ~[spring-data-commons-1.13.8.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean.afterPropertiesSet(JpaRepositoryFactoryBean.java:101) ~[spring-data-jpa-1.11.8.RELEASE.jar:na]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE]
    ... 41 common frames omitted
Description:

Field sendEmail in x.x.x.EmailService required a bean of type 'x.x.x.SendEmail' that could not be found.


Action:

Consider defining a bean of type 'x.x.x.SendEmail' in your configuration.
作为结论,我需要有一个名为
Email
的模型,该模型映射到数据库中的一个表,以便上面的工作。为什么会这样


(我试图用
@Repositories
@Component
对接口进行注释)

基于存储库的域驱动设计概念:一个行为类似于聚合根集合但内容存储在某个持久存储中的对象。因此,
存储库
需要知道它负责的聚合根以及它的id类型,以便在存储中找到它

对于JPA存储库,这意味着聚合必须是JPA映射的实体。当然,基于POJO实体并由存储过程支持实现存储库是可能的。这个想法没有什么错,只是它不是一个常见的用例,而且对于您的用例来说,可能有点过头了


如果只想调用存储过程,最好使用一个简单的Springbean和一个
JdbcTemplate

,您可以这样使用用户:

@Repository
public class RepositoryClass implements DataRepository {
    @PersistenceContext
    private EntityManager em;
}

谢谢你的回答。我确实提出了一个解决方案,我@Autowire一个EntityManager,然后生成一个StoredProcedure对象:StoredProcedureStoredProcedure=EntityManager.CreateStoreProcedureQuery(“发送电子邮件”);这也很好…我知道这已经很晚了,但是你能分享一下你是如何在github上完成的吗,这会有帮助,或者是材料吗?是的。。。这个答案的一些代码会很有帮助。事实上,这充其量只是间接的帮助。