- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到过这样的情况:一部电影可以有很多评论,但评论只与一部电影相关。我遇到了如下所述的问题,但由于我现在已经尝试了很多排列,所以我实际上有点困惑,所以在我再搞乱我的代码之前,我认为最好在这里问一下。
问:应该如何设置?在保存评论之前,我可以在评论中将 movie_id 硬设置为 int 吗?或者我是否需要有一个 MovieDTO 电影对象作为 ReviewDTO 类中的对象之一?当我创建要保存的评论对象时,我调用 review.setMovie(someMovieObject)?
所以这是我的代码中发生的错误:
首先它提示未设置非空电影引用(Hibernate:非空属性引用空值或 transient 值),因此在无法修复它之后,有人建议删除约束,看看它是否有效。
现在它提示“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_id
或MovieDTO
在你的ReviewDTO
。这取决于几件事。双向关系很好(意味着 ReviewDTO
附加了 MovieDTO
,而相同的 MovieDTO
在 ReviewDTO
列表中具有相同的 Reviews
。但是,如果您相反,只留下 movie_id
整数意味着任何时候您想要来自 MovieDTO
的 ReviewDTO
对象,您将有另一个数据库调用...但是这可能不会太昂贵,因为如果它是双向的方向性,无论如何,该调用都会发生。
关于java - hibernate 错误: "java.lang.Integer cannot be cast to java.lang.String" and so on,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26280303/
什么是 hibernate 和n- hibernate ?我可以在 Visual Studio 2008 中使用它进行 C# Web 应用程序开发吗?请给我建议...我是 asp.net Web 应用
我有一个不系统地发生的异常(exception)。 我试图通过在每次迭代中刷新和清理 session 来解决此问题,但没有成功。 [quartzScheduler_Worker-7] ERROR jd
使用 Hibernate 在数据库中存储 IP 地址的最佳类型是什么? 我虽然是 Byte[] 或 String,但有没有更好的方法,或者你用什么? @Column(name = "range_fr
我正在尝试制定一个公式来选择用户个人资料的用户友好名称。它选择名字 + ' ' + 姓氏 如果其中至少有一个不为空且不为空(包含非空白字符),否则选择 短名称 (条件相同),最后,如果 短名称 为空或
在hibernate中,是否可以将鉴别器作为一个实体?例如,如果我将 Department 作为基类,将 AdminDepartment 和 ProcessingDepartment 作为子类。 De
我只想从表中获取一些列值。因此,我已经使用投影来实现这一目标。该代码有效,但我认为它无效。 我的问题是当我使用ProjectionsList并将标准条件列表设置为ArrayList时-Bulletin
你好: 我对 hibernate 缓存缓存的内容感到困惑。 从文档中,我知道 hibernate 中有缓存类型。 一级 :交易级别。 似乎要被 session 持久化的实体被缓存在这里。 二级缓存 :
我遇到了一个情况: save或update hibernate 的目标表中的某些数据 在目标表上有一个触发器,该触发器将在目标表的insert或update操作之前执行 由 hibernate 将此记
我有一个名为 Master_Info_tbl 的表。它是一个查询表: 这是该表的代码: @Entity @Table(name="MASTER_INFO_T") public class Code
我想知道如何在 Hibernate 查询语言中使用日期文字。我在我的 JPA 项目中做了如下操作(作为 Eclipselink 提供者)并且它工作正常。 SELECT m FROM Me m WHER
@Entity public class Troop { @OneToMany(mappedBy="troop") public Set getSoldiers() { ...
我正在尝试使用 hibernate 查询删除表 'user_role' 中的所有行。但每次我都会出错。有人可以帮我吗。 DaoImpl @Override public void deleteAll(
不是将数据库操作分散在四个 (osgi) 包中,而是在那里做略有不同的事情。我想创建一个负责所有持久性问题的(简单的)OSGi 包。我觉得这并不像听起来那么简单,因为“每个包都有独特的类加载器”。 因
这就是我使用生成器的方式: private Integer id; 我看到的行为是: 创建第一个对象 hibernate 分配 id = 1 删除该对象 关闭服务
对象级别的实体和值类型有什么区别。我知道实体将有一个 id 但值不会,但为什么我们需要不同的方法来映射实体与值类型? 这样做是为了让hibernate可以对值类型应用任何优化吗? 最佳答案 一个实体已
我正在使用 HibernateTemplate.findByCriteria 方法进行一些查询。现在我想在标准上创建一些 SQL 限制,比如 criteria.add(Restrictions.sql
所以我有以下代码: Query query = session.createQuery("from Weather"); List list = query.list();
如何使用Hibernate映射具有多个实体的 View ? 问候, 混沌 最佳答案 请参见Hibernate文档中第5.1.3节“类”,紧接在“Id”节之前: There is no differen
据我所知,Hibernate 有两种类型的实现 JPA的实现(2)(@Entity,@Table注解) 扩展到旧的(传统的) hibernate (没有 JPA),使用 HSQL 查询,没有注释 如果
我需要一个将条目存储为键值对的集合(因此我可以通过键查找值),但我需要一个允许多个值使用 hibernate 共享同一个键的集合 最佳答案 一个键具有多个值的映射称为多映射 - 在 Apache 公共
我是一名优秀的程序员,十分优秀!