Java SpringBoot2-jpa实体不是托管类型
我有一个标有Java SpringBoot2-jpa实体不是托管类型,java,spring-boot,spring-data-jpa,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 usernam
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);
}
和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);
}
}
当我运行应用程序时,会收到一条很长的错误消息,其中包括由以下原因引起的:java.lang.IllegalArgumentException:非托管类型: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
我不明白用户类有什么问题
更新:
我在SpringSecurityCongig.java
中添加/修改了注释,并尝试了
@Configuration
@EnableWebSecurity
@ComponentScan({"com.bae.dbauth.security", "com.bae.dbauth.model"})
及
其中com.bae.dbauth.model
是用户
实体所在的包,com.bae.dbauth
是SpringSecurityConfig.java
和主应用程序
类所在的包
每种情况下的结果都是相同的-错误消息相同。首先尝试在
User
中将id
的类型更改为Long
,而不是Long
然后确保@ComponentScan
还包括包含实体的java包。您可以指定多个要扫描的包,请参阅。E示例:
@ComponentScan({"pkg1","pkg2"})
在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);
}
@存储库
公共接口UserRepository扩展了Crudepository{
用户findByUsername(字符串用户名);
列出findByName(字符串名称);
@查询(“更新AppUser u SET u.lastLogin=:lastLogin,其中u.username=?#{principal?.username}”)
@修改
@交易的
public void updateLastLogin(@Param(“lastLogin”)Date lastLogin);
}
未找到实体。如果已自动配置entity manager Factory,则有2个选项:
- 添加@EntityScan
- 将实体放在应用程序下的包中(这些包按约定扫描)
em.setPackagesToScan(new String[] { "com.bae.dbauth.models" });
当您的用户实体位于:com.bae.dbauth.model
这让我觉得这只是一个输入错误。完成,但仍然返回相同的错误消息。您的
用户
类和应用程序
类的包名是什么?
@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);
}
em.setPackagesToScan(new String[] { "com.bae.dbauth.models" });