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

Spring Boot项目@Entity/@Id注解无法解析,提示javax.persistence包不存在

Fixing "Package javax.persistence does not exist" in Spring Boot

Hey there! Since you're new to Spring Boot, let's break down why this error is popping up and fix it step by step.

Why You're Seeing This Error

There are two key reasons behind the "Package javax.persistence does not exist" issue:

  • Spring Boot Version Shift: Starting with Spring Boot 3.x, JPA-related packages moved from javax.persistence to jakarta.persistence—this is part of the industry-wide switch from Java EE to Jakarta EE.
  • Missing Core Dependency: You likely haven't added the Spring Data JPA starter to your pom.xml, which includes all the JPA annotations and underlying libraries your project needs.

Step 1: Add the Required Dependency to pom.xml

First, make sure your pom.xml includes the Spring Data JPA starter. Drop this inside the <dependencies> section:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

Note: If you're using Spring Boot 2.x, this dependency still works, and you can keep using the javax.persistence package.

Step 2: Update Your Import Statements

If you're on Spring Boot 3.x, replace all your javax.persistence imports with jakarta.persistence. For example:
Instead of this:

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.GeneratedValue;

Use this:

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.GeneratedValue;

Step 3: Refresh Maven Dependencies

After updating your pom.xml, you need to tell your IDE to pull in the new libraries:

  • In IntelliJ IDEA: Right-click your project → Maven → Reload All Maven Projects
  • In Eclipse: Right-click your project → Maven → Update Project...

Quick Check: Confirm Your Spring Boot Version

To verify which version you're running, look at the spring-boot-starter-parent in your pom.xml:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.2.0</version> <!-- This is a 3.x version -->
    <relativePath/>
</parent>

If the version starts with 2.x, stick with javax.persistence imports and just ensure the JPA starter dependency is present.

That should clear up the "cannot resolve symbol" errors for your @Entity, @Id, and other JPA annotations!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 09:46:05