0

如何@ManyToOne在 Hibernate 中使用非主键映射关联?我可以使用@NaturalId注释,但仍然有错误:

A Foreign key refering POJO.Question from POJO.Answer has the wrong number of column. should be 4

问题类(具有多个主键):

    @Entity
    @Table(name = "[dbo].[Question]")
    public class Question implements Serializable {

        @Column(name = "[Id]", unique = true, nullable = false) 
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @NaturalId
        private Integer id;

        @EmbeddedId
        private QuestionEmbeddable questionEmbeddable;
}

首要的关键

    public class QuestionEmbeddable implements Serializable{

        @Column(name = "[Development Template Id]")
        private int templateId;

        //Sekcja w templejcie
        @Column(name = "[Section]")
        private Integer section;

        //Numer w sekcji
        @Column(name = "[Number]")
        private Integer number;

        //Wersja 
        @Column(name = "[Version]")
        private Integer version;
}

答题类:

    @Entity
    @Table(name = "[dbo].[Answer]")
    public class Answer implements Serializable {

        @Id
        @Column(name = "[Id]", unique = true, nullable = false) 
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Integer id;

        @JoinColumn(name = "[Question Id]")
        @ManyToOne(fetch=FetchType.LAZY)
        private Question question;

看起来该关联仍然是 to@EmbeddedId而不是@Naturalid. 我不知道为什么?

4

1 回答 1

0

这就是答案,必须使用referencedColumnName

@ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(
        name = "[Question Id]", referencedColumnName = "[Id]"
    )
    private Question question;
于 2020-03-20T21:01:21.763 回答