如何@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
. 我不知道为什么?