唯一约束异常(SQLIntegrityConstraintViolationException)

时间:2017-04-19 18:00:19

标签: java spring jpa

我正在尝试使用spring JPA存储库

在oracle DB中插入数据

我有一个哈希映射,其中包含需要填充到DB中的所有值,我正在迭代每个值并设置到我的Entity类中 基本上我有一个具有复合主键(NotifiedToId)的表。当我设置值ints抛出约束违规异常。在我的日志中它打印所有正确的值,但它没有插入,

我的实体课程:

@Embeddable
public class TbBamiNotifUserLogPK implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;

@Column(name="NOTIF_REF_NO")
private String notifRefNo;

@Column(name="NOTIFIED_TO_ID")
private String notifiedToId;

public TbBamiNotifUserLogPK() {
}
public String getNotifRefNo() {
    return this.notifRefNo;
}
public void setNotifRefNo(String notifRefNo) {
    this.notifRefNo = notifRefNo;
}
public String getNotifiedToId() {
    return this.notifiedToId;
}
public void setNotifiedToId(String notifiedToId) {
    this.notifiedToId = notifiedToId;
}

public boolean equals(Object other) {
    if (this == other) {
        return true;
    }
    if (!(other instanceof TbBamiNotifUserLogPK)) {
        return false;
    }
    TbBamiNotifUserLogPK castOther = (TbBamiNotifUserLogPK)other;
    return 
        this.notifRefNo.equals(castOther.notifRefNo)
        && this.notifiedToId.equals(castOther.notifiedToId);
}

public int hashCode() {
    final int prime = 31;
    int hash = 17;
    hash = hash * prime + this.notifRefNo.hashCode();
    hash = hash * prime + this.notifiedToId.hashCode();

    return hash;
}

}

import java.io.Serializable;
import javax.persistence.*;
@Entity
@Table(name="TB_BAMI_NOTIF_USER_LOG")
@NamedQuery(name="TbBamiNotifUserLog.findAll", query="SELECT t FROM      TbBamiNotifUserLog t")
public class TbBamiNotifUserLog implements Serializable {
private static final long serialVersionUID = 1L;

@EmbeddedId
private TbBamiNotifUserLogPK id;

@Column(name="NOTIF_CAT")
private String notifCat;

@Column(name="NOTIFIED_TO_NAME")
private String notifiedToName;

@Column(name="NOTIFIED_TO_ROLE")
private String notifiedToRole;

public TbBamiNotifUserLog() {
}

public TbBamiNotifUserLogPK getId() {
    return this.id;
}

public void setId(TbBamiNotifUserLogPK id) {
    this.id = id;
}

public String getNotifCat() {
    return this.notifCat;
}

public void setNotifCat(String notifCat) {
    this.notifCat = notifCat;
}

public String getNotifiedToName() {
    return this.notifiedToName;
}

public void setNotifiedToName(String notifiedToName) {
    this.notifiedToName = notifiedToName;
}

public String getNotifiedToRole() {
    return this.notifiedToRole;
}

public void setNotifiedToRole(String notifiedToRole) {
    this.notifiedToRole = notifiedToRole;
}

}

@Entity
@Table(name="TB_BAMI_NOTIFICATION_LOG")
@NamedQuery(name="TbBamiNotificationLog.findAll", query="SELECT t FROM   TbBamiNotificationLog t")
public class TbBamiNotificationLog implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="NOTIF_REF_NO")
private String notifRefNo;

@Column(name="\"ACTION\"")
private String action;

@Column(name="NOTIF_CONTENT")
private String notifContent;

@Column(name="NOTIF_TYPE")
private String notifType;

@Column(name="NOTIFIED_DATE_TIME")
private Timestamp notifiedDateTime;

private String refno;

@Column(name="\"VERSION\"")
private BigDecimal version;

public TbBamiNotificationLog() {
}

public String getNotifRefNo() {
    return this.notifRefNo;
}

public void setNotifRefNo(String notifRefNo) {
    this.notifRefNo = notifRefNo;
}

public String getAction() {
    return this.action;
}

public void setAction(String action) {
    this.action = action;
}

public String getNotifContent() {
    return this.notifContent;
}

public void setNotifContent(String notifContent) {
    this.notifContent = notifContent;
}

public String getNotifType() {
    return this.notifType;
}

public void setNotifType(String notifType) {
    this.notifType = notifType;
}

public Timestamp getNotifiedDateTime() {
    return this.notifiedDateTime;
}

public void setNotifiedDateTime(Timestamp notifiedDateTime) {
    this.notifiedDateTime = notifiedDateTime;
}

public String getRefno() {
    return this.refno;
}

public void setRefno(String refno) {
    this.refno = refno;
}

public BigDecimal getVersion() {
    return this.version;
}

public void setVersion(BigDecimal version) {
    this.version = version;
}

}

业务逻辑:

        for (Entry<String, PushNotificationDetails> entry : finalTemplate.entrySet()){
        try{    
            notificationlog.setNotifRefNo("121323");
            notificationlog.setRefno(refNum);
            notificationlog.setVersion(new BigDecimal(1));
            notificationlog.setAction(action);
            LOGGER.debug("TEMPLATE TYPE"+entry.getValue().getTemplate_type());
            LOGGER.debug("TEMPLATE"+entry.getValue().getTemplate());
            notificationlog.setNotifType(entry.getValue().getTemplate_type());
            notificationlog.setNotifContent(entry.getValue().getTemplate());
            notificationlog.setNotifiedDateTime(notifiedDateTime);
            tbBamiNotifyLogRepository.save(notificationlog);

            LOGGER.debug("inside if block ::: ");
            LOGGER.debug("USERID is: "+entry.getValue().getUserID());
            LOGGER.debug("NOTIFCAT is: "+entry.getValue().getNotif_cat());
            LOGGER.debug("NOTIFUSER is: "+entry.getValue().getUserName());
            LOGGER.debug("NOTIFROLE is: "+entry.getKey());
            tbBamiNotifUserLogPK.setNotifiedToId(entry.getValue().getUserID());
            tbBamiNotifUserLogPK.setNotifRefNo("121323");
            tbBamiNotifUserLog.setId(tbBamiNotifUserLogPK);

            LOGGER.debug("GET is: "+tbBamiNotifUserLog.getId().getNotifiedToId());
            tbBamiNotifUserLog.setNotifCat(entry.getValue().getNotif_cat());
            tbBamiNotifUserLog.setNotifiedToName(entry.getValue().getUserName());
            tbBamiNotifUserLog.setNotifiedToRole(entry.getKey());
            tbBamiNotifyUserLogRepository.save(tbBamiNotifUserLog);

        }

1 个答案:

答案 0 :(得分:1)

尝试在保存开始时添加notificationlog = new TbBamiNotificationLog()。当您尝试保存第二行但实例是相同的时(第一次保存期间提供了id),这是可能的。