title: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException Duplicate entry
body: <p>I work with a Spring boot project with MySQL database and the entities are provided, </p>
  
  <p>The IP address entity, </p>
  
  <pre><code>@Entity
  public class IpAddress {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "IP_ADDR_ID")
      private Long id;
  
      @Column(name = "IP_ADDRESS")
      @NotEmpty
      private String address;
  
  
      @JsonIgnore
      @ManyToMany(cascade = CascadeType.ALL, mappedBy = "ipAddresses")
      private List&lt;HttpInfoMessage&gt; httpInfoMessages = new ArrayList&lt;&gt;();
  
      public IpAddress() {
      }
  
      public IpAddress(String address) {
          this.address = address;
      }
  
      public IpAddress(String address, List&lt;HttpInfoMessage&gt; httpInfoMessages) {
          this.address = address;
          this.httpInfoMessages = httpInfoMessages;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
      public List&lt;HttpInfoMessage&gt; getHttpInfoMessages() {
          return httpInfoMessages;
      }
  
      public void setHttpInfoMessages(List&lt;HttpInfoMessage&gt; httpInfoMessages) {
          this.httpInfoMessages = httpInfoMessages;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (!(o instanceof IpAddress)) return false;
  
          IpAddress ipAddress = (IpAddress) o;
  
          if (!getId().equals(ipAddress.getId())) return false;
          return getAddress().equals(ipAddress.getAddress());
      }
  
      @Override
      public int hashCode() {
          int result = getId().hashCode();
          result = 31 * result + getAddress().hashCode();
          return result;
      }
  
      @Override
      public String toString() {
          return "IpAddress{" +
                  "id=" + id +
                  ", address='" + address + '\'' +
                  '}';
      }
  }
  </code></pre>
  
  <p>The Http message entity, </p>
  
  <pre><code>@Entity
  public class HttpInfoMessage {
  
      @Id
  //    @GeneratedValue(strategy = GenerationType.IDENTITY)
      @Column(name = "STATUS_ID")
      private Long statusId;
  
      @Column(name = "STATUS")
      @NotEmpty
      private String status;
  
      public HttpInfoMessage() {
  
      }
  
      public HttpInfoMessage(String status) {
          this.status = status;
      }
  
      public HttpInfoMessage(Long statusId, String status) {
          this.statusId = statusId;
          this.status = status;
      }
  
      public HttpInfoMessage(Long statusId, String status, List&lt;IpAddress&gt; ipAddresses) {
          this.statusId = statusId;
          this.status = status;
          this.ipAddresses = ipAddresses;
      }
  
      public Long getStatusId() {
          return statusId;
      }
  
      public void setStatusId(Long statusId) {
          this.statusId = statusId;
      }
  
      public String getStatus() {
          return status;
      }
  
      public void setStatus(String status) {
          this.status = status;
      }
  
      //    @ManyToMany(cascade = CascadeType.ALL, mappedBy = "httpInfoMessages")
      @ManyToMany(cascade = CascadeType.ALL)
      @JoinTable(name = "IP_ADDR_STATUS",
              joinColumns = @JoinColumn(name = "STATUS_ID", referencedColumnName = "STATUS_ID"),
              inverseJoinColumns = @JoinColumn(name = "IP_ADDRESS_ID", referencedColumnName = "IP_ADDR_ID"))
      private List&lt;IpAddress&gt; ipAddresses = new ArrayList&lt;&gt;();
  
      public List&lt;IpAddress&gt; getIpAddresses() {
          return ipAddresses;
      }
  
      public void setIpAddresses(List&lt;IpAddress&gt; ipAddresses) {
          this.ipAddresses = ipAddresses;
      }
  
      @Override
      public boolean equals(Object o) {
          if (this == o) return true;
          if (!(o instanceof HttpInfoMessage)) return false;
  
          HttpInfoMessage httpInfoMessage1 = (HttpInfoMessage) o;
  
          if (!getStatusId().equals(httpInfoMessage1.getStatusId())) return false;
          return getStatus().equals(httpInfoMessage1.getStatus());
      }
  
      @Override
      public int hashCode() {
          int result = getStatusId().hashCode();
          result = 31 * result + getStatus().hashCode();
          return result;
      }
  
      @Override
      public String toString() {
          return "Status{" +
                  "statusId=" + statusId +
                  ", status='" + status + '\'' +
                  '}';
      }
  }
  </code></pre>
  
  <p>I read the server log entries from a text file and perform some manipulation on them and eventually, I get the <code>List&lt;IpAddress&gt;</code>.</p>
  
  <p>After I get the <code>List&lt;IpAddress&gt;</code>, I printed them just to show, </p>
  
  <pre><code>The IP list size is = 5
  
  IP address = IpAddress{id=null, address='177.132.239.67'}
  Status{statusId=403, status='403_Forbidden'}
  Status{statusId=404, status='404_Not Found'}
  Status{statusId=405, status='405_Method Not Allowed'}
  Status{statusId=406, status='406_Not Acceptable'}
  
  IP address = IpAddress{id=null, address='178.133.239.67'}
  Status{statusId=403, status='403_Forbidden'}
  Status{statusId=404, status='404_Not Found'}
  Status{statusId=405, status='405_Method Not Allowed'}
  Status{statusId=406, status='406_Not Acceptable'}
  
  IP address = IpAddress{id=null, address='175.130.239.67'}
  Status{statusId=403, status='403_Forbidden'}
  Status{statusId=404, status='404_Not Found'}
  Status{statusId=405, status='405_Method Not Allowed'}
  Status{statusId=406, status='406_Not Acceptable'}
  
  IP address = IpAddress{id=null, address='176.131.239.67'}
  Status{statusId=403, status='403_Forbidden'}
  Status{statusId=404, status='404_Not Found'}
  Status{statusId=405, status='405_Method Not Allowed'}
  Status{statusId=406, status='406_Not Acceptable'}
  
  IP address = IpAddress{id=null, address='174.129.239.67'}
  Status{statusId=403, status='403_Forbidden'}
  Status{statusId=404, status='404_Not Found'}
  Status{statusId=405, status='405_Method Not Allowed'}
  Status{statusId=406, status='406_Not Acceptable'}
  </code></pre>
  
  <p>I then tried to save all the entries by iterating through them,</p>
  
  <pre><code>ipAddresses.forEach(
                  ipAddress -&gt; {
                      ipAddressService.save(ipAddress);
                  }
          );
  </code></pre>
  
  <p>I get the error at the time of saving the entries, </p>
  
  <pre><code>java.lang.IllegalStateException: Failed to execute CommandLineRunner
  
  Caused by: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [PRIMARY]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  
  Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
  
  Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '403' for key 'PRIMARY'
  </code></pre>
  
  <p>I can provide more info if required</p>