You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hibernate一对多关联报错:mappedBy引用未知目标实体属性

Hey George, let's fix this mappedBy issue step by step—you're right that the problem lies in the property names you're using for mappedBy, so let's break down exactly what's wrong and how to resolve it.

Why the Error Occurs

The mappedBy attribute in @OneToMany tells Hibernate which property in the child entity holds the reference back to the parent. Your current code is using the parent's collection property name (like seasons in TvShow) instead of the child's reference property name, which is why Hibernate can't find the target property.

Let's walk through each relationship to fix it:


1. User ↔ TvShow Relationship

Your User class uses @OneToMany(mappedBy = "tvshows")—but this expects the TvShow class to have a property named tvshows annotated with @ManyToOne, which isn't correct. Instead, TvShow should have a User property (since each favorite TvShow belongs to one User), and mappedBy should point to that property name.

2. TvShow ↔ Season Relationship

Same mistake here: TvShow uses mappedBy = "seasons", but Season doesn't have a property named seasons referencing TvShow. Season needs a TvShow property, and mappedBy should use that property's name.

3. Season ↔ Episode Relationship

The pattern repeats: Season's @OneToMany(mappedBy = "episodes") is incorrect—Episode needs a Season property, and mappedBy should target that property.


Fixed Entity Code

Here's the corrected code for each entity, with comments highlighting the changes:

User Entity

@Entity 
@Table(name = "users") 
public class User { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "user_id", updatable = false, nullable = false) 
    private int id; 

    // Changed mappedBy to "user" (matches the new property in TvShow)
    @OneToMany(mappedBy = "user", fetch = FetchType.EAGER) 
    private List<TvShow> favourites; 

    // Rest of your code...
}

TvShow Entity

@Entity 
@Table(name = "tvshows") 
public class TvShow { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "tvShow_id", updatable = false, nullable = false) 
    private int id; 

    // Added: Many-to-one reference back to User
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;

    // Changed mappedBy to "tvShow" (matches the new property in Season)
    @OneToMany(mappedBy = "tvShow", fetch = FetchType.EAGER) 
    private List<Season> seasons; 

    // Rest of your code...
}

Season Entity

@Entity 
@Table(name = "seasons") 
public class Season{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "season_id", updatable = false, nullable = false) 
    private int id; 

    // Added: Many-to-one reference back to TvShow
    @ManyToOne
    @JoinColumn(name = "tvshow_id")
    private TvShow tvShow;

    // Changed mappedBy to "season" (matches the new property in Episode)
    @OneToMany(mappedBy = "season", fetch = FetchType.EAGER) 
    private List<Episode> episodes; 

    // Rest of your code...
}

Episode Entity (required to complete the chain)

@Entity 
@Table(name = "episodes") 
public class Episode{ 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "episode_id", updatable = false, nullable = false) 
    private int id; 

    // Added: Many-to-one reference back to Season
    @ManyToOne
    @JoinColumn(name = "season_id")
    private Season season;

    // Rest of your code...
}

Key Takeaway

Always remember: mappedBy = "X" means "look for the property named X in the child entity that references this parent entity". It never refers to the collection property in the parent.

内容的提问来源于stack exchange,提问作者George Cimpoies

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:04:54