Constraint Violation Exception In Hibernate

时间:2018-06-04 17:25:01

标签: java hibernate

I am getting constraint violation exception on specific primary key when committing.

I have a base table which stores logs. Also I have a IP Table which stores IP details of log. This IP table have one to many relationship to log table.

When I commit first time it works perfectly. And then re-ran my code check the integrity of constraints. It works well if I omit the record which has IP table ID of below. If I try to insert the record which has this IP ID (UUID Calculated By IP Address Value to ensure unique IP is inserted) I'm getting constraint violation exception.

73B0AFE5-A54D-3130-868E-A5D2040A8A20 (UUID of specific IP)


Here error thrown when executing above record

ERROR : Duplicate entry 'UUID bin val' for key 1

ExceptionMapperStandardImpl - HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]

Here is my base table implementatation

@Entity
@Table(name = "LOG")
public class LOG_Y18 implements LogEntity {
    private UUID logId;
    private Date LOG_TIME;
    private String URL;
    private LOGIP logip;

    public LOG_Y18() {
        this.logId = UUID.randomUUID();
    }

    public LOG_Y18(Date LOG_TIME, String URL, LOGIP logip) {
        this.logId = UUID.randomUUID();
        this.LOG_TIME = LOG_TIME;
        this.URL = URL;
        this.logip = logip;
    }

    @Id
    @Column(name = "LOGID", columnDefinition = "BINARY(16)")
    public UUID getLogId() {
        return logId;
    }

    public void setLogId(UUID logId) {
        this.logId = logId;
    }

    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "LOG_TIME")
    public Date getLOG_TIME() {
        return LOG_TIME;
    }

    public void setLOG_TIME(Date LOG_TIME) {
        this.LOG_TIME = LOG_TIME;
    }

    @Basic
    @Column(name = "URL", columnDefinition = "TEXT")
    public String getURL() {
        return URL;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }

    @ManyToOne(cascade = CascadeType.ALL)
    public LOGIP getLogip() {
        return logip;
    }

    public void setLogip(LOGIP logip) {
        this.logip = logip;
    }

}

Here is my one to many relationship to above entity

@Entity
@Table(name = "LOG_IP", indexes = @Index(columnList = "IP"))
public class LOGIP {
    private UUID ID;
    private Long IP;
    private String COUNTRY;

    public LOGIP() {
    }

    public LOGIP(Long IP, String COUNTRY) {
        this.ID = UUID.nameUUIDFromBytes(String.valueOf(IP).getBytes());
        this.IP = IP;
        this.COUNTRY = COUNTRY;
    }

    @Id
    @Column(name = "ID", columnDefinition = "BINARY(16)", nullable = false)
    public UUID getID() {
        return ID;
    }

    public void setID(UUID ID) {
        this.ID = ID;
    }

    @Basic
    @Column(name = "IP")
    public Long getIP() {
        return IP;
    }

    public void setIP(Long IP) {
        this.IP = IP;
    }

    @Basic
    @Column(name = "COUNTRY")
    public String getCOUNTRY() {
        return COUNTRY;
    }

    public void setCOUNTRY(String COUNTRY) {
        this.COUNTRY = COUNTRY;
    }

}

0 个答案:

没有答案