我正在使用spring和hibernate创建一个Web应用程序.一个表中可能有多个@ManyToOne映射,它们引用另一表中的同一列?我的意思是
I am creating a web application using spring and hibernate. Is it possible to have multiple @ManyToOne mappings in one table that reference the same Column in another table? What I mean is
如果我有这两个表
@Entity @Table(name="AUTHOR") public class Author{ @Id @GeneratedValue @Column(name="AUTHOR_ID") private IntegerauthorId; @Column(name="AUTHOR_NAME") private String authorName; ... }和
@Entity @Table(name="BOOK") public class book{ @Id @GeneratedValue @Column(name="BOOK_ID") private Integer bookId; @Column(name="TITLE") private String title; @Column(name="PREFACE_AUTHOR") private Author prefaceAuthor; @Column(name="BOOK_AUTHOR") private Author bookAuthor; ... }如何进行映射,以便可以从AUTHOR表中同时填充PREFACE_AUTHOR和BOOK_AUTHOR? 一位作者既可以是序言作者,也可以是本书的作者,但每本书只能有一位序言作者和一位作者.
How do I do the mapping so that I can populate both PREFACE_AUTHOR and BOOK_AUTHOR from the AUTHOR table? One Author can be both the preface and book author but there can only be one preface author and one book author per book.
让我知道我是否可以澄清.
Let me know if there is anything I can clarify.
谢谢您的帮助!
/D
推荐答案您应指定它是多对一的,并使用@JoinColumn而不是@Column,如下所示:
You should specify that it is a many-to-one and use @JoinColumn instead of @Column, like this:
@ManyToOne @JoinColumn(name="PREFACE_AUTHOR") private Author prefaceAuthor; @ManyToOne @JoinColumn(name="BOOK_AUTHOR") private Author bookAuthor;更多推荐
将表中的两列映射到另一个表中的同一列
发布评论