简体   繁体   English

JPA调用存储过程从2 db中选择数据

[英]JPA called stored procedure selecting data from 2 db

I am using mySql and jee application using jpa and hibernate 我正在使用jpa和hibernate的mySql和jee应用程序

I have a stored procedure that deletes data from table in db1 then selects data from table from db2 and inserts them in table from db1 我有一个存储过程,该过程从db1中的表中删除数据,然后从db2中的表中选择数据,并将其插入db1中的表中

CREATE PROCEDURE `update_data`(myId int(11))
BEGIN
    DELETE FROM db1.tbl1
    WHERE db1.tbl1.id = myId;

    INSERT INTO
    db1.tbl1
    SELECT * FROM
    db2.tbl1
    WHERE db2.tbl1.id = myId;
END

When calling in sql it succeeds When calling from jpa it returns exception 在sql中调用时成功从jpa调用时返回异常

ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-17) ResultSet is from UPDATE. No Data.
ERROR [stderr] (default task-17) org.hibernate.exception.GenericJDBCException: could not execute query

JAVA code: JAVA代码:

@PersistenceContext(name = "db2", unitName = "db2")
EntityManager em;

org.hibernate.Session session = (org.hibernate.Session) em.getDelegate();

        Transaction tx = null;
        tx = session.beginTransaction();
        try {
            Query query = session
                    .createSQLQuery("CALL update_teachers(:sesId)")
                    .addEntity(Teacher.class).setParameter("sesId", sesId);
            query.list();
            session.flush();
            session.clear();

            tx.commit();
        } catch (HibernateException e) {
            if (tx != null)
                tx.rollback();
            e.printStackTrace();
        } finally {
        session.close();
        }

The problem seems that there is selection from 2 different databases where em can't handle this operation How can i perform this task 问题似乎是从2个不同的数据库中选择了em无法处理此操作的方法,我该如何执行此任务

Note : I also tried native named query and i tried @NamedStoredProcedureQuery All gave the same exception 注意:我也尝试了本机命名查询 ,也尝试了@NamedStoredProcedureQuery All都给出了相同的异常

You seem to execute the stored procedure and expect a result set in return: 您似乎执行了存储过程,并期望返回一个结果集:

query.list();

This doesn't seem correct to me, you won't be getting any records. 这对我来说似乎不正确,您不会获得任何记录。 Try calling it as if you want to run update or delete: 尝试调用它,就像您要运行更新或删除一样:

query.executeUpdate();

As Sva.Mu already wrote, the stored procedure call throws an exception because the entity manager expect a result from the call, if you use 正如Sva.Mu已经写的那样,存储过程调用会引发异常,因为如果您使用,实体管理器会期望调用结果

query.list();

You can see it in this part of the error message 您可以在错误消息的此部分中看到它

ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-17) ResultSet is from UPDATE. No Data.

The call should work, if you instead use 如果您改为使用该呼叫,则该呼叫应该正常工作

query.executeUpdate()

In that case, the entity manager will not expect any result from the stored procedure call. 在这种情况下,实体管理器将不会从存储过程调用中获得任何结果。

This is the same, if you use a @NamedStoredProcedureQuery or the more dynamic StoredProcedureQuery. 如果使用@NamedStoredProcedureQuery或更动态的StoredProcedureQuery,则相同。 You can read more about it on my here: @NamedStoredProcedureQuery and StoredProcedureQuery 您可以在这里阅读有关此内容的更多信息: @NamedStoredProcedureQueryStoredProcedureQuery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM