I'm invoke a Sql Stored procedure through getSingleResult from a "for", but results gets be the same to each row

huangapple 未分类评论44阅读模式
英文:

I'm invoke a Sql Stored procedure through getSingleResult from a "for", but results gets be the same to each row

问题

我在控制器中有这段代码

```java
@PostMapping(value="/Servicios/fetch-data/info-adicional", produces= {"application/json"})
public dto_encabezado getAddInfoItem(@RequestBody dto_encabezado encabezado) 
{
    encabezado.setValid(true);
    PA_ITEMS_INFO_ENTITY info;
    for(dto_detalle item: encabezado.getDetalle()) {
        info = new PA_ITEMS_INFO_ENTITY();
        
        try {
            info = FetchData.getAddInfoItem(item.getProducto(), 
                                            item.getProveedor() == null ? "" : item.getProveedor(),
                                            encabezado.isServicioArticulo() ? "S" : "I");
            
            item.setDescripcion(info.getDescription());//Nombre
        
        }catch(Exception e) {
            item.setComentario(e.getMessage());
            encabezado.setValid(false);
        }
    }
    return encabezado;
}

我像这样实现了存储过程:

@Override
@Transactional
public PA_ITEMS_INFO_ENTITY getAddInfoItem(String itemCode, String cardCode, String docType) {
    StoredProcedureQuery sp = entityManager.createNamedStoredProcedureQuery("getAddInfoItem");
    sp.setParameter("ItemCode", itemCode);
    sp.setParameter("CardCode", cardCode);
    sp.setParameter("DocType", docType);
    PA_ITEMS_INFO_ENTITY obj = new PA_ITEMS_INFO_ENTITY();
    obj = (PA_ITEMS_INFO_ENTITY)sp.getSingleResult();
    System.out.println(itemCode); //打印给定的参数
    System.out.println(obj.getCode());//Code属性是参数的相同值,但作为存储过程的结果获得
    return obj;
}

存储过程类中的属性:

@Column(name = "ItemCode")
public String code;

没有错误,但在控制台中打印出了这些内容,似乎结果代码在存储过程中重复了,但参数是不同的。为什么会这样?

  • FL1341101-L //参数
  • FL1341101-L //结果(正常)
  • FL1341102-L //参数
  • FL1341101-L //结果(错误,之前的结果)
  • FL1320221-L //参数
  • FL1341101-L //结果(错误,之前的结果)
  • FL1331002-L //参数
  • FL1331002-L //结果(正常)
  • FL1341101-L //参数
  • FL1331002-L //结果(错误,之前的结果)...
  • FL1341102-L
  • FL1331002-L
  • FL1320221-L
  • FL1331002-L

如果我删除 @Transaction 注解,结果会在所有行中重复出现...


<details>
<summary>英文:</summary>

I have this code in controller

    @PostMapping(value=&quot;/Servicios/fetch-data/info-adicional&quot;, produces= {&quot;application/json&quot;})
	public dto_encabezado getAddInfoItem(@RequestBody dto_encabezado encabezado) 
	{
		encabezado.setValid(true);
		PA_ITEMS_INFO_ENTITY info;
		for(dto_detalle item: encabezado.getDetalle()) {
			 info = new PA_ITEMS_INFO_ENTITY();
			
			try {
				info = FetchData.getAddInfoItem(item.getProducto(), 
												item.getProveedor() == null ? &quot;&quot; : item.getProveedor(),
												encabezado.isServicioArticulo() ? &quot;S&quot; : &quot;I&quot;);
				
				item.setDescripcion(info.getDescription());//Nombre
	        
			}catch(Exception e) {
				item.setComentario(e.getMessage());
				encabezado.setValid(false);
			}
		}
		return encabezado;
	}
I implement stored procedure like this:

    @Override
	@Transactional
	public PA_ITEMS_INFO_ENTITY getAddInfoItem(String itemCode, String cardCode, String docType) {
		StoredProcedureQuery sp = entityManager.createNamedStoredProcedureQuery(&quot;getAddInfoItem&quot;);
		sp.setParameter(&quot;ItemCode&quot;, itemCode);
		sp.setParameter(&quot;CardCode&quot;, cardCode);
		sp.setParameter(&quot;DocType&quot;, docType);
		PA_ITEMS_INFO_ENTITY obj = new PA_ITEMS_INFO_ENTITY();
		obj = (PA_ITEMS_INFO_ENTITY)sp.getSingleResult();
		System.out.println(itemCode); //Print given parameter
		System.out.println(obj.getCode());//Code property is the same value of paramenter but was get as result into StoredProcedure 
		return obj;
	}	

property in Stored Procedure class

    @Column(name = &quot;ItemCode&quot;)
	public String code;

There are no erros, but in console print this, seems the result code is repeated in stored procedure but the parameter is different. why?

 - FL1341101-L //parameter
 - FL1341101-L //result (ok)
 - FL1341102-L //parameter
 - FL1341101-L //result (bad, previous result)
 - FL1320221-L //parameter
 - FL1341101-L //result (bad, previous result)
 - FL1331002-L //parameter
 - FL1331002-L //result(ok)
 - FL1341101-L //parameter
 - FL1331002-L //result (bad, preious result)....
 - FL1341102-L
 - FL1331002-L
 - FL1320221-L
 - FL1331002-L

If I delete @Transaction anotation, the result is repeated for all rows..

</details>


# 答案1
**得分**: 0

解决方案:在调用 getSingleResult() 方法之前,如果有任何活动会话,请关闭会话...

    Session session = entityManager.unwrap(Session.class);
    if(session != null) {
	    session.clear();
	}

希望你觉得有用。

<details>
<summary>英文:</summary>

Solution: Close session if any is active, before to invoke getSingleResult() method...

    Session session = entityManager.unwrap(Session.class);
    if(session != null) {
	    session.clear();
	}

I hope you find it useful

</details>



huangapple
  • 本文由 发表于 2020年4月4日 00:31:50
  • 转载请务必保留本文链接:https://java.coder-hub.com/61016397.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定