java.lang.IllegalArgumentException: Not a managed type: class com.SportyShoe.Entity.Shoe
Asked Answered
U

4

4

I am new to spring and 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.

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:

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?

Undecided answered 12/8, 2022 at 8:10 Comment(4)
Your entity scan is pointing to different package than your entity. @EntityScan("com.netsurfingzone.*"), you might not need it as the entities are in sub package of main and have @EnableAutoConfigurationPalladous
@SapanVashishth, that was posted by mistake, I have already tried without EntityScan annotation and also with the EntityScan annotation with right package address (with * or without *, parent package or exact entity package does not help at all).Undecided
I suggest try with fresh project created from spring starter. I tried with your code and it does create the table in dbPalladous
If you are using spring boot 3.+, you'll encounter this issue.Shikari
U
3

The main problem was the version of Spring boot I used.

When I used 2.7.2 instead of 3.0.0(SnapShot) which I originally used it started working.

Undecided answered 12/8, 2022 at 16:11 Comment(0)
S
15

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.

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. Because of this even configuring properly the Spring JPA annotations like @EntityScan it does not find the entities.

When upgrading Spring Boot up to version 3, the Persistence API artifact must be also migrated.

For more context about this change, it's well explained in this other SO thread.

Hope it helps anyone else!

Spirketing answered 18/12, 2022 at 6:4 Comment(2)
Thanks for this useful tip! It is working after replace package form "javax" to "jakarta"!Muskmelon
Perfect information update. For anyone following the official Spring on using Spring Boot Rest - spring.io/guides/tutorials/rest Be aware of the import changes to your model class. This will prevent the managed class exception.Brynne
U
3

The main problem was the version of Spring boot I used.

When I used 2.7.2 instead of 3.0.0(SnapShot) which I originally used it started working.

Undecided answered 12/8, 2022 at 16:11 Comment(0)
E
2

My problem was : i was using spring boot 3.0.8 but using javax.* Replaved with jakarta.persistence.* API's and worked fine.

Electrify answered 7/9, 2023 at 5:35 Comment(0)
B
1

You have String ID in your Shoe class, but you've created an repository interface of JpaRepository<Shoe, Integer> instead of JpaRepository<Shoe, String>. So I suggest to define Integer ID in your Shoe class to match the repository.

Also the problem may be in a package definition - the javadoc suggests to use base package name like "com.SportyShoe", instead of "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). These annotation may be used for extra configuration.
See information on this in the reference documentation.

Brouhaha answered 12/8, 2022 at 13:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.