0

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]
5
  • There are probably 100+ questions on Stack Overflow covering that stack trace/error. Please edit your question and show only the 10-20 relevant lines of code. Commented Nov 18, 2018 at 9:09
  • i saw the realted errors but i didnot found the solution :( Commented Nov 18, 2018 at 9:11
  • Use annotation EmbeddedId TwoP twoP instead of Id in your LetterDoc class Commented Nov 18, 2018 at 9:45
  • no it is still showing me same error after changing to EmebeddedId Commented Nov 18, 2018 at 9:49
  • Cannot see all the relevant sources, where is Letter.java? Also, the use of "@Embeddable" as an "@Id" should be done through the use of "@EmbeddedId". Commented Nov 18, 2018 at 12:44

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

did you try both possibilities?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.