英文:
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="/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;
}
I implement stored procedure like this:
@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); //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 = "ItemCode")
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>
专注分享java语言的经验与见解,让所有开发者获益!
评论