【发布时间】:2021-01-27 21:46:19
【问题描述】:
我正在使用图表为一个项目创建课程以用于练习目的,直到我偶然发现了这个order_items:
创建Orders 或Products 之类的实体没有问题,因为我知道对于订单我只需要执行以下操作:
@Entity
@Table(name = "orders")
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "order_id")
private Integer orderId;
// rest of the code...
}
对于Products 类似:
@Entity
@Table(name = "products")
public class Products {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "product_id")
private Integer productId;
// rest of the code...
}
但是表order_items 有变量order_id 和item_id,这算作复合键吗?如果是这样,这些变量在我的 OrderItems 类中应该如何显示?
@Entity
@Table(name = "order_items")
public class OrderItems {
@Column(name = "order_id")
private Integer orderId;
@Column(name = "item_id")
private Integer itemId;
// rest of the code...
}
我检查了不同的问题,他们提到使用@IdClass 或@EmbeddableId 作为复合键,但我想先确认在这种情况下我是否应该这样做,除非不是这样,也许还有更多方法。
非常感谢您提供与此相关的意见和/或任何文章,感谢您抽出宝贵时间。
【问题讨论】:
标签: java spring-boot orm hibernate-mapping