gpt4 book ai didi

java - org.springframework.dao.InvalidDataAccessApiUsageException

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:25:55 25 4
gpt4 key购买 nike

我正在使用 Spring MVC + Hibernate

通用方法

public <T, E> void saveOrUpdate(T entity, List<E> list) throws DataAccessException {

Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(entity);
for(E getlist : list){
session.saveOrUpdate(getlist);
}
}

(以下) Controller 工作正常

@Controller
public class FinGeneralJournalController {

protected static Logger log = Logger
.getLogger(FinGeneralJournalController.class);

@Resource(name = "PersistenceTemplate")
private PersistenceTemplate pt;

@RequestMapping(value = "/AddFinGeneralJournal", method = RequestMethod.POST)
public @ResponseBody
JsonResponseStatus addGeneralJournalEntry(
@RequestParam(value="description", required=true) String description,
@RequestParam(value="trxDate", required=true) Date trxDate,
@RequestParam(value="sourceDocumentId", required=true) long sourceDocumentId,
@RequestParam(value="currencyId", required=true) long currencyId,
@RequestParam(value="batchId", required=true) long batchId,
@RequestParam(value="tableList", required=true) String tableList, HttpSession session ) {

Date sysdate = null;
JsonResponseStatus response = null;
List<FinGeneralJournalEntryModel> list=null;
FinGeneralJournalEntryModel generalJournalEntryModel= null;
try {
long userId=Long.parseLong(session.getAttribute("userId").toString());
String userIp=session.getAttribute("userIp").toString();

sysdate = new Date();
response = new JsonResponseStatus();

FinGeneralJournalModel generalJournalModel = new FinGeneralJournalModel(description,
trxDate, userId, userIp, sysdate, 0);

generalJournalModel.setFko_gj_batch(new FinBatchModel(batchId));
generalJournalModel.setFko_gj_srcdoc(new FinSourceDocumentModel(sourceDocumentId));
generalJournalModel.setFko_gj_curr(new GenCurrencyModel(currencyId));

list= new ArrayList<FinGeneralJournalEntryModel>();

String []getDistributionlist=tableList.split(",");

for (int i=0; i<getDistributionlist.length; i+=6){

generalJournalEntryModel= new FinGeneralJournalEntryModel(Long.parseLong(getDistributionlist[i+3].toString()),
Long.parseLong(getDistributionlist[i+4].toString()),
currencyId, 86.5, userId, userIp, sysdate, 0);

generalJournalEntryModel.setFko_gje_gj(generalJournalModel);
generalJournalEntryModel.setFko_gje_coam(new FinCOAMaintenanceModel(Long.parseLong(getDistributionlist[i].toString())));

list.add(generalJournalEntryModel);
}

pt.saveOrUpdate(generalJournalModel,list);

response.setStatus("Success");
} catch (Exception ex) {
log.error("Exception.." + ex);
response.setStatus("Fail");
}
return response;
}
}

另一个与上面相同但给出异常的后续 Controller

@Controller
public class ProPurchaseOrderController {
protected static Logger log = Logger
.getLogger(ProPurchaseOrderController.class);

@Resource(name = "PersistenceTemplate")
private PersistenceTemplate pt;

@RequestMapping(value = "/AddProPurchaseOrder", method = RequestMethod.POST)
public @ResponseBody
JsonResponseStatus AddProPurchaseOrder(
@RequestParam(value="poDesc", required=true) String poDesc,
@RequestParam(value="poTrxDate", required=true) Date poTrxDate,
@RequestParam(value="supplierId", required=true) long supplierId,
@RequestParam(value="currencyId", required=true) long currencyId,
@RequestParam(value="batchId", required=true) long batchId,
@RequestParam(value="poSubTotal", required=true) double poSubTotal,
@RequestParam(value="poDiscount", required=true) double poDiscount,
@RequestParam(value="poFreight", required=true) double poFreight,
@RequestParam(value="poTax", required=true) double poTax,
@RequestParam(value="poMisc", required=true) double poMisc,
@RequestParam(value="poGTotal", required=true) double poGTotal,
@RequestParam(value="tableList", required=true) String tableList, HttpSession session ) {

Date sysdate = null;
JsonResponseStatus response = null;
ProPurchaseOrderModel purchaseOrder=null;
ProPurchaseOrderEntryModel purchaseOrderEntry=null;
List<ProPurchaseOrderEntryModel> list=null;

try {
sysdate = new Date();
response = new JsonResponseStatus();

long userId=Long.parseLong(session.getAttribute("userId").toString());
String userIp=session.getAttribute("userIp").toString();

purchaseOrder= new ProPurchaseOrderModel(poDesc, poTrxDate, poSubTotal, poFreight, poTax, poMisc, poDiscount,
0, 0, userId, userIp, sysdate, 0);

purchaseOrder.setFko_po_batch(new ProBatchModel(batchId));
purchaseOrder.setFko_po_currency(new GenCurrencyModel(currencyId));
purchaseOrder.setFko_po_supp(new ProSupplierModel(supplierId));

list= new ArrayList<ProPurchaseOrderEntryModel>();

String []getItemlist=tableList.split(",");

for (int i=0; i<getItemlist.length; i+=11){

double actualQuantity=Double.parseDouble(getItemlist[i+5].toString());
double fraction=Double.parseDouble(getItemlist[i+6].toString());
double baseQuantity=Double.parseDouble(getItemlist[i+7].toString());
double cost=Double.parseDouble(getItemlist[i+8].toString());
double total=Double.parseDouble(getItemlist[i+9].toString());

purchaseOrderEntry= new ProPurchaseOrderEntryModel(actualQuantity, fraction, baseQuantity, cost, total,
0, 0, userId, userIp, sysdate, 0);

purchaseOrderEntry.setFko_poe_uome(new InvUOMExtendedModel(Long.parseLong(getItemlist[i+4].toString())));
purchaseOrderEntry.setFko_poe_item(new InvItemModel(Long.parseLong(getItemlist[i].toString())));
purchaseOrderEntry.setFko_poe_po(purchaseOrder);

list.add(purchaseOrderEntry);
}

pt.saveOrUpdate(purchaseOrderEntry,list);
response.setStatus("Success");
} catch (Exception ex) {
log.error("Exception.." + ex);
response.setStatus("Fail");
}
return response;
}
}

异常(exception)是

Exception..org.springframework.dao.InvalidDataAccessApiUsageException:对象引用一个未保存的 transient 实例 - 在刷新之前保存 transient 实例:com.soft.erp.pro.model.ProPurchaseOrderModel;嵌套异常是 org.hibernate.TransientObjectException:对象引用未保存的 transient 实例 - 在刷新之前保存 transient 实例

最佳答案

如错误消息所述:您的 ProPurchaseOrderModel 引用了另一个实体(很可能是 InvItemModel 和/或 InvUOMExtendedModel),它本身是还没有保存。您要么先保存这些实体,要么(可能更好)修复您的映射,以便通过为 persist 声明一个级联 来自动保存这些引用的对象。

如果您正在使用注释,例如在与的多对一关系上完成此操作

    @ManyToOne(cascade = CascadeType.PERSIST)

甚至

    @ManyToOne(cascade = CascadeType.ALL)

关于java - org.springframework.dao.InvalidDataAccessApiUsageException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17901932/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com