使用 Spring @Procedure 调用 StoredProcedure 而不绑定到表 [英] Using Spring @Procedure to call StoredProcedure without binding to a table

查看:26
本文介绍了使用 Spring @Procedure 调用 StoredProcedure 而不绑定到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to know if it is possible to call a stored procedure without having to bind it to a Table/Model?

就我而言,我在数据库中有一个发送邮件的存储过程.我想从 Spring 调用它:

In my case I have a stored procedure in the database that sends a mail. I would like to call this from Spring:

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);
}

上面的代码编译得很好并且运行得很好 - 没问题.唯一的问题是我想摆脱 extends Repository - 我该怎么做?如果我只是删除 (因为 StoredProcedure 不返回任何内容,因此不需要类型):

The above code compiles fine and runs fine - no problem. Only problem is that I would like to get rid of the extends Repository<Email, Long> - how do I do this? If I just delete the <Email, Long> (since the StoredProcedure does not return anything and thus no type is needed):

public interface SendEmail extends org.springframework.data.repository.Repository {
    @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

如果我删除 extends Repository 并且有:

If I delete extends Repository<Email, Long> and have:

public interface SendEmail {
    @Procedure(procedureName = "send_email")
    void sendEmail(String sender, String recipient, String ccRecipient, String subject, String message);
}

然后我收到以下错误:

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 的模型,该模型映射到数据库中的一个表,以便上述工作正常进行.为什么会这样?

As a conclusion I need to have a model called Email that is mapped to a table in the database in order for the above to work. Why is this so?

(我尝试用 @Repositories@Component 注释接口)

(I have tried to annotate the interface with @Repositories and @Component)

推荐答案

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

Repositories are based on the Domain Driven Design concept of Repository: An object that behaves similarly to a collection of aggregate roots but with the content stored in some persistent store. Therefore a Repository needs to know the aggregate root it is responsible for and also it's id type in order to find it in the store.

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

For JPA Repositories this means the aggregate has to be an entity mapped by JPA. It would certainly be possible to implement Repositories based on POJO entities and backed by stored procedures. Nothing wrong with that idea except that it is not a common use case and for your use case probably overkill.

如果你只想调用一个存储过程,你可能最好使用一个简单的 Spring bean 和一个 JdbcTemplate.

If you just want to call a stored procedure you are probably better of with a simple Spring bean and a JdbcTemplate.

这篇关于使用 Spring @Procedure 调用 StoredProcedure 而不绑定到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆