简体   繁体   English

JPA中的存储过程

[英]Stored procedure in JPA

I have one procedure in data base it returns list of objects when i try to iterate that result i am getting only one row multiple times.it is not ponting to next record in loop, 我在数据库中有一个过程,当我尝试迭代该结果时,它会返回对象列表,而我只会多次获得一行。它不响应循环中的下一条记录,

here is my code, 这是我的代码,

Query nativeQuery = entityManager.createNativeQuery("call getdata(?, ?, ?, ?)", Detail.class); 
List<Detail>  paymentAnalysisDetails=nativeQuery.getResultList();
for( Detail Details : paymentAnalysisDetails) {
    System.out.println(Details.getStart_date());
}

i getting output:: 我得到输出::

 start_date   end_date   total_no_of_txns 
 2015-01-01  2015-01-30                10 
 2015-01-01  2015-01-30                10 

but while executing same procedure in mysql output as 但是在mysql输出中执行相同的过程时

start_date  end_date    total_no_of_txns
2015-01-01  2015-01-30  10
2015-02-01  2015-02-30  200

can anyone suggest me 谁能建议我

You don't need/have to call stored procedures from the 'createNativeQuery' method because JPA 2.1 supports them now. 您不需要/不必通过'createNativeQuery'方法调用存储过程,因为JPA 2.1现在支持它们。 Check out the following code: 查看以下代码:

Stored prodcedure (MySQL): 存储过程(MySQL):

DELIMITER $$
  CREATE PROCEDURE sp_getUsers(inputAge INT)
  BEGIN
    SELECT u.* FROM usertable u WHERE u.age >= inputAge;
  END;
$$

JPA calling: JPA调用:

int age = 10;

StoredProcedureQuery query = em.createStoredProcedureQuery("sp_getUsers", Usertable.class)
.registerStoredProcedureParameter("inputAge", Integer.class, ParameterMode.IN)
.setParameter("inputAge", age);

List<Usertable> userList = query.getResultList();

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

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