将表中的两列映射到另一个表中的同一列 [英] Mapping two columns in a table to the same column in another table

查看:120
本文介绍了将表中的两列映射到另一个表中的同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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_AUTHORBOOK_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;

这篇关于将表中的两列映射到另一个表中的同一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆