我尝试连接JpaRepository以使用Hibernate。 但我不能像这样自动发送它。 我看到错误:无法自动装配字段JpaUserRepository。
@Autowired
private JpaUserRepository jpaUserRepository;
我的存储库:
public interface JpaUserRepository extends JpaRepository<User, Integer> {
}
我错过了什么?
我的HibernateConfig:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.springapp.mvc.repositories")
public class HibernateConfig {
@Bean
public DriverManagerDataSource dataSource(){
DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource();
driverManagerDataSource.setDriverClassName("org.postgresql.Driver");
driverManagerDataSource.setUrl("jdbc:postgresql://localhost:5432");
driverManagerDataSource.setUsername("postgres");
driverManagerDataSource.setPassword("password");
return driverManagerDataSource;
}
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource());
factoryBean.setPersistenceUnitName("jpaData");
factoryBean.setJpaVendorAdapter(hibernateJpaVendorAdapter());
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
properties.put("hibernate.show_sql", "true");
properties.put("hibernate.format_sql", "true");
properties.put("hibernate.hbm2ddl.auto", "update");
factoryBean.setJpaProperties(properties);
factoryBean.setPackagesToScan(new String[]{"com.springapp.mvc.models"});
return factoryBean;
}
@Bean
public JpaTransactionManager transactionManager(){
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return jpaTransactionManager;
}
@Bean
public HibernateJpaVendorAdapter hibernateJpaVendorAdapter(){
return new HibernateJpaVendorAdapter();
}
}