save the transient instance before flushing:

问题描述

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='text']&quot;).val(&quot;&quot;); </p><pre><code> $(this).find(&quot;input[name='educationBackgroundlist[0].startTime']&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='educationBackgroundlist[0].university']&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='educationBackgroundlist[0].overTime']&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='educationBackgroundlist[0].professional']&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='educationBackgroundlist[0].qualification']&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='deletenum']&quot;).attr(&quot;class&quot;&quot;edcationdeletetable&quot;+ num).attr(&quot;name&quot;&quot;deletenum&quot;+ num).attr(&quot;onclick&quot;&quot;deletethiseducation('&quot;+num+&quot;')&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(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

时间: 2024-09-20 16:26:38

save the transient instance before flushing:的相关文章

Hibernate(6)—— 一对多 和 多对多关联关系映射(xml和注解)总结

俗话说,自己写的代码,6个月后也是别人的代码--复习!复习!复习!涉及的知识点总结如下: One to Many 映射关系 多对一单向外键关联(XML/Annotation) 一对多单向外键关联(XML/Annotation) 懒加载和积极加载 一对多双向外键关联(XML/Annotation) Many to Many 映射关系 多对多单向外键关联(XML/Annotation) 多对多双向外键关联(XML/Annotation) set的inverse元素详解 问题小结 关联关系的优缺点 多

hibernate5(9)注解映射[1]多对一单向关联

在博客网站中,我们可能需要从某一篇文章找到其所关联的作者,这就需要从文章方建立起对用户的关联,即是多对一的映射关系. 现在先看一个配置实例:我们的文章实体类 package com.zeng.model; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue;

Hibernate开发中常见错误总结

org.hibernate.MappingException: Unknown entity: cn.itcast.hibernate717.helloworld.Person hibernate框架根本不知道Person这个持久化类的存在 Caused by: java.sql.SQLException: Field 'id' doesn't have a default value 如果主键的生成策略采取的是identity,而数据库中表的主键不是自动生成机制,则报这样的错误 org.hib

hibernate系列(二)一对多的关联关系

上一篇文章介绍了基本知识后,本篇该介绍下现实中的一对多的关联关系.如Customer和Order,一个Customer可以拥有多个Order,每个Order只属于一个Customer.这样就存在几种表示形式,可以分为单向关联和双向关联.  形式1:Order拥有一个Customer引用,这种属于单向关联  形式2:Customer拥有Order的集合,这种也属于单向关联  形式3:Order拥有一个Customer引用,同时Customer拥有Order集合,这种属于双向关联  先来说说形式1:

hibernate3 的多对一的外键更新问题!

问题描述 public class SysModel extends BaseEntity<SysModel> {@ManyToOne(cascade = { CascadeType.MERGE })@JoinColumn(name = "PARENT_ID")private SysModel sysModel; .......} 映射文件大概如此, 有一个自关联字段.在操作的时候,遇到一个问题,当此字段为 null 的时候,我可以保存,但是不能更新,报错:Hibernat

Hibernate单向1-1含连接表映射实例详解

Hibernate单向1-1映射范例 其实 Many-to-one 和 one-to-one 非常相似,只需要在 <many-to-one> 的尖括号中添加 unique="true"即可 1.hibernate.cfg.xml  代码如下 复制代码 <!DOCTYPE hibernate-configuration PUBLIC         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

Java中Hibernate单向(1-N)映射实例详解

1.hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <s

[NHibernate]存储过程的使用(二)

目录 写在前面 文档与系列文章 创建对象 更新对象 总结 写在前面 上篇文章介绍了如何使用MyGeneration代码生成器生成存储过程,以及nhibernate中通过存储过程删除数据的内容,这篇文章将介绍如何创建对象,更新对象. 文档与系列文章 [Nhibernate]体系结构 [NHibernate]ISessionFactory配置 [NHibernate]持久化类(Persistent Classes) [NHibernate]O/R Mapping基础 [NHibernate]集合类(

Hibernate学习之------&amp;gt;Hibernate的保存的区别

hibernate的保存 hibernate对于对象的保存提供了太多的方法,他们之间有很多不同,这里细说一下,以便区别: 一.预备知识: 在所有之前,说明一下,对于hibernate,它的对象有三种状态,transient.persistent.detached 下边是常见的翻译办法: transient:瞬态或者自由态 persistent:持久化状态 detached:脱管状态或者游离态 脱管状态的实例可以通过调用save().persist()或者saveOrUpdate()方法进行持久化