我有两个名为Client和Movie的课程。我想要一个类RentedMovie,它将保存客户端的外键和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 +
'}';
}
}
现在正在租用的jsut正常的课程:
@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中我还需要改变什么?
答案 0 :(得分:0)
首先,您应该将Rented
课程中的clientId更改为private client Client
。
接下来,您必须为此字段添加注释@ManyToOne
。接下来你要对字段movieId做同样的事情。
当然,你必须改变结构,吸气剂和制定者。
答案 1 :(得分:0)
您的映射应如下所示(省略其他字段)。始终考虑对象而不是实体字段。
@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;
}
这是一个基本的双向一对多映射,您可以在这里阅读更多信息: