通用方法
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;
}
}
例外是
例外组织。springframework。刀。InvalidDataAccessApiUsageException:对象引用未保存的临时实例-在刷新之前保存临时实例:com。软的企业资源计划赞成的意见。模型ProPurchaseOrderModel;嵌套的异常是org。冬眠TransientObjectException:对象引用未保存的临时实例-在刷新之前保存临时实例
如错误消息所述:您的ProPurchaseOrderModel
引用了另一个实体(很可能是InvItemModel
和/或InvUOMExtendedModel
),该实体本身尚未保存。要么你先保存这些实体,要么(可能更好)修复你的映射,这样这些引用的对象将通过声明一个持续级联来自动保存。
如果您使用的是注释,则可以在多对一关系上使用注释
@ManyToOne(cascade = CascadeType.PERSIST)
或者甚至
@ManyToOne(cascade = CascadeType.ALL)
我使用hibernate-4.1.2和spring-framework-4.1.6中的所有jar文件来尝试hibernateTemplate问题中指定的项目。我正面临一个组织。springframework。刀。InvalidDataAccessApiUsageException 上面提到的错误堆栈建议将会话的刷新模式设置为COMMIT/AUTO或从事务定义中删除“readOnly”标记。有没有办
我添加了以下代码以将记录插入/更新到表中 如果数据存在,它将更新表,否则将插入一个新记录,在我的情况下,数据已经存在,所以我首先更新记录,如果循环工作,但我得到 org.springframework.dao.DataIntegrityViolationException: key'umul_facebook_id_UNIQUE'的重复条目'482186425258498';SQL[n/a];约束
我正在实现SimpleAsyncTaskExec导师。当我插入或更新到数据库时,它会抛出以下异常org.springframework.dao.InvalidDataAccessApiUsageExc0019:执行更新/删除查询;嵌套异常是javax.persistence.Transaction要求异常:执行更新/删除查询错误。 Spring的背景。xml 错误详细信息:
我试图用Spring DI创建一个简单的HelloWorld应用程序。我创建了一个Java项目并导入了一些教程类,其中一个很简单: 然而,我得到了以下错误:。我正在使用Eclipse Spring Tool Suite 3.7.2,当我单击“修复项目设置”时,我认为通过使用这个Eclipse版本,它会为我添加这些依赖项。我在这里做错了什么?即使在STS中,我也需要手动添加这些依赖项吗?如果是,正确
我在Spring的申请中做了联系表格。我有一个问题: 字段名称上的对象接触中的字段错误:拒绝的值[null];代码[NotBlank.contact.name, NotBlank.name, NotBlank.java.lang.String, NotBlank];参数[org.springframework.context.support.DefaultMessageSourceResolabl
我收到了第三方的回复,如下所示 我们有课。 但是当我们转换时,我们会低于误差 错误com.openbet.commons.sdk.common.util.请求SenderImpl-在HTTP交换过程中意外的异常:org.springframework.http.converter.HttpMessageNotReadableExctive:JSON解析错误:无法反序列化的实例START_ARRAY