title: JPA how to update existing object data without exception?
body: <p>I try to update existing data rows in database but i get that exception:</p>
  
  <pre><code>[EL Warning]: 2012-10-24 20:02:27.798--UnitOfWork(22664464)--Exception [EclipseLink-4002] (Eclipse Persistence Services -
  2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '20-http://www.vilpra.lt/products/Foto/Aremikas/Katilas_zvake_big' for key 'PRIMARY' Error Code: 1062 Call: INSERT INTO x_links_media (image, link_id) VALUES (?, ?)  bind =&gt; [2 parameters bound] Query: InsertObjectQuery(database.entity.XLinksMedia[ xLinksMediaPK=database.entity.XLinksMediaPK[ linkId=20, image=http://www.link.lt/products.jpg ] ])
  </code></pre>
  
  <p>The main object class is look like code below. There is some variables with relation OneToMany. </p>
  
  <pre><code>package database.entity;
  
  import java.io.Serializable;
  import java.util.Date;
  import java.util.List;
  import javax.persistence.Basic;
  import javax.persistence.CascadeType;
  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.NamedQueries;
  import javax.persistence.NamedQuery;
  import javax.persistence.OneToMany;
  import javax.persistence.OneToOne;
  import javax.persistence.Table;
  import javax.persistence.Temporal;
  import javax.persistence.TemporalType;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @Entity
  @Table(name = "x_parser_links")
  @XmlRootElement
  @NamedQueries({
      @NamedQuery(name = "XParserLinks.findAll", query = "SELECT x FROM XParserLinks x"),
      @NamedQuery(name = "XParserLinks.findByLinkId", query = "SELECT x FROM XParserLinks x WHERE x.linkId = :linkId"),
      @NamedQuery(name = "XParserLinks.findByPageId", query = "SELECT x FROM XParserLinks x WHERE x.pageId = :pageId"),
      @NamedQuery(name = "XParserLinks.findByLink", query = "SELECT x FROM XParserLinks x WHERE x.link = :link"),
      @NamedQuery(name = "XParserLinks.findByLevel", query = "SELECT x FROM XParserLinks x WHERE x.level = :level"),
      @NamedQuery(name = "XParserLinks.findByLinkType", query = "SELECT x FROM XParserLinks x WHERE x.linkType = :linkType"),
      @NamedQuery(name = "XParserLinks.findByCreateDate", query = "SELECT x FROM XParserLinks x WHERE x.createDate = :createDate"),
      @NamedQuery(name = "XParserLinks.findByDelDate", query = "SELECT x FROM XParserLinks x WHERE x.delDate = :delDate")})
  public class XParserLinks implements Serializable {
  
      private static final long serialVersionUID = 1L;
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Basic(optional = false)
      @Column(name = "link_id")
      private Integer linkId;
      @Column(name = "page_id")
      private Integer pageId;
      @Column(name = "link")
      private String link;
      @Column(name = "level")
      private Integer level;
      @Column(name = "link_type")
      private Short linkType;
      @Column(name = "create_date")
      @Temporal(TemporalType.TIMESTAMP)
      private Date createDate;
      @Column(name = "del_date")
      @Temporal(TemporalType.TIMESTAMP)
      private Date delDate;
      @JoinColumn(name = "tev_link_id")
      @OneToOne(cascade = CascadeType.ALL)
      private XParserLinks tevas;
      @OneToMany(mappedBy = "xParserLink", targetEntity = XLinksMedia.class, cascade = CascadeType.ALL)
      private List&lt;XLinksMedia&gt; fotos;
      @OneToMany(mappedBy = "xParserLink", targetEntity = XLinksVarchar.class, cascade = CascadeType.ALL)
      private List&lt;XLinksVarchar&gt; atributes;    
  
      public XParserLinks() {
      }
  
      public XParserLinks(Integer linkId) {
          this.linkId = linkId;
      }
  
      public Integer getLinkId() {
          return linkId;
      }
  
      public void setLinkId(Integer linkId) {
          this.linkId = linkId;
      }
  
      public Integer getPageId() {
          return pageId;
      }
  
      public void setPageId(Integer pageId) {
          this.pageId = pageId;
      }
  
      public String getLink() {
          return link;
      }
  
      public void setLink(String link) {
          this.link = link;
      }
  
      public Integer getLevel() {
          return level;
      }
  
      public void setLevel(Integer level) {
          this.level = level;
      }
  
      public Short getLinkType() {
          return linkType;
      }
  
      public void setLinkType(Short linkType) {
          this.linkType = linkType;
      }
  
      public Date getCreateDate() {
          return createDate;
      }
  
      public void setCreateDate(Date createDate) {
          this.createDate = createDate;
      }
  
      public Date getDelDate() {
          return delDate;
      }
  
      public void setDelDate(Date delDate) {
          this.delDate = delDate;
      }
  
      public XParserLinks getTevas() {
          return tevas;
      }
  
      public void setTevas(XParserLinks tevas) {
          this.tevas = tevas;
      }
  
      public List&lt;XLinksMedia&gt; getFotos() {
          return fotos;
      }
  
      public void setFotos(List&lt;XLinksMedia&gt; fotos) {
          this.fotos = fotos;
      }
  
      public List&lt;XLinksVarchar&gt; getAtributes() {
          return atributes;
      }
  
      public void setAtributes(List&lt;XLinksVarchar&gt; atributes) {
          this.atributes = atributes;
      }
  
      @Override
      public int hashCode() {
          int hash = 0;
          hash += (linkId != null ? linkId.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 XParserLinks)) {
              return false;
          }
          XParserLinks other = (XParserLinks) object;
          if ((this.linkId == null &amp;&amp; other.linkId != null) || (this.linkId != null &amp;&amp; !this.linkId.equals(other.linkId))) {
              return false;
          }
          return true;
      }
  
      @Override
      public String toString() {
          return "database.entity.XParserLinks[ linkId=" + linkId + " ]";
      }
  }
  </code></pre>
  
  <p>And here is code where I want to proceed data. <code>XParserLinks</code> object is the man object like code above. In this example I check if object do not have his primary key LinkId then create new object with persist, but else just update object and his values, but I get exception like I mean before when I want to update existing object.</p>
  
  <pre><code>        XParserLinks e = entry.getValue();
  
          if (e.getLinkId() == null) {
              try {
                  TarpineManager.startTransaction();
  
                  TarpineManager.persist(e);
  
                  TarpineManager.commitTransaction();
  
              } catch (Exception ex) {
                  ex.printStackTrace();
                  if (TarpineManager.getInstance().getTransaction().isActive()) {
                      TarpineManager.rollbackTransaction();
                  }
              }
          } else {
  
              try {
  
                  TarpineManager.startTransaction();
  
                  TarpineManager.commitTransaction();
  
              } catch (Exception ex) {
                  ex.printStackTrace();
                  if (TarpineManager.getInstance().getTransaction().isActive()) {
                      TarpineManager.rollbackTransaction();
                  }
              }
  
          }
  </code></pre>