title: SpringBoot2 - jpa entity is not a managed type
body: <p>I have a class marked with <code>javax.persistence.Entity</code> which <code>SpringBoot</code> says is not a managed type. </p>
  
  <p>The class is as follows</p>
  
  <pre><code>@Entity
  @Table(name="users")
  public class User {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private long id;
  
  private String name;
  
  @Column(unique = true)
  private String username;
  
  ...
  </code></pre>
  
  <p>UserRepository.java </p>
  
  <pre><code>public interface UserRepository extends CrudRepository&lt;User, Long&gt; {
  
      User findByUsername(String username);
  
      List&lt;User&gt; findByName(String name);
  
      @Query("UPDATE AppUser u SET u.lastLogin=:lastLogin WHERE u.username = ?#{ principal?.username }")
      @Modifying
      @Transactional
      public void updateLastLogin(@Param("lastLogin") Date lastLogin);
  
  }
  </code></pre>
  
  <p>AuthenticationSuccessHandler.java</p>
  
  <pre><code>@Component
  public class AuthenticationSuccessHandlerImpl implements AuthenticationSuccessHandler {
  
      @Autowired
      private UserRepository userRepository;
  
      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
          Authentication authentication) throws IOException, ServletException {
          userRepository.updateLastLogin(new Date());
      }
  
  }
  </code></pre>
  
  <p>and SpringSecurityConfig.java </p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @ComponentScan("com.bae.dbauth.security")
  public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private WebApplicationContext applicationContext;
      private CustomUserDetailsService userDetailsService;
      @Autowired
      private AuthenticationSuccessHandlerImpl successHandler;
      @Autowired
      private DataSource dataSource;
  
      @PostConstruct
      public void completeSetup() {
          userDetailsService = applicationContext.getBean(CustomUserDetailsService.class);
      }
  
      @Override
      protected void configure(final AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService)
              .passwordEncoder(encoder())
              .and()
              .authenticationProvider(authenticationProvider())
              .jdbcAuthentication()
              .dataSource(dataSource);
      }
  
      @Override
      public void configure(WebSecurity web) throws Exception {
          web.ignoring()
          .antMatchers("/resources/**");
      }
  
      @Override
      protected void configure(final HttpSecurity http) throws Exception {
          http.authorizeRequests()
              .antMatchers("/login")
              .permitAll()
              .and()
              .formLogin()
              .permitAll()
              .successHandler(successHandler)
              .and()
              .csrf()
              .disable();
      }
  
      ....
  
  }
  </code></pre>
  
  <p>And the application class...</p>
  
  <pre><code>@SpringBootApplication
  @PropertySource("classpath:persistence-h2.properties")
  @EnableJpaRepositories(basePackages = { "com.bae.dbauth.repositories" })
  @EnableWebMvc
  @Import(SpringSecurityConfig.class)
  public class BaeDbauthApplication implements WebMvcConfigurer {
  
      @Autowired
      private Environment env;
  
      @Bean
      public DataSource dataSource() {
          final DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(env.getProperty("driverClassName"));
          dataSource.setUrl(env.getProperty("url"));
          dataSource.setUsername(env.getProperty("user"));
          dataSource.setPassword(env.getProperty("password"));
          return dataSource;
      }
  
      @Bean
      public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
          final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] { "com.bae.dbauth.models" });
          em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
          em.setJpaProperties(additionalProperties());
          return em;
      }
  
      public static void main(String[] args) {
          SpringApplication.run(BaeDbauthApplication.class, args);
      }
  
  }
  </code></pre>
  
  <p>When I run the application I get quite long an error message which includes <code>Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.bae.dbauth.model.User</code></p>
  
  <p>The whole stack trace is quite extensive, it starts with:</p>
  
  <pre><code>org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springSecurityConfig': Unsatisfied dependency expressed through field 'successHandler'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authenticationSuccessHandlerImpl': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.bae.dbauth.model.User
  </code></pre>
  
  <p>And I don't understand what is the problem with the User class.</p>
  
  <p><strong>UPDATE:</strong>
  I added/modified annotations in <code>SpringSecurityCongig.java</code> and tried </p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @ComponentScan({"com.bae.dbauth.security", "com.bae.dbauth.model"})
  </code></pre>
  
  <p>and</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @ComponentScan("com.bae.dbauth.security")
  @EntityScan(basePackages = {"com.bae.dbauth.model"})
  </code></pre>
  
  <p>where <code>com.bae.dbauth.model</code> is package where the <code>User</code> entity is while <code>com.bae.dbauth</code> is the package where <code>SpringSecurityConfig.java</code> and the main <code>application</code> class are.</p>
  
  <p>The result is the same in each case - the same error message.</p>