休眠错误:“;无法将java.lang.Integer转换为java.lang.String;等等
我有这样一种情况,一部电影可以有很多评论,但评论只与一部电影相关。我遇到了如下所述的问题,但由于我现在尝试了这么多的排列,我实际上有点困惑,所以在我再弄乱我的代码之前,我认为最好在这里提问 该如何设置?在保存评论之前,我可以在评论中将电影id硬设为int吗?或者我需要一个moviedtomovie对象作为ReviewDTO类中的一个对象吗?当我创建一个要保存的review对象时,我称之为review.setMovie(someMovieObject) 下面是我的代码中发生的错误:休眠错误:“;无法将java.lang.Integer转换为java.lang.String;等等,java,hibernate,Java,Hibernate,我有这样一种情况,一部电影可以有很多评论,但评论只与一部电影相关。我遇到了如下所述的问题,但由于我现在尝试了这么多的排列,我实际上有点困惑,所以在我再弄乱我的代码之前,我认为最好在这里提问 该如何设置?在保存评论之前,我可以在评论中将电影id硬设为int吗?或者我需要一个moviedtomovie对象作为ReviewDTO类中的一个对象吗?当我创建一个要保存的review对象时,我称之为review.setMovie(someMovieObject) 下面是我的代码中发生的错误: 首先,它抱怨没
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>
包edu.unsw.comp9321.jdbc;
公开课复习{
私有int-id;
私人字符串审查;
私人字符串评级;
私人int客户端id;
私人电影识别码;
公开评论{
}
public ReviewDTO(int-id、String-review、String-rating、int-client\u-id、int-movie\u-id){
超级();
this.id=id;
这个。回顾=回顾;
这个。评级=评级;
this.client\u id=client\u id;
this.movie\u id=movie\u id;
}
公共int getid(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getReview(){
回报审查;
}
公共void setReview(字符串审阅){
这个。回顾=回顾;
}
公共字符串getRating(){
回报率;
}
公共无效设置等级(字符串等级){
这个。评级=评级;
}
public int getClient_id(){
返回客户端id;
}
public void setClient_id(字符串client_id){
this.client\u id=新整数(client\u id);
}
public int getMovie_id(){
返回电影id;
}
公共无效设置电影id(int电影id){
this.movie\u id=movie\u id;
}
}
包edu.unsw.comp9321.jdbc;
导入java.util.Comparator;
导入java.util.HashSet;
导入java.util.Set;
导入javax.persistence.OneToMany;
公共类电影实现可比性{
私有int-id;
私有字符串标题;
私人弦乐海报;
私有字符串控制器;
私人弦乐演员;
私有字符串概要;
私有字符串发布日期;
私人国际影院;
私有集类型=新HashSet();
private Set reviews=new HashSet();
私人双重评级;
公映{
}
公共电影导演(int id、字符串标题、字符串海报、字符串导演、,
弦乐演员、弦乐大纲、弦乐发行日期、双倍评级){
超级();
this.id=id;
this.title=标题;
this.poster=海报;
this.director=director;
这个。演员=演员;
this.synosis=大纲;
this.release\u date=发布日期;
这个。评级=评级;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getPoster(){
返回海报;
}
公共海报(弦乐海报){
this.poster=海报;
}
公共字符串getDirector(){
返回董事;
}
公共无效集合控制器(字符串控制器){
this.director=director;
}
公共字符串getActors(){
返回演员;
}
public void setActors(字符串actors){
这个。演员=演员;
}
公共字符串getSynosis(){
返回概要;
}
公共无效设置同步(字符串概要){
this.synosis=大纲;
}
公共字符串getRelease_date(){
返回发布日期;
}
公共作废设置发布日期(字符串发布日期){
this.release\u date=发布日期;
}
公共集getGenres(){
回归体裁;
}
公共无效集合类型(集合类型){
this.genres=流派;
}
公共集getReviews(){
回报审查;
}
公共无效集合审查(集合审查){
这个.评论=评论;
}
public int getCinema_id(){
返回电影院标识;
}
公共无效设置电影院id(国际电影院id){
this.cinema\u id=cinema\u id;
}
公共双重评级(){
回报率;
}
公共评级(双重评级){
这个。评级=评级;
}
@凌驾
公共整数比较对象(对象o){
MovieDTO other=(MovieDTO)o;
如果(this.rating>other.rating)返回-1;
如果(本评级<其他评级)返回1;
返回0;
}
}
您收到的第一个错误是因为您定义了
,这意味着movie\u id
在该MovieDTO的GenreDTO
中不能为空。在代码中的某个地方,当试图持久化时,该值为null(没有显示足够的代码来告诉您在哪里)
第二个错误是因为您定义了
,但在ReviewDTO
电影中,id
是一个int。它们应该是相同的
至于你的问题,你应该使用movie\u id
还是a