AnnotationException: A Foreign key refering has the wrong number of column. should be 2

前端 未结 2 667

I am mapping my classes with the tables of my database, but when running a test, I get the following error:

Caused by: org.hibernate.AnnotationException: A F         


        
相关标签:
2条回答
  • 2020-12-14 17:17

    As of Java 8 (introducing @Repeatable), the wrapper annotation @JoinColumns is no longer required .

    So, you can simply do this:

    // bi-directional many-to-one association to Provincia
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "codProvincia", insertable = false, updatable = false)
    @JoinColumn(name = "codRegion", insertable = false, updatable = false)
    private Provincia provincia;
    

    References:

    Java 8's new Type Annotations

    Repeating Annotations

    0 讨论(0)
  • 2020-12-14 17:41

    Your class CiudadPK has two columns in it. You're only using @JoinColumn which is limited to a single column. You need to use @JoinColumns and list out both columns in the FK, e.g.

    // bi-directional many-to-one association to Provincia
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumns({
      @JoinColumn(name = "codProvincia", insertable = false, updatable = false),
      @JoinColumn(name = "codRegion", insertable = false, updatable = false)
    })
    private Provincia provincia;
    

    You'll likely have the other issue w/ Ciudad as well, please follow the pattern here to correct that one.

    0 讨论(0)
提交回复
热议问题