我的模型类:
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() {
}
}
我从客户端发送到 api 的 json 数据是:
在我的 api 中,我使用此方法来使用 json 数据:
@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类
private LetterDto letterDto;
private List<DocumentDto> documentDto;
private List<SelectionCustomOfficeDto> selectionCustomOfficeDto;
现在在我上面使用的For循环中,它必须循环两次,因为传入的Json数据数量是Two。是的,它循环两次,在这一行中document = documentRepository. findById((long) documentDto.getDocId()).get();
,我两次获取 Document 对象,但是当将其插入 letterDocRepository.save(letterDoc); 时,它显示了我
Duplicate entry '22' for key 'UK_5pahc9x5lwh5k4owpm3vjfq3c'
“22”是我插入“letter”表时创建的最新 ID
由于我的 documentDto.getDocId() 返回来自 JSON 数据的 1,2,尽管它们不同,但程序向我显示错误。
为了了解发生了什么,我调试了它们以查看数据是否从数据库到达:
错误是:
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]
最佳答案
我认为您的问题来自于Cascade
属性行为,使用Persist
它会在您保存letterDoc
对象时尝试插入它。
尝试改用cascade.MERGE
。
如果您想要 Persist,请不要插入您使用 cascade.persist
声明的任何关联,因为 Hibernate 会为您执行此操作。
关于java - MySQL 插入数据时出现错误 "Duplicate entry ' ' for key ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53359315/