簡體   English   中英

一個映射類Hibernate + JPA + Spring中有多個外鍵

[英]Multiple foreign keys in one mapping class Hibernate+JPA+Spring

我有2個叫做“客戶”和“電影”的類。 我想擁有一個RentedMovie類,該類可以容納Client的外鍵和Book的外鍵,並且還具有自己的ID。 到目前為止,我的課程看起來是一樣的:

客戶:

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

    @Id
    @Column(name="id", nullable= false)
    private Integer id;

    @Column(name="age", nullable = false)
    private Integer age;

    @Column(name="numberofrentals", nullable = false)
    private Integer numberofrentals;

    @Column(name="name", nullable = false)
    private String name;

    @Column(name="address", nullable = false)
    private String address;

    public Client() {
    }

    public Client(Integer id, Integer age, Integer numberofrentals, String name, String address) {

        this.id = id;
        this.age = age;
        this.numberofrentals = numberofrentals;
        this.name = name;
        this.address = address;
    }

    public Integer getId() {

        return id;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Integer getnumberofrentals() {
        return numberofrentals;
    }

    public void setnumberofrentals(int numberofrentals) {
        this.numberofrentals = numberofrentals;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Client client = (Client) o;

        if (age != client.age) return false;
        if (numberofrentals != client.numberofrentals) return false;
        if (!id.equals(client.id)) return false;
        if (!name.equals(client.name)) return false;
        return address.equals(client.address);

    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + age;
        result = 31 * result + numberofrentals;
        result = 31 * result + name.hashCode();
        result = 31 * result + address.hashCode();
        return result;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Client{" +
                "id=" + id +
                ", age=" + age +
                ", numberofrentals=" + numberofrentals +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

電影:

@Entity
@Table(name="movie")
public class Movie  implements Serializable {

    @Id
    @Column(name="id", nullable=false)
    private Integer id;

    @Column(name="name", nullable = false)
    private String name;

    @Column(name="numberofrentals", nullable=false)
    private Integer numberofrentals;

    @Column(name="director", nullable = false)
    private String director;

    @Column(name="year", nullable = false)
    private Integer year;

    public Movie() {
    }

    public Movie(Integer id, String name, Integer numberofrentals, String director, Integer year) {
        this.id = id;
        this.name = name;
        this.numberofrentals = numberofrentals;
        this.director = director;
        this.year = year;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDirector() {
        return director;
    }

    public void setDirector(String director) {
        this.director = director;
    }

    public Integer getYear() {
        return year;
    }

    public void setYear(Integer year) {
        this.year = year;
    }

    public Integer getNumberofrentals() {
        return numberofrentals;
    }

    public void setNumberofrentals(Integer numberofrentals) {
        this.numberofrentals = numberofrentals;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Movie movie = (Movie) o;

        if (!id.equals(movie.id)) return false;
        if (!name.equals(movie.name)) return false;
        if (!numberofrentals.equals(movie.numberofrentals)) return false;
        if (!director.equals(movie.director)) return false;
        return year.equals(movie.year);

    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + name.hashCode();
        result = 31 * result + numberofrentals.hashCode();
        result = 31 * result + director.hashCode();
        result = 31 * result + year.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Movie{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", numberofrentals=" + numberofrentals +
                ", director='" + director + '\'' +
                ", year=" + year +
                '}';
    }
}

現在租來的類是正常的:

@Entity
@Table(name="rented")
public class Rented implements Serializable {
    @Id
    @Column(name="id", nullable= false)
    private Integer id;

    @Column(name="movieID", nullable = false)
    private Integer movieID;

    @Column(name="clientID", nullable = false)
    private Integer clientID;

    public Rented(Integer id, Integer movieID, Integer clientID) {
        this.id = id;
        this.movieID = movieID;
        this.clientID = clientID;
    }

    public Rented() {
    }

    public Integer getId() {
        return id;
    }

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

    public Integer getMovieID() {
        return movieID;
    }

    public void setMovieID(Integer movieID) {
        this.movieID = movieID;
    }

    public Integer getClientID() {
        return clientID;
    }

    public void setClientID(Integer clientID) {
        this.clientID = clientID;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Rented rented = (Rented) o;

        if (!id.equals(rented.id)) return false;
        if (!movieID.equals(rented.movieID)) return false;
        return clientID.equals(rented.clientID);

    }

    @Override
    public int hashCode() {
        int result = id.hashCode();
        result = 31 * result + movieID.hashCode();
        result = 31 * result + clientID.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "Rented{" +
                "id=" + id +
                ", movieID=" + movieID +
                ", clientID=" + clientID +
                '}';
    }
}

我真的需要更改什么? 我需要給Rented課上電影和客戶嗎? 客戶與書本之間是一對多的關系。 一本書只能租用一次,客戶可以租多本書。 我是否必須將清單保存在某個地方,或者租用的班級如何?在其他兩個班級中還需要更改什么?

首先,你應該改變的clientId Rentedprivate client Client 接下來,您必須為此字段添加注釋@ManyToOne 接下來,您對字段movieId進行tt相同的操作。 當然,您必須更改構造器,獲取器和設置器。

您的映射應如下所示(省略其他字段)。 始終考慮對象而不是實體字段。

@Entity
@Table(name="rental")
public class Rental implements Serializable {

    @Id
    @Column(name="id", nullable= false)
    private Integer id;

    //movie associated with this rental
    @ManyToOne
    @JoinColumn(name="movieID", nullable = false)
    private Movie movie;

    //client associated with this rental
    @ManyToOne
    @JoinColumn(name="clientID", nullable = false)
    private Client client;
}

@Entity
@Table(name="movie")
public class Movie  implements Serializable {

    //collection of rentals for this movie
    @OneToMany(mappedBy = "movie")
    private Set<Rental> rentals;
}

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

    //collection of rentals for this client
    @OneToMany(mappedBy = "client")
    private Set<Rental> rentals;
}

這是基本的雙向一對多映射,您可以在此處了解更多信息:

https://zh.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_and_inverse_ManyToOne_annotations

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM