user7352962
user7352962

Reputation: 25

unique constraint Exception (SQLIntegrityConstraintViolationException)

I am trying to insert data in oracle DB using spring JPA repositories

I have a hash map which contains all the values which needs to be populated into DB,I am Iterating each value and setting into my Entity class Basically I have a table which has a composite primary key(NotifiedToId).when I am setting the values ints throwing constraint violation exception.In my logs it is printing all correct values but its not getting inserted,

My Entity class:

@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;
}

}

Business Logic:

        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);

        }

Upvotes: 1

Views: 492

Answers (1)

StanislavL
StanislavL

Reputation: 57421

Try to add notificationlog = new TbBamiNotificationLog() in the beginning of your saving for. It could be possible when you try to save the second row but the instance is the same (with id provided during the first save).

Upvotes: 1

Related Questions