我遇到了标题中简短描述的问题。谁能告诉我,如何使用 JPA 获得相同的数据库?
create table ALBUM
(
IdAlbum int,
AlbumName varchar(35) not null,
UrlOfAlbum varchar(60) not null,
Primary Key(IdAlbum)
);
create table ARTIST
(
IdArtist int,
ArtistName varchar(35) not null,
Primary Key(IdArtist)
);
create table TRACK
(
IdTrack int,
IdAlbum int,
IdArtist int,
TrackName varchar(35) not null,
Primary Key(IdTrack, IdAlbum),
Foreign Key(IdAlbum) references Album(IdAlbum),
Foreign Key(IdArtist) references Artist(IdArtist)
);
最佳答案
只需将列放入嵌入键中并在主类中保留关系:
@Embeddable
class TrackId
{
private Integer idAlbum;
private Integer idTrack;
// getters, setters, equals and hashCode
}
@Entity
class Track
{
@EmbeddedId
TrackId trackId;
@ManyToOne
@MapsId("idAlbum")
@JoinColumn(name = "idAlbum", referencedColumnName = "idAlbum")
private Album album = null;
....
}
关于java - JPA、 hibernate 、Java。复合主键,其中之一也是外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40064122/