简体   繁体   English

如何在hibernate中使用作为另一个表的复合主键的外键的一部分作为主键?

[英]How to use part of foreign key which is composite primary key of another table as a primary key in hibernate?

I have 2 tables.. Table A contains composite primary key.我有 2 个表。表 A 包含复合主键。 I am using this key as a foreign key in another table.我将此键用作另一个表中的外键。 but in this table i need to have a composite primary key where one of the column i need to take it from the composite key of A table.但在这张表中,我需要有一个复合主键,其中一列我需要从 A 表的复合键中取出它。 I could not achieve this with mapsId as it is taking a whole CK.我无法使用 mapsId 实现这一点,因为它需要整个 CK。 is there anyway to achieve it?有没有办法实现它?

i just need hibernate way of doing like below:我只需要像下面这样的休眠方式:

I need exactly like this in hibernate 我在休眠中完全需要这样

Here is how you could map the entities corresponding to the database tables in the SO question you linked to:以下是如何映射与链接到的 SO 问题中的数据库表相对应的实体:

@Entity
public class Concert {
  @Id
  Integer id;

  String name;

  ...
}


@Embeddable
public class ConcertDetailsId {
  Date date;

  Integer concertId; // corresponds to PK type of Concert
}


@Entity
public class ConcertDetails {
  @EmbeddedId
  ConcertDetailsId id;

  @MapsId("concertId") // maps concertId attribute of embedded id
  @ManyToOne
  Concert concert;

  BigDecimal cost;

  ...
}

Is this how you tried to use @MapsId ?这是您尝试使用@MapsId吗? If so, what was the problem?如果是这样,问题是什么?

Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.派生身份在JPA 2.2 规范的 2.4.1 节中讨论(通过示例)。

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

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