Constraint Violation when persisting One To Many relation

前端 未结 2 628
滥情空心
滥情空心 2020-12-07 04:12

In a spring mvc application using hibernate and MySQL, I am getting the following constraint violation exception:

Caused by: com.mysql.jdbc.exceptions.jdbc         


        
2条回答
  •  渐次进展
    2020-12-07 04:58

    Change this:

    @OneToMany(mappedBy = "providertype")
    private Set documententities;
    

    To this:

    @OneToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "Link_Documents", joinColumns = {@JoinColumn(name = "codePk", unique = true)}, inverseJoinColumns = {@JoinColumn(name = "change_this_with_primary_key_variable_name_from_HL7DocumentEntity")})
     private Set documententities;
    

    And in HL7DocumentEntity change as follows:

    This

    @ManyToOne
        @JoinColumns({ @JoinColumn(name = "ptcode", referencedColumnName = "code"),
            @JoinColumn(name = "ptcodesystem", referencedColumnName = "codesystem")
        })
        private HL7GeneralCode providertype;
    

    Change to this:

    @ManyToOne(fetch = FetchType.LAZY)
      @JoinTable(name = "Link_Documents", joinColumns = {@JoinColumn(name = "change_this_with_primary_key_variable_name_from_HL7DocumentEntity")}, inverseJoinColumns = {@JoinColumn(name = "codePk")})
      private HL7GeneralCode providertype;
    

    I think you have to change "change_this_with_primary_key_variable_name_from_HL7DocumentEntity" with "id" like it is in BaseEntity but take a look at your sql table, you willsee there the correct name.

    I hope you notice How I told JPA to use the same "Link_Documents" table for linking the 2 tables. I think this is were your mistake is. Just make sure to change where I told you with the correct variable name and I think it should work

提交回复
热议问题