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

Hibernate AnnotationException排查:mappedBy引用未知实体属性

解决Hibernate mappedBy引用未知属性的问题

核心问题定位

报错的直接原因是**mappedBy的值与目标实体的属性名不匹配**:

  • 在UserProfile的@OneToMany(mappedBy="user_profile")中,你指定的是下划线命名的user_profile,但Show类中对应的关联属性是驼峰命名的userProfile,Hibernate无法找到这个属性,因此抛出异常。

同时代码还有两个不符合JavaBean规范的问题,也会影响Hibernate的映射逻辑:

  1. UserProfile中的setter方法命名错误:比如ListId、ListType不符合标准setXxx()的命名规则,Hibernate无法正确识别属性。
  2. getshow()方法命名错误:应改为getShow(),属性名show对应的getter需要首字母大写。

修正后的代码

修正后的UserProfile类

package com.vitoria.models;

import java.io.Serializable;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name="user_profile")
public class UserProfile implements Serializable{
    
    private static final long serialVersionUID = 1L;
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="user_profile_id")
    private Integer id;
    
    @Column(name="user_profile_type")
    private String type;
    
    // 需确保User类中存在名为user_profile的UserProfile属性,否则会触发同类报错
    @OneToOne(mappedBy="user_profile")
    private User user;
    
    // 修正mappedBy值为Show类中的属性名userProfile
    @OneToMany(mappedBy="userProfile")
    private List<Show> show;

    public UserProfile() {
        super();
    }

    public UserProfile(Integer id, String type, User user, List<Show> show) {
        super();
        this.id = id;
        this.type = type;
        this.user = user;
        this.show = show;
    }

    public Integer getId() {
        return id;
    }

    // 修正setter方法名
    public void setId(Integer id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    // 修正getter方法名
    public List<Show> getShow() {
        return show;
    }

    public void setShow(List<Show> show) {
        this.show= show;
    }
}

Show类无需修改(确认属性名userProfile即可)

package com.vitoria.models;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name="show")
public class Show implements Serializable{
    private static final long serialVersionUID = 1L;
    
    @Column(name="show_name", unique=true)
    private String name;
    
    @Column(name="show_genre")
    private String genre;
    
    @Column(name="show_synopsis")
    private String synopsis;
    
    @Column(name="show_id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Id
    private Integer id;
    
    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name="user_profile_id")
    private UserProfile userProfile;

    public Show() {
    }

    public Show(String name, String genre, String synopsis, Integer id, UserProfile userProfile) {
        this.name = name;
        this.genre = genre;
        this.synopsis = synopsis;
        this.id = id;
        this.userProfile = userProfile;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGenre() {
        return genre;
    }

    public void setGenre(String genre) {
        this.genre = genre;
    }

    public String getSynopsis() {
        return synopsis;
    }

    public void setSynopsis(String synopsis) {
        this.synopsis = synopsis;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }
}

额外注意事项

  • 对于@OneToOne(mappedBy="user_profile"),需同步检查User类中是否存在名为user_profile的UserProfile类型属性,否则会触发同类报错。
  • Hibernate完全依赖JavaBean规范的getter/setter方法识别实体属性,必须严格遵循getXxx()/setXxx()的命名格式,属性名首字母在方法名中需大写。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 01:25:26