简体   繁体   English

如何将我的主键作为外键链接到另一个表?

[英]How to link my primary key to to another table as a foreign key?

I am making a small project with a DB of a "book_store".我正在制作一个带有“book_store”数据库的小项目。 I am planning to have a two tables :我打算有两个表:

Books -> table(It consists of id as a Primary Key, and genre_id as a Foreign_key of genre). Books -> table(它由id作为主键, genre_id作为流派的 Foreign_key 组成)。 Genre table will have multiple genres(comedy, horror with its unique id) Genre -> table(id as a Primary Key, and yes this column will link to Books table as a primary Key) Here is the implementation of Book table流派表将有多种流派(喜剧,具有唯一 id 的恐怖片)流派 -> 表(id​​ 作为主键,是的,此列将链接到Books表作为主键)这是Book table的实现

@Entity
@Table(name = "books")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private String author;

    @JoinColumn(name = "genre_id", unique = true)
    @OneToOne(cascade = CascadeType.ALL)
    private Genre genre;

And here is my Genres table这是我的Genres

@Entity
@Table(name = "genres")
public class Genre {
    @Id
    @Column(name = "genre_id")
    private int id;
    private String genre;

So result of Book table i am getting is like this: I am using PostgresQL所以我得到的Book表的结果是这样的:我正在使用 PostgresQL

and table of Genre GenreGenre

enter image description here在此处输入图像描述

So my question is like this , while i am adding a new book ,and putting a genre 2 -> that means comedy, the Error is occured , that is telling that repeating of Unique key genres_pkey ?所以我的问题是这样的,当我添加一本新书并放入类型 2 -> 这意味着喜剧时,发生了Error is occured , that is telling that repeating of Unique key genres_pkey Thanks for feedback!感谢您的反馈!

One to one means that a Book has a single genre and that a single genre can only be used for a book.一对一意味着一本书只有一个流派,一个流派只能用于一本书。 You have also specified unique=true , meaning that in the column genre_id you cannot have the same id twice.您还指定了unique=true ,这意味着在列genre_id您不能有两次相同的 id。

Your description is not clear but I'm assuming multiple books can have the same genre.您的描述不清楚,但我假设多本书可以具有相同的类型。

The mapping in this case changes to a many-to-one:这种情况下的映射变为多对一:

@Entity
@Table(name = "books")
public class Book {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;
    private String author;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "genre_id")
    private Genre genre;
}

It also seems weird there books cannot have multiple genres (what about horror comedy stories?).书不能有多种类型似乎也很奇怪(恐怖喜剧故事呢?)。 In that case you will need a bidirectional one-to-many .在这种情况下,您将需要一个双向的一对多

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户 ID 主键链接到另一个表中的 user_id 作为外键? - How can I make User ID primary key link to user_id in another table as a foreign key? JPA :一张表带主键,另一张表带主键和外键 - JPA : one table with primary key and another table with primary and foreign key 如何在hibernate中使用作为另一个表的复合主键的外键的一部分作为主键? - How to use part of foreign key which is composite primary key of another table as a primary key in hibernate? 如何处理一个表的外键到多个表主键的映射? - How to handle mapping of a foreign key of a table to multiple tables primary key? 如何将主键的 ID 填充到外键 - How to populate Id of Primary Key to Foreign Key 如何添加到 INSERT INTO PRIMARY KEY 和 FOREIGN KEY - How to add to INSERT INTO PRIMARY KEY AND FOREIGN KEY 在SQL Server中将另一个键映射为外键而不是主键 - Mapping Another key as foreign key instead of primary key in sql server 主键也是外键的表上的复合键? - Composite key on table where primary key is also foreign key? 在邮递员中发送有效载荷时,是否有办法从另一个表的主键插入外键值的值? - Is there a way to insert the value of the foreign key value from the primary key of another table when sending a payload in postman? org.hibernate.MappingException:复合主键作为另一个表中的外键 - org.hibernate.MappingException: Composite primary key as a foreign key in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM