简体   繁体   中英

Spring JPA - mapping foreign keys as a primary key [MySQL]

I'm having problems in mapping foreign keys as the primary key.

My tables are:

client:
PK: id_client

games:
PK: id_game

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

And here are the classes I have defined as entities:

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;
    }
}

I have tried all the advices I have found so far, but none of them helped. Also, I need this PK to be composed by foreign keys, so I really need help to solve out, how should I map it correctly.

You can use @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;

    ....
}

More info here : http://docs.oracle.com/javaee/6/api/javax/persistence/MapsId.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM