简体   繁体   中英

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. 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. I could not achieve this with mapsId as it is taking a whole 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:

@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 ? If so, what was the problem?

Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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