title: Hibernate: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry
body: <p>I was faced to the following error:</p>
  
  <pre><code>com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract void com.xxx.Service.save(com.xxx.Bean)' threw an unexpected exception: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)
      at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:385)
      at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:588)
      at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:208)
      at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:248)
      at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:496)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
      at java.lang.Thread.run(Thread.java:680)
  Caused by: org.hibernate.AssertionFailure: null id in com.xxx.Bean entry (don't flush the Session after an exception occurs)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:82)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:190)
      at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:147)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:219)
      at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:99)
      at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
      at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1216)
      at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:383)
      at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:133)
      at com.xxx.ServiceImpl.save(ServiceImpl.java:32)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      at java.lang.reflect.Method.invoke(Method.java:597)
      ... 23 more
  </code></pre>
  
  <p>Here is MySQL table definition:</p>
  
  <pre><code>CREATE TABLE `tblARF` (
    `ARF_ID` bigint(19) NOT NULL AUTO_INCREMENT,
    `ARF_MANUFACTURER_MNF_ID` bigint(19) NOT NULL,
    ...
    PRIMARY KEY (`ARF_ID`),
    KEY `ARF_MANUFACTURER_MNF_ID` (`ARF_MANUFACTURER_MNF_ID`)
  )
  </code></pre>
  
  <p>And here is Hibernate mapping:</p>
  
  <pre><code>&lt;class name="Bean" table="tblARF"&gt;
    &lt;id name="key" type="long" unsaved-value="null"&gt;
      &lt;column name="ARF_ID" not-null="true"/&gt;
      &lt;generator class="identity" /&gt;
    &lt;/id&gt;
  &lt;many-to-one column="ARF_MANUFACTURER_MNF_ID" name="manufacturer"
      class="ManufacturerBean" not-null="true" lazy="false"/&gt;
  ...
  &lt;/class&gt;
  </code></pre>
  
  <p>Java code:</p>
  
  <pre><code>Session s = hibernate.getCurrentSession();
  Transaction t = s.beginTransaction();
  s.merge(bean.getManufacturer());
  s.save(bean);
  t.commit();
  </code></pre>
  
  <p>I noticed that if I remove <code>KEY ARF_MANUFACTURER_MNF_ID (ARF_MANUFACTURER_MNF_ID)</code> than <code>AssertionFailure</code> doesn't happen. Is there a way to avoid this error without removing the KEY?</p>
  
  <p><a href="https://stackoverflow.com/questions/1838520">This solution</a> doesn't work for me unfortunately.</p>