映射主键由两个外键组成?过冬

时间:2014-10-28 16:05:30

标签: java hibernate foreign-keys primary-key

我有3个类玩家(PK Id_player),匹配(PK Id_match)和铭文(PK应该是Id_player和Id_match)。玩家只有一个且只有一个铭文(1个玩家 - > 1个铭文),并且匹配可以在许多铭文中(1个匹配 - >许多文字)。所以表格铭文有2个外键IDI_player和Id_match,问题是我不知道如何告诉hibernate这两个外键必须是铭文表的组合原型键。这是我的代码::

@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<>();

我应该在Inscription类上写些什么来使Fk成为一个组合PK。感谢

1 个答案:

答案 0 :(得分: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",
    joinColumns=@JoinColumn(name="MATCH_ID"),
    inverseJoinColumns=@JoinColumn(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;
    }


}