【问题标题】:MySQL Error "Duplicate entry ' ' for key " while inserting the data in mysql将数据插入 mysql 时,MySQL 错误“重复条目 '' for key”
【发布时间】:2018-11-18 09:06:27
【问题描述】:

我的模型类:

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() {

    }

文档.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 Loop 中,它必须循​​环两次,因为传入的 Json 数据的数量是两次。是的,它循环了两次,在这一行 document = documentRepository.findById((long) documentDto.getDocId()).get(); 中,我得到了文档对象两次,但在 letterDocRepository.save(letterDoc); 中插入它时,它显示了我

密钥“UK_5pahc9x5lwh5k4owpm3vjfq3c”的重复条目“22”

'22' 是我在“字母”表中插入时创建的最新 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]

【问题讨论】:

  • 关于堆栈溢出的问题可能有 100 多个,涉及堆栈跟踪/错误。请编辑您的问题并仅显示 10-20 行相关的代码。
  • 我看到了相关错误,但我没有找到解决方案:(
  • 在您的 LetterDoc 类中使用注释 EmbeddedId TwoP twoP 而不是 Id
  • 不,更改为 EmebeddedId 后仍然显示相同的错误
  • 看不到所有相关来源,Letter.java在哪里?此外,应通过使用“@EmbeddedId”来使用“@Embeddable”作为“@Id”。

标签: java mysql spring hibernate jpa


【解决方案1】:

我认为您的问题来自Cascade 属性行为,Persist 它会在您保存letterDoc 对象时尝试插入它。 尝试改用cascade.MERGE。

如果您想拥有 Persist,请不要插入您使用 cascade.persist 声明的任何关联,因为 Hibernate 会为您执行此操作。

【讨论】:

  • 两种可能性都试过了吗?
猜你喜欢
  • 2010-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多