【问题标题】:Using Store Procedure with multiple result JPA将存储过程与多个结果 JPA 一起使用
【发布时间】:2015-07-22 08:34:12
【问题描述】:

我在 SQL Server 中有两个表:

表 A

ID Num
11 tj55
4  tj40

表 B

ID NUM A_ID
3 se400 4
5 se500 4

我有一个存储过程,它将为 A 中的每一行返回 B 中的所有相应行,我有这个结果

NumA NumB
tj40 se400
tj40 se500

代码:

ALTER PROCEDURE [dbo].[Proc] 
    @param nvarchar(MAX),
AS
BEGIN
    SELECT 
        aa.Num,
        bb.Num
    FROM 
        [dbo].[A]  as aa
    INNER JOIN 
        [dbo].[B] AS bb ON bb.A_ID = aa.ID 
    WHERE 
        aa.Num = @param
    ORDER BY 
        aa.Num
 END

我想在我的 Java 应用程序中使用 JPA 调用我的 StoredProcedure 来生成两行(我的目标是让 List 包含所有 NumB(在本例中为“se400”和“se500”)

StoredProcedureQuery query2 = em.createStoredProcedureQuery("Proc");
query2.registerStoredProcedureParameter("param", String.class,ParameterMode.IN);;
query2.setParameter("param", "tj40");

query2.execute();   

List<Object> res = query2.getResultList();
for (int i=0; i<res.size(); i++){
System.out.println(res.get(i).toString());
}

它找到了两行但它返回了

[Ljava.lang.Object;@94814
[Ljava.lang.Object;@00856

你能帮帮我吗? 非常感谢

【问题讨论】:

  • 你的意思是,将结果行转换为 Object[] ?!
  • 我最好的方案是将结果行转换为 List (或 Object 但我需要将其转换为 String)
  • 如果一行是一个 Object[] 为什么你要把它转换成(字符串)?!
  • 我可能不应该使用“cast to String”这个词,我的意思是我需要像 Object[1] 一样访问对象的内容...(在本例中为“se400”和“se500”值)

标签: java sql sql-server jpa


【解决方案1】:

在我的应用程序中,我使用以下方式

List i1 = query2.getResultList();
while(i1.hasNext()){

    Object[] object =(Object[]) i1.next();
//your rest code
//object[0] will contain numA and object[1] will contain numB
}

【讨论】:

  • 感谢您的回答,这绝对是我所需要的(object[0] 将包含 numA,object[1] 将包含 numB)。但实际上它不起作用:对于 List 类型,方法 hasNext() 是未定义的,当我转换为 Object 时,它是相同的
  • or (int i=0; i
  • @Learthgz 我更喜欢迭代器而不是 for 循环
猜你喜欢
  • 2014-08-29
  • 2018-10-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多