我遇到的情况是,一部电影可以有很多评论,但评论仅与一部电影有关。我遇到了如下所述的问题,但是由于我现在尝试了许多排列,所以实际上我有点困惑,因此在我弄乱代码之前,我想最好在这里问一下。
问:应如何设置?保存评论之前,我可以在评论中硬性地将movie_id设置为int吗?还是需要将MovieDTO电影对象作为ReviewDTO类中的对象之一?当我创建要保存的审阅对象时,我将其称为review.setMovie(someMovieObject)?
所以这是我的代码中发生的错误:
首先,它抱怨没有设置非空的影片引用(Hibernate:not-null属性引用的是null或瞬态值),因此在无法修复它之后,有人建议删除约束,以查看约束是否可行。
现在,它抱怨“ java.lang.Integer无法转换为java.lang.String”,但是我对哪里存在类型不匹配感到困惑? Class Hibernate或Hibernate DB?
很困惑,有人可以说...吗?提前致谢。
package edu.unsw.comp9321.jdbc;
public class ReviewDTO {
private int id;
private String review;
private String rating;
private int client_id;
private int movie_id;
public ReviewDTO() {
}
public ReviewDTO(int id, String review, String rating, int client_id, int movie_id) {
super();
this.id = id;
this.review = review;
this.rating = rating;
this.client_id = client_id;
this.movie_id = movie_id;
}
public int getid() {
return id;
}
public void setid(int id) {
this.id = id;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
public String getRating() {
return rating;
}
public void setRating(String rating) {
this.rating = rating;
}
public int getClient_id() {
return client_id;
}
public void setClient_id(String client_id) {
this.client_id = new Integer(client_id);
}
public int getMovie_id() {
return movie_id;
}
public void setMovie_id(int movie_id) {
this.movie_id = movie_id;
}
}
package edu.unsw.comp9321.jdbc;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.OneToMany;
public class MovieDTO implements Comparable {
private int id;
private String title;
private String poster;
private String director;
private String actors;
private String synopsis;
private String release_date;
private int cinema_id;
private Set<GenreDTO> genres = new HashSet<GenreDTO>();
private Set<ReviewDTO> reviews = new HashSet<ReviewDTO>();
private double rating;
public MovieDTO() {
}
public MovieDTO(int id, String title, String poster, String director,
String actors, String synopsis, String release_date, double rating) {
super();
this.id = id;
this.title = title;
this.poster = poster;
this.director = director;
this.actors = actors;
this.synopsis = synopsis;
this.release_date = release_date;
this.rating = rating;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPoster() {
return poster;
}
public void setPoster(String poster) {
this.poster = poster;
}
public String getDirector() {
return director;
}
public void setDirector(String director) {
this.director = director;
}
public String getActors() {
return actors;
}
public void setActors(String actors) {
this.actors = actors;
}
public String getSynopsis() {
return synopsis;
}
public void setSynopsis(String synopsis) {
this.synopsis = synopsis;
}
public String getRelease_date() {
return release_date;
}
public void setRelease_date(String release_date) {
this.release_date = release_date;
}
public Set<GenreDTO> getGenres() {
return genres;
}
public void setGenres(Set<GenreDTO> genres) {
this.genres = genres;
}
public Set<ReviewDTO> getReviews() {
return reviews;
}
public void setReviews(Set<ReviewDTO> reviews) {
this.reviews = reviews;
}
public int getCinema_id() {
return cinema_id;
}
public void setCinema_id(int cinema_id) {
this.cinema_id = cinema_id;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
@Override
public int compareTo(Object o) {
MovieDTO other = (MovieDTO) o;
if (this.rating > other.rating) return -1;
if (this.rating < other.rating) return 1;
return 0;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="edu.unsw.comp9321.jdbc.ReviewDTO" table="Review" >
<id column="id" name="id">
<generator class="identity" />
</id>
<property column="review" name="review" type="string" />
<property column="rating" name="rating" type="string" />
<property column="client_id" name="client_id" type="string" />
<property column="movie_id" name="movie_id" type="string" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="edu.unsw.comp9321.jdbc.MovieDTO" table="Movie" >
<id column="id" name="id">
<generator class="identity" />
</id>
<property column="title" name="title" type="string" />
<property column="poster" name="poster" type="string" />
<property column="director" name="director" type="string" />
<property column="actors" name="actors" type="string" />
<property column="synopsis" name="synopsis" type="string" />
<property column="release_date" name="release_date" type="string" />
<set name="genres" table="MovieHasGenre" >
<key column="movie_id" not-null="true" />
<many-to-many class="edu.unsw.comp9321.jdbc.GenreDTO" column="genre_id" />
</set>
<set name="reviews" table="Review" >
<key column="movie_id" />
<one-to-many class="edu.unsw.comp9321.jdbc.ReviewDTO" />
</set>
</class>
</hibernate-mapping>
最佳答案
收到的第一个错误是因为您定义了<key column="movie_id" not-null="true" />
,这意味着movie_id
在该GenreDTO
的MovieDTO
中不能为null。尝试保留时,代码中某个位置的值为null(显示的代码不足以告诉您位置)。
第二个错误是因为您定义了<property column="movie_id" name="movie_id" type="string" />
,但是在您的ReviewDTO
中,movie_id
是一个整数。它们应该是相同的。
关于您的问题,是在movie_id
中使用MovieDTO
还是ReviewDTO
。那取决于几件事。双向关系很好(意味着ReviewDTO
附加了MovieDTO
,而MovieDTO
列表中的相同ReviewDTO
具有相同的Reviews
。但是,如果您这样做,则会发生可伸缩性和性能问题具有数以百万计的实体。相反,仅保留movie_id
整数意味着您随时都希望从MovieDTO
中获取ReviewDTO
对象时,您将有另一个数据库调用...但是这可能不会太昂贵,因为如果它是双向的,定向,无论如何都会发生该呼叫。