一个映射类Hibernate + JPA + Spring中有多个外键 [英] Multiple foreign keys in one mapping class Hibernate+JPA+Spring

查看:47
本文介绍了一个映射类Hibernate + JPA + Spring中有多个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个叫做客户"和电影"的类.我想要一个包含RentedMovie的类,该类可以容纳Client的外键和Book的外键,并且还具有自己的ID.到目前为止,我的课程看起来是一样的:

I have 2 classes called Client and Movie. 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. So far my classes look the same:

客户:

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

现在租用的类现在很正常:

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

我真正需要更改什么?我需要给Rented课上电影和客户吗?客户与书本之间是一对多的关系.一本书只能租用一次,客户可以租多本书.我是否必须将清单保留在某个地方,或者租用的班级看起来如何,在其他2个班级中还需要更改什么?

What do I really need to change? I need to give the Rented class a movie and a client? 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?

推荐答案

您的映射应如下所示(省略其他字段).始终考虑对象而不是实体字段.

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_in_An_To_An_To_An_To_Annotation /a>

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

这篇关于一个映射类Hibernate + JPA + Spring中有多个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆