2014-10-28 373 views
0

我有3類Player(PK Id_player),Match(PK Id_match)和銘文(PK應該是Id_player和Id_match)。一個玩家有一個和唯一一個銘文(單人遊戲 - > 1個銘文),並且一個遊戲可以有許多銘文(1場比賽 - >許多比賽)。所以表格銘文有兩個外鍵:Id_player和Id_match,問題是我不知道如何告訴休眠,這兩個外鍵都必須是構成銘文表的組合主鍵。這是我的代碼::由兩個外鍵組成的映射主鍵?休眠

@Entity 

@Table(name = "Players") 

public class Player{ 

    @Id @GeneratedValue 
private Long Id_player; 

@OneToOne 
@JoinColumn(name = "payer_id") 
Inscription Inscription;} 

@Entity 
@Table(name="Matches") 
public class Match{ 

    @Id @GeneratedValue 
    private long Id_match; 

    @OneToMany 
    @JoinColumn(name = "id_match") 
    List<Inscription> inscriptions= new ArrayList<>(); 

我應該在題字課上寫什麼,以使兩個FK的組成PK。由於

回答

1

檢查:

球員

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 

@Entity 

@Table(name = "Players") 

public class Player { 

    @Id 
    @GeneratedValue 
    private Long Id_player; 

    @OneToOne 
    private Match match; 

} 

比賽

import java.util.List; 

import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.OneToMany; 
import javax.persistence.Table; 

@Entity 
@Table(name="Matches") 
public class Match{ 

    @Id @GeneratedValue 
    private long Id_match; 

    @OneToMany 
    @JoinTable(name="Inscription", 
    [email protected](name="MATCH_ID"), 
    [email protected](name="PLAYER_ID")) 
    private List<Player> playersList; 


    public long getId_match() { 
     return Id_match; 
    } 

    public void setId_match(long id_match) { 
     Id_match = id_match; 
    } 

    public List<Player> getPlayersList() { 
     return playersList; 
    } 

    public void setPlayersList(List<Player> playersList) { 
     this.playersList = playersList; 
    } 


} 
+0

問題解決了,比你的男人! – user3781233 2014-10-28 16:56:53

+0

不客氣。請標記爲已回答。 – jitsonfire 2014-10-28 17:06:46