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

添加OneToOne映射后出现Hibernate未知实体AnnotationException异常

解决Hibernate OneToOne映射的未知实体异常

问题描述

在FeeSchedulePracticeLocationEntity类中添加OneToOne映射后,抛出以下异常:

org.hibernate.AnnotationException: Association 'xxx.FeeSchedulePracticeLocationEntity.organizationLocation' targets an unknown entity named 'xxx.OrganizationLocationEntity'

相关实体类代码:

FeeSchedulePracticeLocationEntity

@Entity
@Table(name = "fee_schedule_practice_location")
@AttributeOverrides({@AttributeOverride(name = "id", column = @Column(name = "fee_schedule_practice_location_id"))})
public class FeeSchedulePracticeLocationEntity extends AuditableEntity {

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "fee_schedule_id", nullable = false)
    private FeeScheduleEntity feeScheduleEntity;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "practice_location_id", referencedColumnName = "organization_location_id")
    private OrganizationLocationEntity organizationLocationEntity;

    // getters and setters
}

OrganizationLocationEntity

@Entity
@Table(name = "organization_location")
@AttributeOverrides({@AttributeOverride(name = "id", column = @Column(name = "organization_location_id"))})
public class OrganizationLocationEntity extends AuditableEntity implements ChildEntity {
    // 字段定义
}

解决方案

  • 确认实体被Hibernate扫描到
    检查项目的Hibernate实体扫描配置:

    • Spring Boot项目:确保OrganizationLocationEntity所在包在启动类的扫描范围内,若不在,添加@EntityScan(basePackages = {"你的实体包路径"})到启动类上。
    • 传统Hibernate项目:在persistence.xml中添加<class>xxx.OrganizationLocationEntity</class>标签,显式注册该实体。
  • 验证实体引用的全限定名正确
    检查FeeSchedulePracticeLocationEntity中OrganizationLocationEntity的导入语句,确保包路径和类名完全匹配,避免拼写错误或大小写不一致。

  • 确保实体类定义合法

    • 确认OrganizationLocationEntity上的@Entity注解无拼写错误。
    • 实体类必须包含无参构造函数(Hibernate反射实例化需要),如果自定义了带参构造,需手动添加无参构造。
    • 检查父类AuditableEntity的注解配置,确保ID字段的@AttributeOverride没有冲突或错误。
  • 核对数据库表与映射配置匹配
    再次确认@JoinColumn的配置:

    • name="practice_location_id"是fee_schedule_practice_location表中的外键列,需确保该列在数据库中存在且类型与organization_location表的organization_location_id列一致。
    • referencedColumnName="organization_location_id"是OrganizationLocationEntity对应的主键列,需与实体类中ID字段的@Column配置一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 13:07:27