问题描述
- save the transient instance before flushing:
- SSH开发save()报错:
严重: Exception occurred during processing request: object references an unsaved transient instance - save the transient instance before flushing: com.resume.demain.Resume
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.resume.demain.Resume
at org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:279)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:455)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:281)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:291)
at org.hibernate.type.TypeHelper.findDirty(TypeHelper.java:296)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:4079)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:532)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:215)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:142)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:216)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:85)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:38)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1224)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:464)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:2905)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2281)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:485)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:230)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:65)
at com.resume.dao.impl.ResumeDaoImpl.save(ResumeDaoImpl.java:28)
at com.resume.service.impl.ResumeServiceImpl.save(ResumeServiceImpl.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
前台jsp页面,动态添加:<br> $(function() {<br> $("#btnAddeducation").click(<br> function() {<br> var num = $("#educationhidNum").val(); <br> num = parseInt(num);<br> num++; <br> $("#educationhidNum").val(num); <br> $("#edcationaddtable").clone(true).attr("id" "edcationaddtable" + num).appendTo("#educationbackgrroundadd"); //clone <br> $("#edcationaddtable" + num).each(function() {<br> $(this).find("input[type='text']").val(""); </p><pre><code> $(this).find("input[name='educationBackgroundlist[0].startTime']").attr("id""educationStartTime"+ num).attr("name""educationBackgroundlist["+ num+"].startTime"); $(this).find("input[name='educationBackgroundlist[0].university']").attr("id""university"+ num).attr("name""educationBackgroundlist["+ num+"].university"); $(this).find("input[name='educationBackgroundlist[0].overTime']").attr("id""educationOverTime"+ num).attr("name""educationBackgroundlist["+ num+"].overTime"); $(this).find("input[name='educationBackgroundlist[0].professional']").attr("id""professional"+ num).attr("name""educationBackgroundlist["+ num+"].professional"); $(this).find("input[name='educationBackgroundlist[0].qualification']").attr("id""qualification"+ num).attr("name""educationBackgroundlist["+ num+"].qualification"); $(this).find("input[name='deletenum']").attr("class""edcationdeletetable"+ num).attr("name""deletenum"+ num).attr("onclick""deletethiseducation('"+num+"')"); }); }); }); </script> <script type="text/javascript"> function deletethiseducation(a){ a = parseInt(a); if(a==0){ alert("该项不能删除!"); return; } document.getElementById("edcationaddtable"+a).remove(); }</code></pre><p>
<div id=""educationbackgrroundadd""> <table id = ""edcationaddtable""> <tr> <td colspan=""4"" style=""font-weight:bold; font-size:20px;"">教育背景:</td> </tr> <tr> <td>开始 时间:<input type=""text"" name=""educationBackgroundlist[0].startTime"" id=""educationStartTime"" /></td> <td ></td> <td >学 校:<input type=""text"" name=""educationBackgroundlist[0].university"" id=""university"" /></td> <td ></td> </tr> <tr> <td>结束 时间:<input type=""text"" name=""educationBackgroundlist[0].overTime"" id=""educationOverTime"" /></td> <td></td> <td>专 业:<input type=""text"" name=""educationBackgroundlist[0].professional"" id=""professional"" /></td> <td></td> </tr> <tr> <td>学 位:<input type=""text"" name=""educationBackgroundlist[0].qualification"" id=""qualification"" /></td> <td></td> <td> </td> <td><input type=""button"" name=""deletenum"" class=""edcationdeletetable"" value=""点击删除"" onclick=""deletethiseducation('0')"" /></td> </tr> </table> <table> <tr> <td></td> <td></td> <td></td> <td> <input id=""educationhidNum"" type=""hidden"" name=""hidNum"" value=""0"" /> <input type=""button"" style=""margin-left:100%;"" value=""继续添加"" id=""btnAddeducation"" onmouseover=""this.style.backgroundColor='red';"" onmouseout=""this.style.backgroundColor='#d4e3e5';"" /> </td> </tr> </table>
后台Action:
private List educationBackgroundlist = new ArrayList();
public Resume backresume() throws Exception {
List educationBackgroundlist1 = this.educationBackgroundlist();Resume resume = new Resume(); resume.setResumeID(resumeID); resume.setApartment(apartment); resume.setAppliedPosition(appliedPosition); resume.setDestination(destination); resume.setEducationBackground(educationBackgroundlist1); resume.setEmail(email); resume.setEvaluation(evaluation); resume.setExpectation(expectation); resume.setFamilySituation(familySituation); resume.setLanguageProficiency(languageProficiency); resume.setMotivation(motivation); resume.setNationality(nationality); resume.setOperatingTime(operatingTime); resume.setOperator(operator); resume.setOther(other); resume.setPersonalAge(personalAge); resume.setPersonalName(personalName); resume.setPersonalSex(personalSex); resume.setPhoneNum(phoneNum); resume.setResumeState(resumeState); resume.setSourceCompany(sourceCompany); resume.setWorkSeniority(workSeniority); return resume; }public String addresume() throws Exception { Resume resume = this.backresume(); resumeService.save(resume); Pageres pageres = this.pageser(); list = resumeService.queryresumelist(resumepageresworkSeniority1workSeniority2pageNopageSize); return SUCCESS;}
Resume简历为主表,EducationBackground为简历从表教育背景表,两个表都有主外键关系,
@OneToMany(fetch = FetchType.EAGERtargetEntity=EducationBackground.classcascade={CascadeType.ALL}orphanRemoval=true)
// @Cascade(value={org.hibernate.annotations.CascadeType.ALL})
// @NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name=""resume_id"")
public List getEducationBackground() {
return educationBackground;
}
public void setEducationBackground(List educationBackground) {
this.educationBackground = educationBackground;
}@ManyToOne(targetEntity=Resume.classfetch = FetchType.EAGER)@JoinColumn(name=""resume_id""referencedColumnName=""resumeID"" insertable=true updatable=true)public Resume getResume() { return resume;}public void setResume(Resume resume) { this.resume = resume;}
数据访问层的sava()方法;
@Override
public void save(Resume resume) {
Session session = sessionFactory.openSession();
//Session session = sessionFactory.getCurrentSession();
Transaction tx = session.beginTransaction();
//session.persist(resume);
//session.save(resume);
session.persist(resume);
tx.commit();
session.close();
}当我在action中手动添加教育背景的数据时,数据为多条,可以插入数据库,但是我换成jsp动态添加时 就报上面的错误,是怎么回事啊 ?? 求大神帮帮忙,万分感谢!
解决方案
?
三张数据表的关系如下图?
在java中如下写法报错(错误信息:save the transient instance before flushing):
?@OneToMany(fetch=FetchType.LAZY)? //这种情形会报错save the transient instance before flushing: elitel.szy.groundwater......
答案就在这里:save the transient instance before flushing