hibernate:mappedBy在oneToMany关系中引用未知的目标实体属性

时间:2018-05-29 21:12:48

标签: java hibernate hibernate-mapping

我的代码中有以下结构:

用户有TvShows的收藏列表,TvShow有季节列表,季节有剧集列表。

用户

@Entity
@Table(name = "users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "user_id", updatable = false, nullable = false)
private int id;

@OneToMany(mappedBy = "tvshows", fetch = FetchType.EAGER)
private List<TvShow> favourites;
/// everything else removed for clarity
}

TvShow

@Entity
@Table(name = "tvshows")
public class TvShow {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "tvShow_id", updatable = false, nullable = false)
private int id;

@OneToMany(mappedBy = "tvshows", fetch = FetchType.EAGER)
private List<Season> seasons;
/// everything else removed for clarity
}

@Entity
@Table(name = "seasons")
public class Season{

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "season_id", updatable = false, nullable = false)
private int id;

@OneToMany(mappedBy = "seasons", fetch = FetchType.EAGER)
private List<Episode> episodes;
/// everything else removed for clarity
}

应用程序不会从以下错误消息开始:

mappedBy reference an unknown target entity property: com.gcimpoies.project.model.Season.seasons in com.gcimpoies.project.model.TvShow.seasons

另外,

Invocation of init method failed; nested exception is org.hibernate.AnnotationException: mappedBy reference an unknown target entity property: com.gcimpoies.project.model.Season.seasons in com.gcimpoies.project.model.TvShow.seasons

我很确定我只是错过了正确的属性名称(mappedBy的参数),但我不知道我做错了什么。

提前致谢!

1 个答案:

答案 0 :(得分:2)

您必须在mappedBy内添加的名称是您引用的类的ID。

而不是:

@OneToMany(mappedBy = "tvshows", fetch = FetchType.EAGER)

尝试:

@OneToMany(mappedBy = "id", fetch = FetchType.EAGER)

您也必须为其余的映射执行此操作。