简体   繁体   English

外键作为主键注释

[英]Foreign key as primary key annotation

There is 2 entity 有2个实体
Product(product_id(PK),product_name..) 产品展示(产品(PK),这里给..)
Store(product_id(PK,FK),product_quantity,...) 商店(产品(PK,FK),product_quantity,...)

I need @OneToOne, so i do this 我需要@OneToOne,所以我这样做

public class Product{
  @Id
  @Column(name="PRODUCT_ID")
  @OneToOne(mappedBy="product")
  private int product_id;
  ....
}

public class Store{
  @Id
  @OneToOne(cascade = CascadeType.ALL)
  @JoinColumn(name = "PRODUCT_ID")
  private Product product;
}

Compilier says @Column(s) not allowed on a @OneToOne property: ......shopdb.entity.Product.product_id 编译器说@OneToOne属性上不允许使用@Column:...... shopdb.entity.Product.product_id

What is the problem? 问题是什么?

I just moved @OneToOne(mappedBy="product") to new created field in Product 我刚刚将@OneToOne(mappedBy =“ product”)移至Product中新创建的字段

private Store store; 私人商店;

I dont understand how it works correct; 我不明白它是如何工作的; Why i need to use 3 fields instead of 2? 为什么我需要使用3个字段而不是2个字段?

You can't mark a column defined with @JoinColumn as @Id . 您不能将使用@JoinColumn定义的列标记为@Id This is applicable only to columns defined with @Column annotation. 这仅适用于使用@Column批注定义的列。

It is also a bit strange what are you trying to do. 您尝试做什么也有些奇怪。 You are basically making the produc_id primary key for both entites, so then why don't make just one? 您基本上是将两个produc_id主键,那么为什么不只创建一个呢?

From what I can see here, I believe that the Store should have a store id property which will be used as primary key. 从这里可以看到,我认为Store应该具有商店ID属性,该属性将用作主键。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM