标题中简短描述了我的问题。谁能告诉我,如何使用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;

      ....
}

07-25 23:19