Spring JPA,如何解决“java.lang.非法参数异常:不是托管类型”错误?

gcuhipw9  于 2022-12-17  发布在  Spring
关注(0)|答案(1)|浏览(193)

我正在尝试为Java Sping Boot 项目使用JPA。下面给出了相关的类。当我运行main方法时,我多次收到java.lang.IllegalArgumentException: Not a managed type: class com.domain.example.model.Course错误,应用程序崩溃。我尝试了不同的配置,但似乎都不起作用,因为所有配置都导致相同的问题。我如何解决not a managed type问题?

主类:

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);
    }
}

型号等级:

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;
    }
}

存储库:

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
}

服务内容:

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);
    }
    
}

错误:

java.lang.IllegalArgumentException: Not a managed type: class com.domain.example.model.Course
yqhsw0fo

yqhsw0fo1#

按照@xerx593的建议,将所有导入从javax.persistance更改为jakarta.persistance解决了该问题。

以前的型号类别:

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 ...
}

新模型类:

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 ...
}

相关问题