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

创建Skus实体类报错:Not a managed type: .entity.Skus

解决「Not a managed type: .entity.Skus」错误

1. 统一JPA注解包

你代码里混着用了jakarta.persistence.Column和javax.persistence.Entity,这会导致注解不兼容。Spring Boot 3及以上版本已经切换到Jakarta EE规范,所有JPA注解都要从jakarta.persistence包导入,修正后的代码:

package <path>.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity(name = "skus")
public class Skus extends BaseEntity{
    @Column(name = "name")
    private String name;
}

2. 确保实体类被Spring扫描到

如果你的主启动类(带@SpringBootApplication的类)所在包不是<path>.entity的上层或同级,Spring默认不会扫描到这个实体类,有两种解决方式:

  • 把主启动类移到<path>包下(比如<path>.Application),Spring会自动扫描子包内的实体类。
  • 在主启动类上添加@EntityScan注解,指定实体类所在包:
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan("<path>.entity") // 替换成你的实体类实际包路径
public class YourApplication {
    public static void main(String[] args) {
        SpringApplication.run(YourApplication.class, args);
    }
}

3. 检查父类BaseEntity的配置

如果BaseEntity是自定义父类,要确保它的注解配置正确。比如用@MappedSuperclass标记父类是合法的,但如果父类误用了@Entity却没被Spring扫描到,也会影响子类的识别。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 08:57:01