ジェネリックメソッド
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);
}
}
(Following)Controller Working Fine
@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:オブジェクトは保存されていない一時インスタンスを参照します-フラッシュする前に一時インスタンスを保存します:com.soft.erp.pro.model.ProPurchaseOrderModel;ネストされた例外はorg.hibernate.TransientObjectExceptionです:オブジェクトは保存されていない一時インスタンスを参照します-フラッシュする前に一時インスタンスを保存します
エラーメッセージに次のように記載されています。ProPurchaseOrderModel
には、まだ保存されていない別のエンティティ(おそらくInvItemModel
および/またはInvUOMExtendedModel
)への参照があります。これらのエンティティを最初に保存するか、(おそらくより良い)マッピングを修正して、persistに対してcascadeを宣言することにより、これらの参照オブジェクトが自動的に保存されるようにします。
注釈を使用している場合、これは、たとえば、との多対1の関係で行われます。
@ManyToOne(cascade = CascadeType.PERSIST)
あるいは
@ManyToOne(cascade = CascadeType.ALL)