title: hibernate - cascading type persist for many to many relationship
body: <p>I have two classes Policy and Acl. They have a ManyToMany relationship in them.</p>
  
  <p>// Policy</p>
  
  <pre><code>public class PolicyEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name = "POLICY_ID")
      private int policyId;   
      @Column(name = "POLICY_NAME")
      private String policyName;
      @ManyToMany(cascade = CascadeType.PERSIST)
      @JoinTable(name = "POLICY_ACL",joinColumns=@JoinColumn(name = "POLICY_ID"),
                                      inverseJoinColumns=@JoinColumn(name = "ACL_ID"))
      private List&lt;AclEntity&gt; aclList = new ArrayList&lt;AclEntity&gt;();
  }
  </code></pre>
  
  <p>// Acl</p>
  
  <pre><code>public class AclEntity {
      @Id
      @GeneratedValue(strategy = GenerationType.SEQUENCE)
      @Column(name = "ACL_ID")
      private int aclId;  
      @Column(name = "ACL_NAME")
      private String aclName;
      @ManyToMany(mappedBy = "aclList")
      private List&lt;PolicyEntity&gt; policyList = new ArrayList&lt;PolicyEntity&gt;();
  }
  </code></pre>
  
  <p>Now when I use Cascade type as persist then I got the following error : </p>
  
  <pre><code>Hibernate: insert into POLICY (POLICY_NAME, POLICY_ID) values (?, ?)
  Hibernate: insert into POLICY_ACL (POLICY_ID, ACL_ID) values (?, ?)
  Exception in thread "main" org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: hibernate.AclEntity
  </code></pre>
  
  <p>Whereas when I use cascade type as all then it saves the policy entity and acl entity successfully.I am unable to understand how that is happening.
  Note : I am just performing the save operation.</p>
  
  <p>Here is the main code : </p>
  
  <pre><code>public static void main(String[] args) {
  
          PolicyEntity policyEntity = new PolicyEntity();
          AclEntity aclEntity = new AclEntity();
  
          //policyEntity.setPolicyId(1);
          policyEntity.setPolicyName("Policy 3");
          //aclEntity.setAclId(1);
          aclEntity.setAclName("Acl 3");
  
          policyEntity.getAclList().add(aclEntity);
  
          Configuration configuration = new Configuration().configure();
          StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder().
          applySettings(configuration.getProperties());
          SessionFactory sessionFactory = configuration.buildSessionFactory(builder.build());
          Session session = sessionFactory.openSession();
          session.beginTransaction();
  
          session.save(policyEntity);
  
          session.getTransaction().commit();
          session.close();
      }
  </code></pre>