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

查看:165
本文介绍了使用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<Email, Long>-我该怎么做?如果我只删除<Email, Long>(因为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<Email, Long>并拥有:

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需要知道它负责的聚合根以及它的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天全站免登陆