gpt4 book ai didi

java - hibernate 错误: "java.lang.Integer cannot be cast to java.lang.String" and so on

转载 作者:行者123 更新时间:2023-12-01 12:26:38 25 4
gpt4 key购买 nike

我遇到过这样的情况:一部电影可以有很多评论,但评论只与一部电影相关。我遇到了如下所述的问题,但由于我现在已经尝试了很多排列,所以我实际上有点困惑,所以在我再搞乱我的代码之前,我认为最好在这里问一下。

问:应该如何设置?在保存评论之前,我可以在评论中将 movie_id 硬设置为 int 吗?或者我是否需要有一个 MovieDTO 电影对象作为 ReviewDTO 类中的对象之一?当我创建要保存的评论对象时,我调用 review.setMovie(someMovieObject)?

所以这是我的代码中发生的错误:

  1. 首先它提示未设置非空电影引用(Hibernate:非空属性引用空值或 transient 值),因此在无法修复它之后,有人建议删除约束,看看它是否有效。

  2. 现在它提示“java.lang.Integer 无法转换为 java.lang.String”,但我很困惑哪里存在类型不匹配?类<->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 内不能为 null其中MovieDTO 。在代码中的某个位置,当尝试持久化时,该值为空(没有显示足够的代码来告诉您位置。)

第二个错误是因为你定义了<property column="movie_id" name="movie_id" type="string" />但在你的ReviewDTO movie_id是一个整数。它们应该是相同的。

至于你的问题,是否应该使用movie_idMovieDTO在你的ReviewDTO 。这取决于几件事。双向关系很好(意味着 ReviewDTO 附加了 MovieDTO ,而相同的 MovieDTOReviewDTO 列表中具有相同的 Reviews 。但是,如果您相反,只留下 movie_id 整数意味着任何时候您想要来自 MovieDTOReviewDTO 对象,您将有另一个数据库调用...但是这可能不会太昂贵,因为如果它是双向的方向性,无论如何,该调用都会发生。

关于java - hibernate 错误: "java.lang.Integer cannot be cast to java.lang.String" and so on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26280303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com