简体   繁体   English

一个映射类Hibernate + JPA + Spring中有多个外键

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

I have 2 classes called Client and Movie. 我有2个叫做“客户”和“电影”的类。 I want to have a class RentedMovie that would hold foreign key of a Client and the foreign key of a Book and it would also have its own id. 我想拥有一个RentedMovie类,该类可以容纳Client的外键和Book的外键,并且还具有自己的ID。 So far my classes look the same: 到目前为止,我的课程看起来是一样的:

Client: 客户:

@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 + '\'' +
                '}';
    }
}

Movie: 电影:

@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 +
                '}';
    }
}

And the rented class which is jsut normal right now: 现在租来的类是正常的:

@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 +
                '}';
    }
}

What do I really need to change? 我真的需要更改什么? I need to give the Rented class a movie and a client? 我需要给Rented课上电影和客户吗? Between client and book it's a one to many relationship. 客户与书本之间是一对多的关系。 A book can be rented only once, a client can rent multiple books. 一本书只能租用一次,客户可以租多本书。 Do I have to keep lists somewhere or how would the rented class look like and what else do I have to change in the other 2? 我是否必须将清单保存在某个地方,或者租用的班级如何?在其他两个班级中还需要更改什么?

First you should change clientId in Rented class to private client Client . 首先,你应该改变的clientId Rentedprivate client Client Next you have to add annotation @ManyToOne for this field. 接下来,您必须为此字段添加注释@ManyToOne Next you have tt do the same with field movieId. 接下来,您对字段movieId进行tt相同的操作。 And of course you have to change contructors, getters and setters. 当然,您必须更改构造器,获取器和设置器。

Your mappings should look like the below (additional fields omitted). 您的映射应如下所示(省略其他字段)。 Always think in terms of objects and not entity fields. 始终考虑对象而不是实体字段。

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

This is a basic bi-directional one-to-many mapping which you can read more about here: 这是基本的双向一对多映射,您可以在此处了解更多信息:

https://en.wikibooks.org/wiki/Java_Persistence/OneToMany#Example_of_a_OneToMany_relationship_and_inverse_ManyToOne_annotations 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