Not Found javax.persistence in My Spring-boot Project

Java Knowledge Sharing…

Geno Tech
2 min readMay 19, 2023
Photo by Florian Olivo on Unsplash

Here in my case, I tried to import javax.persistence, but It shows, It doesn’t exist. So I found the following solution. I hope. It will help in your case as well. The main reason for this problem is the replacement of javax with jakarta. Therefore the solution is to add the following dependency to your pom.xml.

 <dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>

Then you can import javax.persistence without any issue.

import jakarta.persistence.*;

This is the pom.xml and my java class.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>annotationProcessor</scope>
</dependency>

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

<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
package com.example.demo.entity;

import jakarta.persistence.*;
@Entity
@Table(name = "customer")
public class Customer {
@Id
@Column(name = "customer_id", length = 50)
@GeneratedValue(strategy = GenerationType.AUTO)
private int customerid;

@Column(name = "customer_name", length = 50)
private String customername;

@Column(name = "customer_address", length = 60)
private String customeraddress;

@Column(name = "mobile", length = 12)
private int mobile;

public Customer(int customerid, String customername, String customeraddress, int mobile) {
this.customerid = customerid;
this.customername = customername;
this.customeraddress = customeraddress;
this.mobile = mobile;
}

public Customer() {
}

public Customer(String customername, String customeraddress, int mobile) {
this.customername = customername;
this.customeraddress = customeraddress;
this.mobile = mobile;
}

public int getCustomerid() {
return customerid;
}

public void setCustomerid(int customerid) {
this.customerid = customerid;
}

public String getCustomername() {
return customername;
}

public void setCustomername(String customername) {
this.customername = customername;
}

public String getCustomeraddress() {
return customeraddress;
}

public void setCustomeraddress(String customeraddress) {
this.customeraddress = customeraddress;
}

public int getMobile() {
return mobile;
}

public void setMobile(int mobile) {
this.mobile = mobile;
}

@Override
public String toString() {
return "Customer{" +
"customerid=" + customerid +
", customername='" + customername + '\'' +
", customeraddress='" + customeraddress + '\'' +
", mobile=" + mobile +
'}';
}
}

--

--

Geno Tech

Software Development | Data Science | AI — We write rich & meaningful content on development, technology, digital transformation & life lessons.