通用方法
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
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
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..org.springframework.dao.InvalidDataAccessApiUsageException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.soft.erp.pro.model.ProPurchaseOrderModel;嵌套异常是org.hibernate.TransientObjectException:object引用未保存的瞬态实例 - 在刷新之前保存瞬态实例
正如错误消息所述:您的ProPurchaseOrderModel
引用了另一个实体(很可能是InvItemModel
和/或InvUOMExtendedModel
),它本身尚未保存。您可以先保存这些实体,或者(可能更好)修复映射,以便通过声明持久化的级联来自动保存这些引用的对象。
如果您正在使用注释,这可以通过例如与多对一关系完成
@ManyToOne(cascade = CascadeType.PERSIST)
甚至
@ManyToOne(cascade = CascadeType.ALL)