用Hibernate和Spring调用存储过程

std*_*all 2 mysql spring stored-procedures hibernate

网上有很多例子描述了如何使用Hibernate调用存储过程,但是,当使用Spring时,图片会有所改变.

我在MySQL中有一个存储过程,我想调用它:在SQL中我需要编写以下内容:

CALL inrange(32.342324,32.234234);
It returns a row with the following: `{INT},{INT},{FLOAT}`
Run Code Online (Sandbox Code Playgroud)

使用Spring,我使用HibernateTemplate执行hibernate操作的方式,我知道你们中的一些人不喜欢它,但这就是我开始时项目的方式,我不是那么渴望改变它,也许是将来...

目前,我在Java中有以下代码,它试图调用该过程:

List<Object[]> resultset = hibernateTemplate
                               .findByNamedQuery("inrange",
                                    person.getAddress().getLatitude(),
                                    person.getAddress().getLongitude());
Run Code Online (Sandbox Code Playgroud)

当我运行它时,我得到以下Hibernate异常:

org.springframework.orm.hibernate3.HibernateSystemException:
   Named query not known: inrange;
Run Code Online (Sandbox Code Playgroud)

我认为这是发生的事实,我没有在hibernate中声明存储过程.我的问题是:

  • 我该怎么声明呢?
  • 有没有一种特殊的方法在Spring的应用程序上下文文件中声明它?

Seb*_*ber 5

您可以在hibernate中调用本机sql查询.

看看这个链接:http: //www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/

顺便说一下,如果你想调用存储过程,你可以简单地使用Spring JdbcTemplate.

请注意,hibernate扩展可以满足您的需求:http: //www.hibernatespatial.org/