SpringBoot2-JPA实体不是托管类型

时间:2019-06-15 07:39:51

标签: java spring-boot spring-data-jpa

我有一个标有javax.persistence.Entity的类,SpringBoot说这不是托管类型。

该类如下

@Entity
@Table(name="users")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;

private String name;

@Column(unique = true)
private String username;

...

UserRepository.java

public interface UserRepository extends CrudRepository<User, Long> {

    User findByUsername(String username);

    List<User> 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);

}

AuthenticationSuccessHandler.java

@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());
    }

}

和SpringSecurityConfig.java

@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();
    }

    ....

}

还有应用程序类...

@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);
    }

}

运行该应用程序时,我收到很长的错误消息,其中包含Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.bae.dbauth.model.User

整个堆栈跟踪非常广泛,它始于:

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

我不了解User类的问题。

更新: 我在SpringSecurityCongig.java中添加/修改了注释,并尝试了

@Configuration
@EnableWebSecurity
@ComponentScan({"com.bae.dbauth.security", "com.bae.dbauth.model"})

@Configuration
@EnableWebSecurity
@ComponentScan("com.bae.dbauth.security")
@EntityScan(basePackages = {"com.bae.dbauth.model"})

其中com.bae.dbauth.modelUser实体所在的包,而com.bae.dbauthSpringSecurityConfig.java和主application类所在的包。

结果在每种情况下都是相同的-相同的错误消息。

3 个答案:

答案 0 :(得分:0)

首先尝试将User的类型从id改为Long

然后确保long还包括包含实体的java包。您可以指定要扫描的多个软件包,请参见Spring docs。足够:

@ComponentScan

答案 1 :(得分:0)

在UserRepository中添加注释@Repository

@Repository
public interface UserRepository extends CrudRepository<User, Long> {

     User findByUsername(String username);
     List<User> 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);
}

答案 2 :(得分:0)

未发现实体。如果您的实体管理器在出厂时已自动配置,则有2个选项:

  • 添加@EntityScan
  • 将实体放置在应用程序下的程序包中(这些程序包按约定扫描)

我可以看到您自己配置了实体管理器工厂。

em.setPackagesToScan(new String[] { "com.bae.dbauth.models" });

您的用户实体位于:com.bae.dbauth.model

这让我觉得这只是一个错字。