如何使用JPA(Entity Manager)调用(MySQL)数据库存储过程:当过程已经存储在数据库中时

发布于 2025-02-09 06:40:31 字数 1213 浏览 2 评论 0原文

用例:

我已经在数据库中存储的过程:

CREATE DEFINER=`DEVDBA`@`%` PROCEDURE `TestReporting`()
begin 

truncate table `report_controller_total_request`;

insert into `report_controller_total_request` 
select count(*) as total_no_of_requests from log_request;

end

***现在,我想使用EntityManager从代码库中调用此存储过程,

    public void UpdateServer() {
        
        System.out.println("Update Server Start");
        
        EntityManager entityManager = null;
        entityManager = EntityManagerFactoryBuilder.createEntityManager();
        entityManager.getTransaction().begin();
        
        StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("TestReporting");
        
        boolean isExceuted = storedProcedure.execute();
        
        System.out.println("Update Server Done: "+isExceuted);
        entityManager.getTransaction().commit();
        entityManager.close();
    }

我尝试了代码的不同版本,这是我在发布之前尝试的最后一次尝试。

我无法调用该过程,每次

完成服务器启动

服务器时,它都会给我一些错误:false

摘要:

我已经存储了数据库中的过程,我希望使用EntityManagger使用EntityManagger来调用该过程,该过程反过来更新数据库表。

注意:我正在使用JNDI进行数据库连接。

Use Case:

I have the procedure stored in the database:

CREATE DEFINER=`DEVDBA`@`%` PROCEDURE `TestReporting`()
begin 

truncate table `report_controller_total_request`;

insert into `report_controller_total_request` 
select count(*) as total_no_of_requests from log_request;

end

*** Now I want to call this stored procedure from the code base using EntityManager

    public void UpdateServer() {
        
        System.out.println("Update Server Start");
        
        EntityManager entityManager = null;
        entityManager = EntityManagerFactoryBuilder.createEntityManager();
        entityManager.getTransaction().begin();
        
        StoredProcedureQuery storedProcedure = entityManager.createStoredProcedureQuery("TestReporting");
        
        boolean isExceuted = storedProcedure.execute();
        
        System.out.println("Update Server Done: "+isExceuted);
        entityManager.getTransaction().commit();
        entityManager.close();
    }

I have tried different versions of the code, this is the last I tried before posting.

I'm not able to call the procedure, it gives me some error every time

Update Server Start

Update Server Done: false

Summary:

I already have the procedure stored in the database, I want the java code using EntityManagger to call the procedure, which in turn updates the database table.

Note: I'm using JNDI for the database connection.

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文