当前位置: 首页 > 知识库问答 >
问题:

com.mysql.jdbc.exceptions.jdbc4.mysqlintegrityConstraintViolationException重复项

龙令雪
2023-03-14

我使用一个带有MySQL数据库的Spring boot项目,

IP地址实体,

@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<HttpInfoMessage> httpInfoMessages = new ArrayList<>();

    public IpAddress() {
    }

    public IpAddress(String address) {
        this.address = address;
    }

    public IpAddress(String address, List<HttpInfoMessage> 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<HttpInfoMessage> getHttpInfoMessages() {
        return httpInfoMessages;
    }

    public void setHttpInfoMessages(List<HttpInfoMessage> 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 + '\'' +
                '}';
    }
}
@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<IpAddress> 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<IpAddress> ipAddresses = new ArrayList<>();

    public List<IpAddress> getIpAddresses() {
        return ipAddresses;
    }

    public void setIpAddresses(List<IpAddress> 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 + '\'' +
                '}';
    }
}
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'}
ipAddresses.forEach(
                ipAddress -> {
                    ipAddressService.save(ipAddress);
                }
        );

我在保存条目时得到错误,

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'

如果需要,我可以提供更多信息

共有1个答案

上官培
2023-03-14

在httpService-Entity中,statusid字段是主键。您的testdata在每个ipaddress-block的statusid中设置了403-406值。如果从statusid中移除这些值并将它们设置为null,它应该可以工作。

 类似资料:
  • 问题内容: 我有一个谷歌应用程序引擎连接的项目,我试图通过它连接到谷歌云sql数据库。我将项目上传到谷歌,然后通过它我尝试连接到数据库。 我的连接URL如下-> 正是在我尝试与url建立连接的时候,我遇到了异常 然后,我将以下字符串添加到url->&autoReconnect = true&failOverReadOnly = false&maxReconnects = 10 然后收到另一个异常-

  • 问题内容: 我正在使用Mac OS X Lion版本10.8。我通过XAMPP安装了MySQL。并且我已经将mysql和jdbc驱动程序(5.1.22)添加到了我的类路径中。以下简单代码无法正常工作: 运行代码时,出现以下错误: 有谁知道如何解决这个问题? 问题答案: 我终于弄明白了。感谢Max和Yogendra发布。问题出在我的XAMPP配置上。要解决此问题,我做了如下操作: 然后我运行我的代码

  • 问题内容: 这个问题已经在这里有了答案 : “软件导致连接中止:套接字写入错误”的官方原因 (14个答案) 3年前关闭。 所有。我已经遇到了几天的问题,这是详细信息堆栈信息: 我想通过mybatis和spring更新mysql中的一条记录,然后一个接一个地更新它,下面是mybatis config:更新建议集count =#{count},version =#{version},freq =#{f

  • 问题内容: 这个问题已经被问过很多次了,但是我找不到任何好的答案,所以我会再问一次。 我有如下的父子单向关系: 我创建了一个父实例,为其分配了一个子实例列表,并尝试保留它: 代码运行时,出现以下异常: MySQLIntegrityConstraintViolationException:不能添加或更新子行,外键约束失败(。,约束 外键()参考()ON DELETE NO ACTION ON UPD

  • 问题内容: 我正在努力使数据库与Java程序进行通讯。 有人可以使用JDBC给我一个快速而肮脏的示例程序吗? 我收到一个非常严重的错误: 测试文件的内容: 问题答案: 所以,你有一个 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败 java.net.ConnectException:连接被拒绝 我引用了这个答案,其中还包

  • 我正在使用hibernate.cfg运行Spring MVC应用程序,如下所示: 当我在本地环境中运行应用程序时,它运行起来没有任何问题。但在服务器上,它给出的异常为: