温柔写的伤 2016-05-26 10:14 采纳率: 0%
浏览 5053

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> $(&quot;#btnAddeducation&quot;).click(<br> function() {<br> var num = $(&quot;#educationhidNum&quot;).val(); <br> num = parseInt(num);<br> num++; <br> $(&quot;#educationhidNum&quot;).val(num); <br> $(&quot;#edcationaddtable&quot;).clone(true).attr(&quot;id&quot;, &quot;edcationaddtable&quot; + num).appendTo(&quot;#educationbackgrroundadd&quot;); //clone <br> $(&quot;#edcationaddtable&quot; + num).each(function() {<br> $(this).find(&quot;input[type=&#39;text&#39;]&quot;).val(&quot;&quot;); </p> <pre><code> $(this).find(&quot;input[name=&#39;educationBackgroundlist[0].startTime&#39;]&quot;).attr(&quot;id&quot;,&quot;educationStartTime&quot;+ num).attr(&quot;name&quot;,&quot;educationBackgroundlist[&quot;+ num+&quot;].startTime&quot;); $(this).find(&quot;input[name=&#39;educationBackgroundlist[0].university&#39;]&quot;).attr(&quot;id&quot;,&quot;university&quot;+ num).attr(&quot;name&quot;,&quot;educationBackgroundlist[&quot;+ num+&quot;].university&quot;); $(this).find(&quot;input[name=&#39;educationBackgroundlist[0].overTime&#39;]&quot;).attr(&quot;id&quot;,&quot;educationOverTime&quot;+ num).attr(&quot;name&quot;,&quot;educationBackgroundlist[&quot;+ num+&quot;].overTime&quot;); $(this).find(&quot;input[name=&#39;educationBackgroundlist[0].professional&#39;]&quot;).attr(&quot;id&quot;,&quot;professional&quot;+ num).attr(&quot;name&quot;,&quot;educationBackgroundlist[&quot;+ num+&quot;].professional&quot;); $(this).find(&quot;input[name=&#39;educationBackgroundlist[0].qualification&#39;]&quot;).attr(&quot;id&quot;,&quot;qualification&quot;+ num).attr(&quot;name&quot;,&quot;educationBackgroundlist[&quot;+ num+&quot;].qualification&quot;); $(this).find(&quot;input[name=&#39;deletenum&#39;]&quot;).attr(&quot;class&quot;,&quot;edcationdeletetable&quot;+ num).attr(&quot;name&quot;,&quot;deletenum&quot;+ num).attr(&quot;onclick&quot;,&quot;deletethiseducation(&#39;&quot;+num+&quot;&#39;)&quot;); }); }); }); &lt;/script&gt; &lt;script type=&quot;text/javascript&quot;&gt; function deletethiseducation(a){ a = parseInt(a); if(a==0){ alert(&quot;该项不能删除!&quot;); return; } document.getElementById(&quot;edcationaddtable&quot;+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>开始&nbsp;时间:<input type="text" name="educationBackgroundlist[0].startTime" id="educationStartTime" /></td>
                <td ></td>
                <td >学&nbsp;&nbsp;&nbsp;校:<input type="text" name="educationBackgroundlist[0].university" id="university" /></td>
                <td ></td>
            </tr>
            <tr>
                <td>结束&nbsp;时间:<input type="text" name="educationBackgroundlist[0].overTime" id="educationOverTime" /></td>
                <td></td>
                <td>专&nbsp;&nbsp;&nbsp;业:<input type="text" name="educationBackgroundlist[0].professional" id="professional" /></td>
                <td></td>
            </tr>
            <tr>
                <td>学&nbsp;&nbsp;&nbsp;位:<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(resume,pageres,workSeniority1,workSeniority2,pageNo,pageSize);
    return SUCCESS;
}

Resume简历为主表,EducationBackground为简历从表教育背景表,两个表都有主外键关系,
@OneToMany(fetch = FetchType.EAGER,targetEntity=EducationBackground.class,cascade={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.class,fetch = 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动态添加时 就报上面的错误,是怎么回事啊 ?? 求大神帮帮忙,万分感谢!

  • 写回答

2条回答 默认 最新

  • Robot-C 2016-05-26 10:22
    关注

    
    三张数据表的关系如下图 

    在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
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?

悬赏问题

  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3