Spring JPA - 将外键映射为主键[MySQL]

时间:2016-08-26 10:59:44

标签: java mysql spring jpa entity

我在将外键映射为主键方面遇到了问题。

我的表是:

client:
PK: id_client

games:
PK: id_game

tickets:
PK: (id_game_fk references game(id_game), id_client_fk references client(id_client))

以下是我定义为实体的类:

Client.java:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "client")
public class Client {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id_client")
    private int id;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Games.java:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "games")
public class Games {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id_game")
    private int id;

        public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }
}

Ticket.java:

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import Client;
import Games;

@Entity
@Table(name = "tickets")
public class Ticket implements Serializable {
    private static final long serialVersionUID = 3287868602749718327L;

    @EmbeddedId
    private TicketId ticketId;

    @ManyToOne
    @JoinColumn(name = "id_game")
    private Games games;

    @ManyToOne
    @JoinColumn(name = "id_client")
    private Client client;

    public TicketId getId() {
        return ticketId;
    }

    public void setId(TicketId id) {
        this.ticketId = id;
    }

    public Games getGames() {
        return games;
    }

    public void setGames(Games games) {
        this.games = games;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }
}

TicketId.java:

import java.io.Serializable;

import javax.persistence.Embeddable;

@Embeddable
public class TicketId implements Serializable {
    private static final long serialVersionUID = 6220676431741410239L;

    private int idGameFk;
    private int idClientFk;

    public TicketId(int idGameFk, int idClientFk) {
        this.idGameFk = idGameFk;
        this.idClientFk = idClientFk;
    }

    public int getIdGameFk() {
        return idGameFk;
    }

    public void setIdGameFk(int idGameFk) {
        this.idGameFk = idGameFk;
    }

    public int getIdClientFk() {
        return idClientFk;
    }

    public void setIdClientFk(int idClientFk) {
        this.idClientFk = idClientFk;
    }
}

我已经尝试了到目前为止我找到的所有建议,但没有一个帮助过。另外,我需要这个PK由外键组成,所以我真的需要帮助解决,我应该如何正确映射。

1 个答案:

答案 0 :(得分:1)

您可以使用@MapsId:

@Entity
@Table(name = "tickets")
public class Ticket implements Serializable {
    private static final long serialVersionUID = 3287868602749718327L;

    @EmbeddedId
    private TicketId ticketId;

    @ManyToOne
    @MapsId("idGameFk")
    @JoinColumn(name = "id_game_fk")
    private Games games;

    @ManyToOne
    @MapsId("idClientFk")
    @JoinColumn(name = "id_client_fk")
    private Client client;

    ....
}

此处有更多信息:http://docs.oracle.com/javaee/6/api/javax/persistence/MapsId.html