title: JPA Null or zero primary key encountered in unit of work clone
body: <p>I learn about <code>JPA</code> and had task to make database and insert some values to it. I wondered how I can find out what was the ID of recently inserted object, so I found a way that I need to use <code>flush</code> method of <code>EntityManager</code>.</p>
  
  <p>Unfortunately I got the</p>
  
  <blockquote>
    <p>Null or zero primary key encountered in unit of work clone</p>
  </blockquote>
  
  <p>exception when I use the above method. I think the problem lies in that my database has all <code>ID's</code> set on autoincrement ( <strong>I use ORACLE 11G Express</strong> ), so before commiting  it has <code>null</code> value and it rollbacks transaction. </p>
  
  <p><strong>What I can do to fix it ?</strong></p>
  
  <p>This is DB ( ID's are autoincrement[Sequences and Triggers in oracle]):
  <a href="https://imgur.com/2GF7UM2"><img src="https://i.imgur.com/2GF7UM2.png" title="Hosted by imgur.com" /></a></p>
  
  <pre><code>public class Client {    
      public static void main(String[] args) {
          EntityManagerFactory emf =
                  Persistence.createEntityManagerFactory("JpaIntroductionPU");        
          EntityManager em = emf.createEntityManager();
          EntityTransaction et = em.getTransaction();
  
          et.begin();
  
          Address ad1 = new Address();
          ad1.setStreet("Skaraktki");
          ad1.setCode("64-340");
  
          em.persist(ad1);
          em.flush();
  
          System.out.println(ad1.getAId());
          et.commit();
      }   
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Address class</strong></p>
  </blockquote>
  
  <pre><code>@Entity
  @Table(name = "ADDRESS")
  @NamedQueries({
      @NamedQuery(name = "Address.findAll", query = "SELECT a FROM Address a"),
      @NamedQuery(name = "Address.findByAId", query = "SELECT a FROM Address a WHERE a.aId = :aId"),
      @NamedQuery(name = "Address.findByStreet", query = "SELECT a FROM Address a WHERE a.street = :street"),
      @NamedQuery(name = "Address.findByCode", query = "SELECT a FROM Address a WHERE a.code = :code")})
  public class Address implements Serializable {
      private static final long serialVersionUID = 1L;
      // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
      @Id
      @Basic(optional = false)
      @Column(name = "A_ID")
      private BigDecimal aId;
  
      @Basic(optional = false)
      @Column(name = "STREET")
      private String street;
  
      @Basic(optional = false)
      @Column(name = "CODE")
      private String code;
  
      @OneToOne(cascade = CascadeType.ALL, mappedBy = "aId")
      private Employee employee;
  
      @OneToOne(cascade = CascadeType.ALL, mappedBy = "aId")
      private Department department;
  
      public Address() {
      }
  
      public Address(BigDecimal aId) {
          this.aId = aId;
      }
  
      public Address(BigDecimal aId, String street, String code) {
          this.aId = aId;
          this.street = street;
          this.code = code;
      }
  
      public BigDecimal getAId() {
          return aId;
      }
  
      public void setAId(BigDecimal aId) {
          this.aId = aId;
      }
  
      public String getStreet() {
          return street;
      }
  
      public void setStreet(String street) {
          this.street = street;
      }
  
      public String getCode() {
          return code;
      }
  
      public void setCode(String code) {
          this.code = code;
      }
  
      public Employee getEmployee() {
          return employee;
      }
  
      public void setEmployee(Employee employee) {
          this.employee = employee;
      }
  
      public Department getDepartment() {
          return department;
      }
  
      public void setDepartment(Department department) {
          this.department = department;
      }       
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (aId != null ? aId.hashCode() : 0);
          return hash;
      }
  
      @Override
      public boolean equals(Object object) {
          // TODO: Warning - this method won't work in the case the id fields are not set
          if (!(object instanceof Address)) {
              return false;
          }
          Address other = (Address) object;
          if ((this.aId == null &amp;&amp; other.aId != null) || (this.aId != null &amp;&amp; !this.aId.equals(other.aId))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "jpaintroduction.Address[ aId=" + aId + " ]";
      }
  
  }
  </code></pre>