Kavita Sanap
Kavita Sanap

Reputation: 1

Spring-boot : Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.smart.entities.User

I am new to springboot, When trying to use the userRespository I set up I am getting this error " Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.smart.entities.User". I have tried the solutions provided previously for same question but none of them is working.

My Controller

package com.smart.controller;


@Controller
public class HomeController {

    @Autowired
    private UserRepository userRepository;

    // handler for registering user
    @RequestMapping(value = "/do_register", method = RequestMethod.POST)
    public String registerUser(@ModelAttribute("user") User user,
            @RequestParam(value = "agreement", defaultValue = "false") boolean agreement, Model model,
            HttpSession session) {
        try {
            if (!agreement) {
                throw new Exception("You have not agreed terms and conditions");
            }

            user.setRole("ROLE_USER");
            user.setEnabled(true);
            user.setImageUrl("default.png");

              this.userRepository.save(user);

            model.addAttribute("user", new User());
            session.setAttribute("message", new Message("Successfully Registered!! ", "alert - success"));

            System.out.println("Agreement " + agreement);
            System.out.println("User " + user);
            return "signup";
            
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("user", user);
            session.setAttribute("message", new Message("SomeThing went wrong " + e.getMessage(), "alert - danger"));
            return "signup";
        }

    }

}

My Repository

    package com.smart.dao;
    
    @Repository
    public interface UserRepository extends JpaRepository<User, Integer> {
    
    }

My user class

    package com.smart.entities;
    
    
    @Entity
    @Table(name="USER")
    public class User {
        
    
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private int id;
        private String name;
    
        @Column(unique = true)
        private String email;
        private String password;
        private String role;
        private Boolean enabled;
        private String imageUrl;
        
        @Column(length = 500)
        private String about;
        
        @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "user")
        private List<Contact> contacts = new ArrayList<>();
        
        public List<Contact> getContacts() {
            return contacts;
        }
    
        public void setContacts(List<Contact> contacts) {
            this.contacts = contacts;
        }
    
        public User() {
            super();
            
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getEmail() {
            return email;
        }
    
        public void setEmail(String email) {
            this.email = email;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public String getRole() {
            return role;
        }
    
        public void setRole(String role) {
            this.role = role;
        }
    
        public Boolean getEnabled() {
            return enabled;
        }
    
        public void setEnabled(Boolean enabled) {
            this.enabled = enabled;
        }
    
        public String getImageUrl() {
            return imageUrl;
        }
    
        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }
    
        public String getAbout() {
            return about;
        }
    
        public void setAbout(String about) {
            this.about = about;
        }
        
        
    
    }

I have tried annotation like ComponentScan(base.package.*),

@EnableJpaRepositories

Upvotes: 0

Views: 890

Answers (1)

Simon Martinelli
Simon Martinelli

Reputation: 36223

Spring Boot scans all classes in the package structure on the same level and below where the class with the @SpringBootApplication annotation is.

To avoid problems and reduce configuration, make sure you do so.

In your case the @SpringBootApplication annotated class must be in the package com.smart

Upvotes: 1

Related Questions