简体   繁体   English

java.lang.IllegalArgumentException:不是托管类型:类 com.SportyShoe.Entity.Shoe

[英]java.lang.IllegalArgumentException: Not a managed type: class com.SportyShoe.Entity.Shoe

I am new to spring and spring boot.我是 spring 和 spring boot 的新手。 I tried to build a project by following an example I found here: http://www.javaguides.net/2018/09/spring-mvc-using-spring-boot2-jsp-jpa-hibernate5-mysql-example.html .我尝试按照我在此处找到的示例构建项目: http ://www.javaguides.net/2018/09/spring-mvc-using-spring-boot2-jsp-jpa-hibernate5-mysql-example.html。

Here is my Application:这是我的申请:

package com.SportyShoe;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;


@ComponentScan(basePackages = "com.SportyShoe")
@SpringBootApplication
@EntityScan("com.SportyShoe.*")
@EnableJpaRepositories
public class SportyShoeApplication {
    

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

}

Here is my Entity:这是我的实体:

package com.SportyShoe.Entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="Shoe")
public class Shoe {
    
    @Id
    @Column(name="id")
    private String id;
    

    @Column(name="colour")
    private String colour;
    
    @Column(name="gender")
    private String gender;
    
    @Column(name="category")
    private String category;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }
    

}

Here is my Repository:这是我的存储库:

package com.SportyShoe.repositories;

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

import com.SportyShoe.Entity.Shoe;


@Repository
public interface  ShoeRepositories extends JpaRepository<Shoe, Integer>{

}

Here is my Controller:这是我的控制器:

package com.SportyShoe.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.SportyShoe.repositories.ShoeRepositories;

@Controller
public class ShoeController {
    
    @Autowired
    ShoeRepositories shoeRepo;
    
    @RequestMapping("/shoes")
    public String shoeList(Model model) {
         model.addAttribute("shoes", shoeRepo.findAll());
         return "shoes";
    }

}

Here is my application.properties:这是我的 application.properties:

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

logging.level.org.springframework=INFO

################### DataSource Configuration ##########################
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/Sporty_Shoes
spring.datasource.username=root
spring.datasource.password=MPword@123

################### Hibernate Configuration ##########################

spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

When I reached this point in the example, it was written that running the Application will create the table in the database but all I got was an error as mentioned in the title.当我在示例中达到这一点时,上面写着运行应用程序将在数据库中创建表,但我得到的只是标题中提到的错误。

What should do now to make it work?现在应该怎么做才能让它发挥作用?

Your problem lies inside the application file您的问题出在应用程序文件中

@EntityScan("com.netsurfingzone.*")

put here your own package name.在此处输入您自己的 package 名称。 com.SportyShoe.*

You have String ID in your Shoe class, but you've created an repository interface of JpaRepository<Shoe, Integer> instead of JpaRepository<Shoe, String> .您的Shoe class 中有字符串 ID,但是您创建了JpaRepository<Shoe, Integer>而不是JpaRepository<Shoe, String>的存储库接口。 So I suggest to define Integer ID in your Shoe class to match the repository.所以我建议在您的Shoe class 中定义 Integer ID 以匹配存储库。

Also the problem may be in a package definition - the javadoc suggests to use base package name like "com.SportyShoe", instead of "com.SportyShoe.*".此外,问题可能出在 package 定义中 - javadoc 建议使用基本 package 名称,例如“com.SportyShoe”,而不是“com.SportyShoe.*”。
Also you may try to use type-safe entity scan like this:您也可以尝试像这样使用类型安全的实体扫描:

@EntityScan(basePackageClasses = Shoe.class)

or like this if you have multiple entities:如果您有多个实体,或者像这样:

@EntityScan(basePackageClasses = {Shoe.class, Lace.class})

Also try to remove @EntityScan , @ComponentScan and @EnableJpaRepositories - spring-boot tries to find entities and components in and under the package where you have @SpringBootApplication annotation by default (and jpa repositories, if you have a dependency on the classpath). Also try to remove @EntityScan , @ComponentScan and @EnableJpaRepositories - spring-boot tries to find entities and components in and under the package where you have @SpringBootApplication annotation by default (and jpa repositories, if you have a dependency on the classpath). These annotation may be used for extra configuration.这些注解可用于额外的配置。
See information on this in the reference documentation .请参阅参考文档中的相关信息

The main problem was the version of Spring boot I used.主要问题是我使用的 Spring 引导版本。

When I used 2.7.2 instead of 3.0.0(SnapShot) which I originally used it started working.当我使用 2.7.2 而不是最初使用的 3.0.0(SnapShot) 时,它开始工作。

I guess this comment is the key.我想这个评论是关键。

When I used 2.7.2 instead of 3.0.0(SnapShot) which I originally used it started working.当我使用 2.7.2 而不是我最初使用的 3.0.0(SnapShot) 时,它开始工作了。

Reading the documentation we realize the Spring Boot JPA module part of the Spring Boot 3 release turned to work with Jakarta Persistence API (JPA) rather than with javax.persistence.api .阅读文档,我们意识到Spring Boot 3版本的Spring Boot JPA模块部分转向使用Jakarta Persistence API (JPA)而不是javax.persistence.api Because of this even configuring properly the Spring JPA annotations like @EntityScan it does not find the entities.因此,即使正确配置了像@EntityScan这样的Spring JPA注释,它也找不到实体。

When upgrading Spring Boot up to version 3, the Persistence API artifact must be also migrated.Spring Boot升级到版本 3 时,还必须迁移Persistence API工件。

For more context about this change, it's well explained in this other SO thread.有关此更改的更多上下文,在其他 SO 线程中对此进行了很好的解释。

Hope it helps anyone else!希望它能帮助其他人!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalArgumentException:不是托管类型:class - java.lang.IllegalArgumentException: Not a managed type: class java.lang.IllegalArgumentException:不是托管类型:class com.modernmail.model.Group - java.lang.IllegalArgumentException: Not a managed type: class com.modernmail.model.Group 当实体类和Spring数据存储库位于同一包中时,“ java.lang.IllegalArgumentException:不是托管类型…” - “java.lang.IllegalArgumentException: Not a managed type …” when entity class and Spring data repository in same package java.lang.IllegalArgumentException:不是托管类型:class models.User - java.lang.IllegalArgumentException: Not a managed type: class models.User java.lang.IllegalArgumentException:不是托管类型:classTestEntity - java.lang.IllegalArgumentException: Not an managed type: classTestEntity java.lang.IllegalArgumentException问题:不是托管类型 - Problem with java.lang.IllegalArgumentException: Not a managed type java.lang.IllegalArgumentException:不是托管类型: - java.lang.IllegalArgumentException: Not a managed type: JPA - java.lang.IllegalArgumentException:不是实体:类 - JPA - java.lang.IllegalArgumentException: Not an entity: class 原因:java.lang.IllegalArgumentException:不是托管类型:&原因:org.hibernate.AnnotationException:未为实体指定标识符: - Caused by: java.lang.IllegalArgumentException: Not a managed type: & Caused by: org.hibernate.AnnotationException: No identifier specified for entity: java.lang.IllegalArgumentException: Not a managed type With @Entity and @Repository Setup with Spring Boot 2 - java.lang.IllegalArgumentException: Not a managed type With @Entity and @Repository Setup with Spring Boot 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM