title: JPA remove referenced entity causes: org.hibernate.StaleStateException
body: <p>I need to delete all <code>Text</code> records which are referencing a specific <code>Post</code> record in database, by using JPA. By calling the remove method from the entity manager the delete statement: <code>delete from TEXT where pk_id=?</code> is printed twice and the following exception is thrown.</p>
  
  <p><code>org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1</code></p>
  
  <pre><code>14:44:26,495 WARN  [com.arjuna.ats.arjuna] (default task-2) ARJUNA012125: TwoPhaseCoordinator.beforeCompletion - failed for SynchronizationImple&lt; 0:ffffc0a8026a:-7653facf:54abe6a9:15, org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization@48c3c965 &gt;: javax.persistence.OptimisticLockException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.wrapStaleStateException(AbstractEntityManagerImpl.java:1800) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1705) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1677) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1683) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.jpa.spi.AbstractEntityManagerImpl$CallbackExceptionMapperImpl.mapManagedFlushFailure(AbstractEntityManagerImpl.java:1882) [hibernate-entitymanager-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:115) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.engine.transaction.synchronization.internal.RegisteredSynchronization.beforeCompletion(RegisteredSynchronization.java:50) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:76) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:358) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:91) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:162) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1166) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.internal.jta.transaction.arjunacore.BaseTransaction.commit(BaseTransaction.java:126) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.jbossatx.BaseTransactionManagerDelegate.commit(BaseTransactionManagerDelegate.java:75)
      at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.endTransaction(TransactionalInterceptorBase.java:147) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:93) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at com.arjuna.ats.jta.cdi.transactional.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:52) [narayana-jts-jacorb-5.0.0.Final.jar:5.0.0.Final (revision: 9aa71)]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_67]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_67]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_67]
      at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_67]
      at org.jboss.weld.interceptor.proxy.SimpleMethodInvocation.invoke(SimpleMethodInvocation.java:30) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNext(AbstractInterceptionChain.java:103) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.interceptor.chain.AbstractInterceptionChain.invokeNextInterceptor(AbstractInterceptionChain.java:81) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.executeInterception(InterceptorMethodHandler.java:48) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.interceptor.proxy.InterceptorMethodHandler.invoke(InterceptorMethodHandler.java:41) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.bean.proxy.CombinedInterceptorAndDecoratorStackMethodHandler.invoke(CombinedInterceptorAndDecoratorStackMethodHandler.java:53) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at at.pou.postit.dao.repository.TextRep$Proxy$_$$_WeldSubclass.remove(Unknown Source) [classes:]
      at at.pou.postit.dao.repository.TextRep$Proxy$_$$_WeldClientProxy.remove(Unknown Source) [classes:]
      at at.pou.postit.model.TestBean.deletePostOfUser(TestBean.java:242) [classes:]
      at at.pou.postit.model.TestBean$Proxy$_$$_WeldClientProxy.deletePostOfUser(Unknown Source) [classes:]
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_67]
      at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_67]
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_67]
      at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_67]
      at javax.el.ELUtil.invokeMethod(ELUtil.java:308) [jboss-el-api_3.0_spec-1.0.3.Final.jar:1.0.3.Final]
      at javax.el.BeanELResolver.invoke(BeanELResolver.java:537) [jboss-el-api_3.0_spec-1.0.3.Final.jar:1.0.3.Final]
      at javax.el.CompositeELResolver.invoke(CompositeELResolver.java:256) [jboss-el-api_3.0_spec-1.0.3.Final.jar:1.0.3.Final]
      at com.sun.el.parser.AstValue.invoke(AstValue.java:269) [javax.el-3.0.0.jar:]
      at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304) [javax.el-3.0.0.jar:]
      at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50) [weld-core-impl-2.1.2.Final.jar:2014-01-09 09:23]
      at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105) [jsf-impl-2.2.6-jbossorg-4.jar:]
      at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]
      at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102) [jsf-impl-2.2.6-jbossorg-4.jar:]
      at javax.faces.component.UICommand.broadcast(UICommand.java:315) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]
      at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]
      at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]
      at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81) [jsf-impl-2.2.6-jbossorg-4.jar:]
      at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) [jsf-impl-2.2.6-jbossorg-4.jar:]
      at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) [jsf-impl-2.2.6-jbossorg-4.jar:]
      at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) [jboss-jsf-api_2.2_spec-2.2.6.jar:2.2.6]
      at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.server.Connectors.executeRootHandler(Connectors.java:177) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727) [undertow-core-1.0.15.Final.jar:1.0.15.Final]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [rt.jar:1.7.0_67]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [rt.jar:1.7.0_67]
      at java.lang.Thread.run(Unknown Source) [rt.jar:1.7.0_67]
  Caused by: org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
      at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:81) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:73) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:63) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3400) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.persister.entity.AbstractEntityPersister.delete(AbstractEntityPersister.java:3630) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.action.internal.EntityDeleteAction.execute(EntityDeleteAction.java:114) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      at org.hibernate.engine.transaction.synchronization.internal.SynchronizationCallbackCoordinatorNonTrackingImpl.beforeCompletion(SynchronizationCallbackCoordinatorNonTrackingImpl.java:110) [hibernate-core-4.3.5.Final.jar:4.3.5.Final]
      ... 74 more
  </code></pre>
  
  <hr>
  
  <p>Here are the sources:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Date;
  import java.util.List;
  
  import javax.persistence.Access;
  import javax.persistence.AccessType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.ManyToMany;
  import javax.persistence.OneToMany;
  import javax.persistence.Table;
  
  import at.pou.postit.dao.repository.base.SingleIdEntity;
  
  @Entity(name = Post.TABLE_NAME)
  @Access(AccessType.FIELD)
  @Table(name = Post.TABLE_NAME)
  public class Post extends SingleIdEntity&lt;Integer&gt; {
  
      /**
       * 
       */
      private static final long serialVersionUID = 2351559621688678451L;
  
      public static final String TABLE_NAME = "POST";
  
      @Id
      @Column(name = "pk_id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      @Column(name = "title")
      private String title;
  
      @Column(name = "description")
      private String description;
  
      @Column(name = "cDate")
      private Date cDate;
  
      @Column(name = "mDate")
      private Date mDate;
  
      @OneToMany(mappedBy = "post")
      private List&lt;Posting&gt; postings;
  
      @OneToMany(mappedBy = "post")
      private List&lt;Text&gt; texts;
  
      @ManyToMany
      @JoinTable(name = "POST_CATEGORY", joinColumns = { @JoinColumn(name = "fk_postcat_post") }, inverseJoinColumns = { @JoinColumn(name = "fk_category") })
      private List&lt;Category&gt; categories;
  
      protected Post() {
  
      }
  
      public Post(final String title, final String description) {
          this(title, description, new Date(), null);
      }
  
      public Post(final String title, final String description, final Date cDate,
              final Date mDate) {
          this.setTitle(title);
          this.setDescription(description);
          this.setcDate(new Date());
          this.setmDate(null);
  
          this.postings = new ArrayList&lt;Posting&gt;();
          this.texts = new ArrayList&lt;Text&gt;();
          this.categories = new ArrayList&lt;Category&gt;();
      }
  
      @Override
      public Integer getId() {
          return id;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  
      public String getDescription() {
          return description;
      }
  
      public void setDescription(String description) {
          this.description = description;
      }
  
      public Date getcDate() {
          return cDate;
      }
  
      public void setcDate(Date cDate) {
          this.cDate = cDate;
      }
  
      public Date getmDate() {
          return mDate;
      }
  
      public void setmDate(Date mDate) {
          this.mDate = mDate;
      }
  
      public List&lt;Text&gt; getTexts() {
          return texts;
      }
  
      public List&lt;Category&gt; getCategories() {
          return categories;
      }
  
      public void addToCategory(Category cat) {
          if (!categories.contains(cat)) {
              categories.add(cat);
              cat.addPost(this);
          }
      }
  
      public void removeFromCategory(Category cat) {
          categories.remove(cat);
          cat.removePost(this);
      }
  
      /**
       * Keep this method protected since this list gets maintained by the
       * corresponding OWNER {@link Text} instances.
       * 
       * @param text
       *            {@link Text}
       */
      protected void addText(Text text) {
          this.texts.add(text);
      }
  
      /**
       * @see Post#addText(Text)
       * @param text
       *            {@link Text}
       */
      protected void removeText(Text text) {
          this.texts.remove(text);
      }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>import java.util.Date;
  
  import javax.persistence.Access;
  import javax.persistence.AccessType;
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.JoinColumn;
  import javax.persistence.JoinTable;
  import javax.persistence.ManyToOne;
  import javax.persistence.Table;
  
  import at.pou.postit.dao.repository.base.SingleIdEntity;
  
  @Entity(name = Text.TABLE_NAME)
  @Access(AccessType.FIELD)
  @Table(name = Text.TABLE_NAME)
  public class Text extends SingleIdEntity&lt;Integer&gt; {
  
      /**
       * 
       */
      private static final long serialVersionUID = -1664519900851671460L;
  
      public static final String TABLE_NAME = "TEXT";
  
      @Id
      @Column(name = "pk_id")
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Integer id;
  
      @Column(name = "subTitle")
      private String subTitle;
  
      @Column(name = "subDescription")
      private String subDescription;
  
      @Column(name = "cDate")
      private Date cDate;
  
      @Column(name = "mDate")
      private Date mDate;
  
      @Column(name = "content")
      private String content;
  
      @ManyToOne()
      @JoinTable(name = Text.TABLE_NAME)
      @JoinColumn(name = "fk_text_post")
      private Post post;
  
      protected Text() {
  
      }
  
      public Text(final String subTitle, final String subDescription,
              final String content) {
          this.setSubTitle(subTitle);
          this.setSubDescription(subDescription);
          this.setcDate(new Date());
          this.setmDate(null);
          this.setContent(content);
          this.post = null;
      }
  
      @Override
      public Integer getId() {
          return id;
      }
  
      public String getSubTitle() {
          return subTitle;
      }
  
      public void setSubTitle(String subTitle) {
          this.subTitle = subTitle;
      }
  
      public String getSubDescription() {
          return subDescription;
      }
  
      public void setSubDescription(String subDescription) {
          this.subDescription = subDescription;
      }
  
      public Date getcDate() {
          return cDate;
      }
  
      public void setcDate(Date cDate) {
          this.cDate = cDate;
      }
  
      public Date getmDate() {
          return mDate;
      }
  
      public void setmDate(Date mDate) {
          this.mDate = mDate;
      }
  
      public String getContent() {
          return content;
      }
  
      public void setContent(String content) {
          this.content = content;
      }
  
      public Post getPost() {
          return post;
      }
  
      public void setPost(final Post post) {
          if (this.post != null) {
              this.post.removeText(this);
          }
          this.post = post;
  
          if (this.post != null) {
              this.post.addText(this);
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Here, the method called from <code>TestBean</code>:</p>
  
  <pre><code>public void deleteTextOfPosting() {
      Posting posting = postingRep.find(userId, postId);
      if (posting == null) {
          return;
      }
      Post p = postRep.findByIdEager(posting.getPost().getId());
  
      /*
       * Although there is a foreach loop, the post just contains a single
       * Text instance.
       */
      for (Text t : p.getTexts()) {
          System.out.println("Text-instance: " + t.toString());
          t.setPost(null);
          textRep.remove(t); // Here the exception is thrown!!!
      }
  }
  </code></pre>
  
  <p>And here the overridden remove method from <code>textRep</code>:</p>
  
  <pre><code>@Override
  public boolean remove(E entity) {
      if (entity == null) {
          return false;
      }
  
      if (!this.entityManager.contains(entity)) {
          if (!isIdSet(entity)) {
              return false;
          }
  
          entity = findById(entity.getId());
          if (entity == null) {
              return false;
          }
      }
  
      this.entityManager.remove(entity);
      return true;
  }
  </code></pre>
  
  <p>The logged SQL statements:</p>
  
  <pre><code>15:14:42,867 INFO  [stdout] (default task-2) Hibernate: select posting0_.pk_id as pk_id1_3_, posting0_.allowSharing as allowSha2_3_, posting0_.grantFromUser as grantFro3_3_, posting0_.owner as owner4_3_, posting0_.fk_perm as fk_perm6_3_, posting0_.fk_posting_post as fk_posti7_3_, posting0_.private as private5_3_, posting0_.fk_user as fk_user8_3_ from POSTING posting0_ inner join POST post1_ on posting0_.fk_posting_post=post1_.pk_id inner join USER user2_ on posting0_.fk_user=user2_.pk_id where user2_.pk_id=? and post1_.pk_id=?
  
  15:14:42,961 INFO  [stdout] (default task-2) Hibernate: select permission0_.pk_id as pk_id1_1_0_, permission0_.perm as perm2_1_0_ from PERMISSION permission0_ where permission0_.pk_id=?
  
  15:14:42,982 INFO  [stdout] (default task-2) Hibernate: select post0_.pk_id as pk_id1_2_0_, post0_.cDate as cDate2_2_0_, post0_.description as descript3_2_0_, post0_.mDate as mDate4_2_0_, post0_.title as title5_2_0_ from POST post0_ where post0_.pk_id=?
  
  15:14:43,020 INFO  [stdout] (default task-2) Hibernate: select user0_.pk_id as pk_id1_6_0_, user0_.eMail as eMail2_6_0_, user0_.fName as fName3_6_0_, user0_.failedLogins as failedLo4_6_0_, user0_.lName as lName5_6_0_, user0_.lastLogin as lastLogi6_6_0_, user0_.lastPwdChange as lastPwdC7_6_0_ from USER user0_ where user0_.pk_id=?
  
  15:14:43,900 INFO  [stdout] (default task-2) Hibernate: select post0_.pk_id as pk_id1_2_0_, post0_.cDate as cDate2_2_0_, post0_.description as descript3_2_0_, post0_.mDate as mDate4_2_0_, post0_.title as title5_2_0_ from POST post0_ where post0_.pk_id=?
  
  15:14:43,914 INFO  [stdout] (default task-2) Hibernate: select categories0_.fk_postcat_post as fk_postc1_2_0_, categories0_.fk_category as fk_categ2_4_0_, category1_.pk_id as pk_id1_0_1_, category1_.description as descript2_0_1_, category1_.name as name3_0_1_ from POST_CATEGORY categories0_ inner join CATEGORY category1_ on categories0_.fk_category=category1_.pk_id where categories0_.fk_postcat_post=?
  
  15:14:43,937 INFO  [stdout] (default task-2) Hibernate: select texts0_.fk_text_post as fk_text_7_2_0_, texts0_.pk_id as pk_id1_5_0_, texts0_.pk_id as pk_id1_5_1_, texts0_.cDate as cDate2_5_1_, texts0_.content as content3_5_1_, texts0_.mDate as mDate4_5_1_, texts0_.fk_text_post as fk_text_7_5_1_, texts0_.subDescription as subDescr5_5_1_, texts0_.subTitle as subTitle6_5_1_ from TEXT texts0_ left outer join TEXT texts0_1_ on texts0_.pk_id=texts0_1_.pk_id where texts0_.fk_text_post=?
  
  15:14:45,662 INFO  [stdout] (default task-2) Text-instance: Text{id=6}
  
  15:14:57,545 INFO  [stdout] (default task-2) Hibernate: select text0_.pk_id as pk_id1_5_0_, text0_.cDate as cDate2_5_0_, text0_.content as content3_5_0_, text0_.mDate as mDate4_5_0_, text0_.fk_text_post as fk_text_7_5_0_, text0_.subDescription as subDescr5_5_0_, text0_.subTitle as subTitle6_5_0_, post1_.pk_id as pk_id1_2_1_, post1_.cDate as cDate2_2_1_, post1_.description as descript3_2_1_, post1_.mDate as mDate4_2_1_, post1_.title as title5_2_1_ from TEXT text0_ left outer join TEXT text0_1_ on text0_.pk_id=text0_1_.pk_id left outer join POST post1_ on text0_.fk_text_post=post1_.pk_id where text0_.pk_id=?
  
  15:14:57,581 INFO  [stdout] (default task-2) Hibernate: delete from TEXT where pk_id=?
  
  15:14:57,604 INFO  [stdout] (default task-2) Hibernate: delete from TEXT where pk_id=?
  </code></pre>