将表中的两列映射到另一个表中的同一列

编程入门 行业动态 更新时间:2024-10-11 03:18:22
本文介绍了将表中的两列映射到另一个表中的同一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

将表中的两列映射到另一个表中的同一列

本文发布于:2023-07-09 21:13:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1088229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!