我的代码具有以下结构:
用户具有TvShow的收藏夹列表,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
}
电视节目
@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的参数),但是我不知道我在做什么错。
提前致谢!
最佳答案
您必须放在mappedBy
中的名称是您所引用的类的ID。
代替:
@OneToMany(mappedBy = "tvshows", fetch = FetchType.EAGER)
尝试:
@OneToMany(mappedBy = "id", fetch = FetchType.EAGER)
您还必须为其余映射执行此操作。
关于java - hibernate :mappedBy在oneToMany关系中引用未知的目标实体属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50593035/