如何修复错误:引起:java.lang.IllegalArgumentException:不是托管类型:class com.example.demo.data.MyObj?

问题描述 投票:0回答:1

有人解决这个问题了吗? “不是托管类型”。

听起来很简单,但令人烦恼的是它已经在扫描包层次结构中定义了。

我想要完成的细节如下:

目标

我正在尝试在我的演示项目中运行 JPA 的单元测试 (JUnit5)。这是代码:

pom.xml:

<?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.2.0</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-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>javax.persistence-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

com.example.demo.DemoApplication:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication
@EntityScan("com.example.demo")

public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

com.example.demo.domain.MyObj:

package com.example.demo.domain;

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

@Entity
public class MyObj {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String message;
}

com.example.demo.domain.MyObjRepository:

package com.example.demo.domain;

import org.springframework.data.jpa.repository.JpaRepository;

//@Repository
public interface MyObjRepository extends JpaRepository<MyObj, Long> {
}

com.example.demo.DemoApplicationTests:

package com.example.demo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class DemoApplicationTests {

    @Test
    void contextLoads() {
    }

}

问题

运行测试时

mvn clean test
我收到此错误,该错误停止了构建/测试:


[ERROR] com.example.demo.DemoApplicationTests.contextLoads -- Time elapsed: 0.004 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@16391278 testClass = com.example.demo.DemoApplicationTests, locations = [], classes = [com.example.demo.DemoApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@1b11171f, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@4b0d79fc, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@1ed6388a, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@55a147cc, org.springframework.boot.test.context.SpringBootTestAnnotation@129aca4d], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
.
.

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myObjRepository' defined in com.example.demo.domain.MyObjRepository defined in @EnableJpaRepositories declared on JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration: Not a managed type: class com.example.demo.domain.MyObj
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1775)
.
.


Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.example.demo.domain.MyObj
    at org.hibernate.metamodel.model.domain.internal.JpaMetamodelImpl.managedType(JpaMetamodelImpl.java:193)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:463)
    at org.hibernate.metamodel.model.domain.internal.MappingMetamodelImpl.managedType(MappingMetamodelImpl.java:97)
    at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:82)


到目前为止我做了什么

我尝试了以下线程,但没有成功:

Spring-boot:由java.lang.IllegalArgumentException引起:不是托管类型:类com.smart.entities.User - 代码日志 Spring-boot:引起:java.lang.IllegalArgumentException:不是托管类型:class com.smart.entities.User

hibernate - 创建名称为“XXX”的bean时出错:通过字段“XXX”表达的依赖关系不满足 - VoidCC 创建名称为“XXX”的bean时出错:通过字段“XXX”表达的依赖关系不满足

spring-boot spring-data-jpa junit5 hsqldb spring-boot-test
1个回答
0
投票

所以——问题是我正在使用

javax.persistence

更改为

jakarta.persistence
,问题就消失了。

我引用表格:

java - Spring boot 不是托管类型 - 代码日志 Spring boot 不是托管类型

“从堆栈跟踪来看,您正在使用 SPring Boot 3。判断 从你的实体来看,你正在搞乱你的依赖关系并且 包括不兼容的。注释应该来自 jakarta.persistence 包不是 javax.persistence。要么修复 注解或将 Spring Boot 降级到 2.7.5。 – M. Deinum 11 月 29 日, 2022 年 15:29"

© www.soinside.com 2019 - 2024. All rights reserved.