Question: Object references an unsaved transient instance-save the transient instance before flushing_hibernate

Source: Internet
Author: User

1, error log:
in the preservation of XX information, if the selection of equipment information, the background will not be an error, if the device is not selected, will be an error, the error message is as follows:

[2017-08-10 08:41:02] Org.springframework.web.servlet.HandlerExecutionChain.triggerAfterCompletion (

handlerexecutionchain.java:173) <ERROR> Handlerinterceptor.aftercompletion threw exception Org.hibernate.TransientObjectException:object references an unsaved transient instance-save the transient instance befo
    Re Flushing:com.hcepms.model.device.Device Org.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved (foreignkeys.java:279) at Org.hibernate.type.EntityType.getIdentifier (entitytype.java:462) at Org.hibernate.type.ManyToOneType.isDirty ( manytoonetype.java:281) at the 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:4144) 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:1282) at COM.HCEPM S.base.dao.basedaoimpl.save (basedaoimpl.java:38) at Com.hcepms.base.service.BaseServiceImpl.save ( baseserviceimpl.java:38) at Com.hcepms.base.interceptor.SecurityAndLogInterceptor.afterCompletion ( SECURITYANDLOGINTERCEPTOR.JAVA:150) at Org.springframework.web.servlet.HandlerExecutionChain.triggerAfterCompletion (Handlerexecutionchain.java:170) at Org.springframework.web.servlet.DispatcherServlet.triggerAfterCompletion ( dispatcherservlet.java:1297) at Org.springframework.web.servlet.DispatcherServlet.doDispatch ( dispatcherservlet.java:976) at Org.springframework.web.servlet.DispatcherServlet.doService ( dispatcherservlet.java:895) at Org.springframework.web.servlet.FrameworkServlet.processRequest (
    frameworkservlet.java:967) at Org.springframework.web.servlet.FrameworkServlet.doPost (frameworkservlet.java:869) At Javax.servlet.http.HttpServlet.service (httpservlet.java:648) at Org.springframework.web.servlet.FrameworkServlet.service (frameworkservlet.java:843) at Javax.servlet.http.HttpServlet.service (httpservlet.java:729) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter (applicationfilterchain.java:292) at Org.apache.catalina.core.ApplicationFilterChain.doFilter (applicationfilterchain.java:207) at Org.apache.tomcat.websocket.server.WsFilter.doFiLter (wsfilter.java:52) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:240) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:207) at Org.springframework.orm.hibernate5.support.OpenSessionInViewFilter.doFilterInternal ( opensessioninviewfilter.java:151) at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( onceperrequestfilter.java:107) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:240) at Org.apache.catalina.core.ApplicationFilterChain.doFilter ( applicationfilterchain.java:207) at Org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal ( characterencodingfilter.java:121) at Org.springframework.web.filter.OncePerRequestFilter.doFilter ( onceperrequestfilter.java:107) at Org.apache.catalina.core.ApplicationFilterChain.internalDoFilter ( applicationfilterchain.java:240) at Org.apache.catalina.core.Applicationfilterchain.dofilter (applicationfilterchain.java:207) at Org.apache.catalina.core.StandardWrapperValve.invoke (standardwrappervalve.java:212) at Org.apache.catalina.core.StandardContextValve.invoke (standardcontextvalve.java:106) at Org.apache.catalina.authenticator.AuthenticatorBase.invoke (authenticatorbase.java:502) at Org.apache.catalina.core.StandardHostValve.invoke (standardhostvalve.java:141) at Org.apache.catalina.valves.ErrorReportValve.invoke (errorreportvalve.java:79) at Org.apache.catalina.valves.AbstractAccessLogValve.invoke (abstractaccesslogvalve.java:616) at Org.apache.catalina.core.StandardEngineValve.invoke (standardenginevalve.java:88) at Org.apache.catalina.connector.CoyoteAdapter.service (coyoteadapter.java:528) at Org.apache.coyote.http11.AbstractHttp11Processor.process (abstracthttp11processor.java:1099) at Org.apache.coyote.abstractprotocol$abstractconnectionhandler.process (abstractprotocol.java:670) at org.apache.toMcat.util.net.nioendpoint$socketprocessor.dorun (nioendpoint.java:1520) at Org.apache.tomcat.util.net.NioEndpoint $SocketProcessor. Run (nioendpoint.java:1476) at Java.util.concurrent.ThreadPoolExecutor.runWorker (
    threadpoolexecutor.java:1142) at Java.util.concurrent.threadpoolexecutor$worker.run (ThreadPoolExecutor.java:617) At Org.apache.tomcat.util.threads.taskthread$wrappingrunnable.run (taskthread.java:61) at Java.lang.Thread.run ( thread.java:745) Log4j:error Failed to write [[2017-08-10 08:41:02]  

2, Hibernate.hbm.xml

<?xml version= "1.0"?>
<! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en"
"http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

3, controller to save the method code:

/** * Save XX information * @author Zhangsan * @date August 2, 2016 * @param deviceenterdetail XX Information * @param session * @re Turn * @throws Exception */@ResponseBody @RequestMapping ("/saveorupdatedetail") public Json Saveorupdatedetail (Device
    Enterdetail deviceenterdetail,httpsession session) throws exception{json JSON = new JSON (); String Operat = (Deviceenterdetail.getid () = null?
    "Add": "Edit");
        try {sessioninfo sessioninfo = (sessioninfo) session.getattribute ("Sessioninfo");
        Userdetail user = new Userdetail ();
        User.setid (Sessioninfo.getuserid ());
            if (deviceenterdetail.getid () = = null) {deviceenterdetail.setcreatedate (New Date ());
            Deviceenterdetail.setcreateuser (user);
        Deviceenterdetail.setstatus (1);
            }else{deviceenterdetail.setupdatedate (New Date ());
        Deviceenterdetail.setupdateuser (user);
        } dedetailservice.saveorupdate (Deviceenterdetail); Json.setsUccess (TRUE);
    Json.setmsg (Operat + "XX information success");
        catch (Exception e) {log.error (Operat + "xx Letter Information exception", e);
        Json.setsuccess (TRUE);
    Json.setmsg (Operat + "xx letter-Fine information failure");
} return JSON; }

4, the page below the figure
Equipment optional, not optional

<label for= "Usedev" class= "col-xs-1 Control-label" style= "White-space:nowrap"; >
<a class= "require" ><i class= "Icon-asterisk" ></i></a>
device name
</label >
<div class= "col-xs-3" >
     <div class= "Threepoint icon-downmenu3" onclick= "Selectdevice ()" >
     </div>
     <input type= "text" class= "Form-control sel" id= "Usedev" 
            placeholder= "Please select Device" readonly >
     <input type= "hidden" id= "DeviceId" name= "Device.id"/>     

5. Solution method
In the Saveorupdatedetail method in controller, in Dedetailservice.saveorupdate (Deviceenterdetail);

if (deviceenterdetail.getdevice () = null | | deviceenterdetail.getdevice (). getId () = null) {
      Deviceenterdetail.setdevice (null);
}

6, Summary:
I do not use cascading. is to save a single object, and the associated object, your page can not select the value of the associated object, that is, to the associated object does not select the ID value, so then? SPRINGMVC will bring your associated object back to new an empty object to this object, and this empty object is definitely not in the database, So when you save the object, you report an error in the transient state of the object that is associated with an optional value. The solution is to make a decision before hibernate save a single object. Whether the associated object is empty (note that the associated object entity configuration must configure the associated object to be null), and if it is null, set the associated object to null.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.