如何使用JPA(Entity Manager)调用(MySQL)数据库存储过程:当过程已经存储在数据库中时
用例:
我已经在数据库中存储的过程:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论