hibernate - InvalidDataAccessApiUsageException @OneToMany and @ManyToOne relationship
问题描述
When I'm trying to save an object into the WatchedEpisodeRepository I get the following error:
Can not set User field WatchedEpisode$WatchedEpisodeId.user to java.lang.Integer
Below are my classes. WatchedEpisode is supposed to be a ManyToMany junction table between User and Episode. I think hibernate can't get the user's and episode's id to create the query for inserting the WatchedEpisode object into the Repository.
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;
@NotNull
@NotEmpty
@Column(unique = true)
@Size(min = 8, max = 30)
private String username = "";
public List<Request> getReceivedRequests() {
return receivedRequests;
}
@NotNull
@NotEmpty
@Size(min = 8)
private String password = "";
@Email
@NotNull
@NotEmpty
@Column(unique = true)
private String email = "";
@NotNull
@NotEmpty
private String firstName = "";
@NotNull
@NotEmpty
private String lastName = "";
@OneToMany(mappedBy = "user")
List<WatchedEpisode> watchedEpisode = new ArrayList<>();
//getters and setters
}
@Entity
@Table
public class Episode {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@NotNull
@NotEmpty
private String title;
@NotNull
@NotEmpty
private int season;
@NotNull
@NotEmpty
private int duration;
private String description;
@ManyToOne
TvSeries tvSeries;
@OneToMany(mappedBy = "episode")
List<WatchedEpisode> watchedEpisodes = new ArrayList<>();
//getter and setters
}
@Entity
@IdClass(WatchedEpisode.WatchedEpisodeId.class)
@Table
public class WatchedEpisode implements Serializable {
@Id
@ManyToOne(targetEntity = User.class)
private User user;
@Id
@ManyToOne(targetEntity = Episode.class)
private Episode episode;
@NotNull
@NotEmpty
private LocalDate dateStarted;
public WatchedEpisode() {
}
public WatchedEpisode(User user, Episode episode, @NotNull @NotEmpty LocalDate dateStarted) {
this.user = user;
this.episode = episode;
this.dateStarted = dateStarted;
}
public User getUser() {
return user;
}
public Episode getEpisode() {
return episode;
}
public LocalDate getDateStarted() {
return dateStarted;
}
public static class WatchedEpisodeId implements Serializable {
private User user;
private Episode episode;
public WatchedEpisodeId(User user, Episode episode) {
this.user = user;
this.episode = episode;
}
public WatchedEpisodeId() {
}
@Override
public int hashCode() {
return Objects.hash(user, episode);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof WatchedEpisodeId)) {
return false;
}
WatchedEpisode watchedEpisode = (WatchedEpisode) obj;
return Objects.equals(user, watchedEpisode.getUser()) &&
Objects.equals(episode, watchedEpisode.getEpisode());
}
}
}
解决方案
推荐阅读
- symfony - “symfony new project-name”命令以 ISO-9958-1 编码生成文件
- python - 安装 python2 pyyaml
- quickfix - 使用 QuickFIXN 的 FIX 客户端拒绝来自服务器的报价取消消息 - 缺少所需的标签 295 NoQuoteEntries - FIX 4.2
- php - 在 PHP - IMAP 中从 ISO-2022-JP-MS 解码为 UTF-8
- jira - JQL - 通过键入一次来搜索多个字段中的值
- sql-server - 更新数据库事务中的 COLUMNSTORE 索引
- ios - 如何使用一个带有 Swift 的 UISlider 更改亮度和对比度
- robotframework - 机器人框架硒在python脚本中计算昨天的日期
- logic-programming - 在 clgo 中有效地减少搜索空间
- elixir - 确保 GenServer 有明确的 handle_info 调用