ashwin karki
ashwin karki

Reputation: 673

MySQL Error "Duplicate entry ' ' for key " while inserting the data in mysql

My model classes i have:

LetterDoc.java

@Entity
@Table(name = "letter_doc")
public class LetterDoc implements Serializable {


    private static final long serialVersionUID = 1L;

    @Id
    TwoP twoP;


    private String docFile;

    //i ommited getters and setters
    public LetterDoc() {

    }

Document.java

@Entity
@Table(name = "document")
public class Document {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long docId;

    private String docName;

    @Transient
    private boolean checked=false;

    public Document() {

    }

TwoP.java

@Embeddable
public class TwoP implements Serializable {

    @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
    @JoinColumn(name = "letterNo",unique=true)
    @JsonIgnore
    private Letter letter;

    @ManyToOne(fetch = FetchType.EAGER, optional = false,cascade=CascadeType.PERSIST)
    @JoinColumn(name = "documentId",unique=true)
    @JsonIgnore
    private Document document;

    public TwoP(Letter letter, Document document) {

        this.letter = letter;
        this.document = document;
    }

    public TwoP() {

    }


}

The json data that i coming in my api send from client side is:

enter image description here

In my api i have used this method to consume the json data:

@PostMapping(value = "/letter/create", produces = MediaType.APPLICATION_JSON_VALUE)
    public SLDto postAllOne(@RequestBody SLDto sldto) {

Letter letter = letterRepository.save(new Letter(clkLetter, sldto.getLetterDto().getInOut(),
                sldto.getLetterDto().getInOutNo(), sldto.getLetterDto().getInOutDate(),
                sldto.getLetterDto().getLetterIssuedSubBy(), sldto.getLetterDto().getLetterFile(),
                sldto.getLetterDto().getRepresentativeName(), selection, assessment));
    //Please consider letter is inserted with Id no 22.

            for (DocumentDto documentDto : sldto.getDocumentDto()) {
                TwoP twoP = null;
                LetterDoc letterDoc = null;

                System.out.println("loopedonce");
                Document document = null;
                System.out.println("total documents Id is" + documentDto.getDocId());
                document = documentRepository.findById((long) documentDto.getDocId()).get();
                System.out.println("Document Id from db is" + document.getDocId());
                twoP = new TwoP(letter, document);
                letterDoc = new LetterDoc();
                letterDoc.setTwoP(twoP);
                letterDoc.setDocFile(null);
                letterDocRepository.save(letterDoc);
                     } 
}

SlDto.java class

private LetterDto letterDto;
    private List<DocumentDto> documentDto;
    private List<SelectionCustomOfficeDto> selectionCustomOfficeDto;

Now in the For Loop i used above,it must be looped twice as the number of Json data coming is Two.Yes it is looping twice and in this line document = documentRepository.findById((long) documentDto.getDocId()).get(); ,I am getting Document object twice but when inserting it in letterDocRepository.save(letterDoc); ,it shows me

Duplicate entry '22' for key 'UK_5pahc9x5lwh5k4owpm3vjfq3c'

'22' is recent ID created when i inserted in "letter" table

Since my documentDto.getDocId() returns me the 1,2 coming from JSON data and though they are different but the program is showing me error.

To see also what is happening I debugged them to see whether the data is arriving from database as:

enter image description here

Error is :

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '22' for key 'UK_5pahc9x5lwh5k4owpm3vjfq3c'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_161]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_161]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_161]
    at java.lang.reflect.Constructor.newInstance(Unknown Source) ~[na:1.8.0_161]
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.Util.getInstance(Util.java:408) ~[mysql-connector-java-5.1.47.jar:5.1.47]
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936) ~[mysql-connector-java-5.1.47.jar:5.1.47]

Upvotes: 0

Views: 81

Answers (1)

stacker
stacker

Reputation: 4475

I think your problem is from Cascade property behavior, with Persist it will try to insert it when you save letterDoc object. try to use cascade.MERGE instead.

if you want to have Persist, don't insert any association you declared with cascade.persist as Hibernate will do that for you.

Upvotes: 1

Related Questions