title: Unable to insert into database using JPA. Tried merge, persist and INSERT query
body: <p>I am trying to add an entry into AttributeName:</p>
  
  <pre><code>@Entity
  public class AttributeName implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "attNameId", nullable = false)
      private Long attNameId;
  
      @Column(name = "attName", unique = true, nullable = false, length = 200)
      private String attName;
  
      @Column(name = "required", nullable = false)
      private boolean required;
  
      ...
  
  @Stateless
  @LocalBean
  public class AttributeNameManagerBean implements AttributeNameManagerRemote
  {
      @PersistenceContext(unitName = "mainPU")
      private EntityManager entityManager;
  
      public AttributeNameManagerBean()
      {
  
      }
  
      ...
  </code></pre>
  
  <p>I first must add this entry and use the newly created id to insert into the Attribute which stores this id (to link to an AttributeValue).</p>
  
  <p>I have tried:</p>
  
  <ul>
  <li><code>AttributeName savedAttName = entityManager.merge(newAttName);</code></li>
  <li><code>entityManager.persist(newAttName); entityManager.flush();</code></li>
  <li><code>entityManager.create(Native)Query("INSERT...");</code></li>
  </ul>
  
  <p>I receive a <code>ConstraintViolationException</code> later but the main problem is that the insert statement in any of the 3 above forms does not throw an exception, but when I query the database the new entry is not there.
  In the first example, <code>savedAttName</code> contains the newly generated unqiue PK :S</p>
  
  <p>Note: I am sure that <code>attName</code> is a new unique entry into the database.]</p>
  
  <p>Any help would be greatly appreciated. Happy to provide any more information if required.</p>
  
  <p><strong>Edit</strong>: There is an attNameId field in Attribute. Attribute has two unique constraints: </p>
  
  <p><code>uniqueConstraints={
          @UniqueConstraint(columnNames = {"attNameId", "attValueId"} ),
          @UniqueConstraint(columnNames = {"sectionId", "sectionIndex"} )}</code></p>
  
  <p>The sectionId and sectionIndex being entered are a unique combination which doesn't exist currently in AttributeName (which only has 1 entry, which I manually inserted).</p>
  
  <p><strong>Fix:</strong>
  Removed the constraints from the table, reployed. Then dropped the table, re-added the constraints and it works. I'm guessing it didn't correctly set up the constraints.</p>