我如何使用 spring 和 jpa 调用存储过程

发布于 2024-12-11 12:00:07 字数 3349 浏览 0 评论 0原文

我是使用 JPA 技术的 SPRING 新手。

我正在尝试调用用 mysql 5 编写的存储过程。当我尝试使用存储过程获取数据时,将其称为“喷出异常”。

org.springframework.dao.InvalidDataAccessApiUsageExceptionorg.hibernate.QueryException:查询必须以SELECTFROM开头code>: call [call st_proc_getusers()]; 嵌套异常是 java.lang.IllegalArgumentException: org.hibernate.QueryException:查询必须以SELECTFROM开头:call [call st_proc_getusers()]< /p>

my persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit  name="SPT3" transaction-type="RESOURCE_LOCAL">
            <mapping-file>META-INF/persistence-query.xml</mapping-file>
            <class>com.spt3.pojo.Users</class>
            <properties>
                    <property name="hibernate.dialect" value=">org.hibernate.dialect.MySQLDialect" />
                    <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/spring_security" />
                    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
                    <property name="hibernate.connection.username" value="user" />
                    <property name="hibernate.connection.password" value="pass" />
                    <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
                    <property name="hibernate.max_fetch_depth" value="3"/>
                    <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
                    <property name="hibernate.query.substitutions" value="true 1, false 0"/>
                    <property name="hibernate.show_sql" value="true"/>
                    <property name="hibernate.hbm2ddl.auto" value="create"/>
            </properties>
    </persistence-unit>
</persistence>

我尝试过的代码,

package com.spt3.dao;

import com.spt3.pojo.Users;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;


@Transactional
public class JpaUsersDao extends JpaDaoSupport {

    public void getResultsByStoredProcedure() {       
        List list=(ArrayList)getJpaTemplate().execute(new JpaCallback() {
            public List doInJpa(EntityManager em) throws PersistenceException {
                javax.persistence.Query query=em.createQuery("call st_proc_getusers()"); // Here the procedure call 
                return query.getResultList(); // returning result list
            }
        });        
    }

}

实际上我不知道如何使用 jpa 模板调用存储过程。

如何从 Spring JPA 调用存储过程?

请给出解决方案以摆脱这个问题。

Am new to SPRING with JPA techniques.

am trying to call the stored procedure which is written in mysql 5. when i am trying to get the data using stored procedure call it spewing exception.

org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.QueryException: query must begin with SELECT or FROM: call [call st_proc_getusers()]; nested exception is java.lang.IllegalArgumentException: org.hibernate.QueryException: query must begin with SELECT or FROM: call [call st_proc_getusers()]

my persistence.xml is

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit  name="SPT3" transaction-type="RESOURCE_LOCAL">
            <mapping-file>META-INF/persistence-query.xml</mapping-file>
            <class>com.spt3.pojo.Users</class>
            <properties>
                    <property name="hibernate.dialect" value=">org.hibernate.dialect.MySQLDialect" />
                    <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/spring_security" />
                    <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
                    <property name="hibernate.connection.username" value="user" />
                    <property name="hibernate.connection.password" value="pass" />
                    <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
                    <property name="hibernate.max_fetch_depth" value="3"/>
                    <property name="hibernate.query.factory_class" value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory"/>
                    <property name="hibernate.query.substitutions" value="true 1, false 0"/>
                    <property name="hibernate.show_sql" value="true"/>
                    <property name="hibernate.hbm2ddl.auto" value="create"/>
            </properties>
    </persistence-unit>
</persistence>

i tried code is

package com.spt3.dao;

import com.spt3.pojo.Users;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.transaction.annotation.Transactional;


@Transactional
public class JpaUsersDao extends JpaDaoSupport {

    public void getResultsByStoredProcedure() {       
        List list=(ArrayList)getJpaTemplate().execute(new JpaCallback() {
            public List doInJpa(EntityManager em) throws PersistenceException {
                javax.persistence.Query query=em.createQuery("call st_proc_getusers()"); // Here the procedure call 
                return query.getResultList(); // returning result list
            }
        });        
    }

}

Actually i don't know how to call the stored procedure using jpa template.

How can i call stored procedure from spring JPA?

please give the solution to get out from this issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不疑不惑不回忆 2024-12-18 12:00:07

使用 EntityManager .createNativeQuery() 相反。我认为不可能通过 JPA 查询调用存储过程。

public List doInJpa(EntityManager em) throws PersistenceException {
  javax.persistence.Query query=em.createNativeQuery("call st_proc_getusers()"); 
  return query.getResultList(); 
}

您还可以使用 @NamedNativeQuery

Use EntityManager.createNativeQuery() instead. I don't think it's possible to call a stored procedure through a JPA query.

public List doInJpa(EntityManager em) throws PersistenceException {
  javax.persistence.Query query=em.createNativeQuery("call st_proc_getusers()"); 
  return query.getResultList(); 
}

You could also use @NamedNativeQuery.

吃素的狼 2024-12-18 12:00:07

调用存储过程意味着执行一些SQL语句。您无法使用 JPQL 查询(执行 em.createQuery(...) 时在代码中得到的内容)来做到这一点。您必须创建一个本机查询,该查询允许您将本机 SQL 发送到数据库、执行它并获取结果:

 String query = "call st_proc_getusers(?)";
 NativeQuery nq = em.createNativeQuery(query);
 //the following line is necessary only if your stored procedure accepts a parameter:
 nq.setParameter(1, "paramValue");
 List<?> results = em.createNativeQuery(query).getResultList();

Calling a stored procedure means executing some SQL statement. You can't do that with with a JPQL query (what you get in your code when you do em.createQuery(...)). You must create a native query that allows you to send native SQL to the database, execute it and get the results:

 String query = "call st_proc_getusers(?)";
 NativeQuery nq = em.createNativeQuery(query);
 //the following line is necessary only if your stored procedure accepts a parameter:
 nq.setParameter(1, "paramValue");
 List<?> results = em.createNativeQuery(query).getResultList();
街道布景 2024-12-18 12:00:07

JPA 2.1 引入了对存储过程的支持。您可能想尝试一下它而不是本机查询。

http://docs .oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createStoredProcedureQuery%28java.lang.String%29

JPA 2.1 introduced support for stored procedures. You might want to give it a try instead of a native queries.

http://docs.oracle.com/javaee/7/api/javax/persistence/EntityManager.html#createStoredProcedureQuery%28java.lang.String%29

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文