I am trying to use JPA for a Java Spring Boot project. The related classes are given below. When I run the main method, I get the java.lang.IllegalArgumentException: Not a managed type: class com.domain.example.model.Course
error multiple times, and the application crashes. I tried different configurations but none seem to work, as all of them result in the same issue. How can I resolve the not a managed type
issue?
Main class:
package com.domain.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
// @Configuration
// @EnableAutoConfiguration// (exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
// @ComponentScan
@SpringBootApplication
// @EnableJpaRepositories
@EntityScan(basePackages = {"com.domain.example.model"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
Model class:
package com.domain.example.model;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
// import lombok.RequiredArgsConstructor;
@Entity
@Data
@Table(name = "course")
// @RequiredArgsConstructor
public class Course {
@Id
@GeneratedValue(generator = "UUID")
@Column(name = "course_id")
private final UUID courseId;
@NotNull
@Column(name = "code")
private final Long code;
@NotBlank
@Column(name = "name")
private final String name;
public Course(
@JsonProperty("courseId") UUID courseId,
@JsonProperty("code") Long code,
@JsonProperty("name") String name
) {
this.courseId = (courseId == null) ? UUID.randomUUID() : courseId;
this.code = code;
this.name = name;
}
}
The repository:
package com.domain.example.repository;
import java.util.List;
import java.util.UUID;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.domain.example.model.Course;
@Repository
public interface ICourseRepository extends JpaRepository<Course, UUID> {
List<Course> findAllByCode(Long code); // temporary
}
The service:
package com.domain.example.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.domain.example.model.Course;
import com.domain.example.repository.ICourseRepository;
import lombok.RequiredArgsConstructor;
@Service
@RequiredArgsConstructor
public class CourseService implements ICourseService {
private final ICourseRepository courseRepository;
@Override
public boolean saveCourse(Course course) {
courseRepository.save(course);
return true;
}
@Override
public List<Course> getAllCourses() {
return courseRepository.findAll();
}
@Override
public List<Course> getAllCoursesByCode(Long code) {
return courseRepository.findAllByCode(code);
}
}
The error:
java.lang.IllegalArgumentException: Not a managed type: class com.domain.example.model.Course
CodePudding user response:
As @xerx593 recommended, changing all imports from javax.persistance
to jakarta.persistance
solved the issue.
Previous model class:
package com.domain.example.model;
import java.util.UUID;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
// import lombok.RequiredArgsConstructor;
@Entity
@Data
@Table(name = "course")
// @RequiredArgsConstructor
public class Course {
// ... implementation ...
}
New model class:
package com.domain.example.model;
import java.util.UUID;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
// import lombok.RequiredArgsConstructor;
@Entity
@Data
@Table(name = "course")
// @RequiredArgsConstructor
public class Course {
// ... implementation ...
}