title: Spring JPA with Hibernate: Error during managed flush [Session/EntityManager is closed]
body: <p>I am in the process of updating my application to hibernate 5.2.x and Spring 4.3.x and Spring data JPA 1.11.x</p>
  
  <p>A current problem I can't figure out is related to a number of integration tests  failing with the error:</p>
  
  <pre><code>HHH000346: Error during managed flush [Session/EntityManager is closed]
  </code></pre>
  
  <p>(The tests are based on Arquillian/rest assured and were previously passing. I am using @Transactional annotations on the service methods as well as the repository interfaces.) 
  There seems to be a problem with the managed flush initiated by Spring - as if i add a manually call to flush() at the end of the service method, the test passes.</p>
  
  <p>An example of a full trace logs for hibernate + spring are available here: <a href="http://pastebin.com/LiEU7WAg" rel="nofollow noreferrer">http://pastebin.com/LiEU7WAg</a></p>
  
  <p>I was hoping someone could identify from the logs what is causing the entity manager to be closed causing the Hibernate error to be thrown.</p>
  
  <p><strong>Edit one</strong>
  Below is the entity hierarchy </p>
  
  <pre><code>@Audited
  @Entity
  @Table(uniqueConstraints = @UniqueConstraint(columnNames = {"group_id",   "type"}))
  public class GroupPreference extends AbstractPreference&lt;GroupPreferenceType&gt; {
  
  @ManyToOne(fetch = FetchType.EAGER, optional = false)
  @JoinColumn(name = "group_id")
  @Index(name = "idx_group_pref_group_id")
  private Group group;
  
  @Enumerated(EnumType.STRING)
  private GroupPreferenceType type;
  }
  
  
  @Audited
  @Entity
  @Inheritance(strategy = InheritanceType.JOINED)
  @Table(name="preference")
  public abstract class AbstractPreference&lt;T extends Enum&lt;T&gt; &amp; PreferenceType&gt; extends Identifiable {
  
  @CollectionTable(name = "preference_entry", joinColumns = {@JoinColumn(name = "preference_id")})
  @ElementCollection(fetch = FetchType.LAZY)
  @Column(name = "entry_value", length = ENTRY_VALUE_LENGTH)
  @MapKeyColumn(name = "entry_key")
  private Map&lt;String, String&gt; entries = new HashMap&lt;&gt;();
  
  }
  
  
  @MappedSuperclass
  public abstract class Identifiable {
  
  @Id
  @GeneratedValue(generator = "uuid")
  @GenericGenerator(name = "uuid", strategy = "uuid2", parameters = {
  @org.hibernate.annotations.Parameter(name = UUIDGenerator.UUID_GEN_STRATEGY_CLASS,
                                           value="SequentialVersionOneStrategy")
  })
  private String id;
  
  }
  </code></pre>
  
  <p>The test is attempting to delete a group preference, which calls through to delete in the Spring data CrudRepository. </p>