当我使用spring数据保存实体时,Repository返回空指针异常

时间:2017-12-20 23:52:41

标签: java spring jpa spring-data-jpa

我已经使用JavaConfig文档处理Spring Data我已经设置了Spring JPA配置,但是在我的存储库中调用save方法时会抛出空指针异常。

这是github项目:https://github.com/Ibrahimhizeoui/HRessources.git 我的存储库:

@Repository
public interface EmployeeRepository extends JpaRepository<Employee,Integer> 
{
}

我的课程:

@Entity
@Table
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String firstName;
private String lastName;
private Date created_at;
private Date updated_at;
public Employee() {}
 }
//getters & setters

使用我的仓库的类:

public class EmployeeDao {

@Autowired
private EmployeeRepository employeeRepository;

public void saveEmployee(Employee employee){
    employeeRepository.save(employee);

}}

这是我的Jpa Config:

@Configuration
@EnableJpaRepositories(basePackages="dao")
@EnableTransactionManagement
public class JpaConfiguration {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) {
LocalContainerEntityManagerFactoryBean emfb =
new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(dataSource);
emfb.setJpaVendorAdapter(jpaVendorAdapter);
emfb.setPackagesToScan("model");
return emfb;
}

@Bean(name = "dataSource")
public DriverManagerDataSource dataSource() {
    DriverManagerDataSource driverManagerDataSource = new 
DriverManagerDataSource();
    driverManagerDataSource.setDriverClassName("com.mysql.jdbc.Driver");

driverManagerDataSource.setUrl("jdbc:mysql://localhost:3306/hressource");
    driverManagerDataSource.setUsername("root");
    driverManagerDataSource.setPassword("");
    return driverManagerDataSource;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new 
    HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(true);
    hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
    return hibernateJpaVendorAdapter;
}

 @Bean
    public PersistenceExceptionTranslationPostProcessor 
  exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

 @Bean
 public BeanPostProcessor persistenceTranslation() {
 return new PersistenceExceptionTranslationPostProcessor();
 }}

我对Spring Data相当新,所以我认为这是我缺少的配置。任何建议将不胜感激!

Exception in thread "main" java.lang.NullPointerException
at dao.EmployeeDao.saveEmployee(EmployeeDao.java:16)
at Main.main(Main.java:10)

这是github项目:https://github.com/Ibrahimhizeoui/HRessources.git我的回购

1 个答案:

答案 0 :(得分:1)

如果您使用saveEmployee Main.java方法拨打main,那么它当然无效,因为您没有创建春季上下文,因此您的EmployeeRepository未自动装配且等于null。您可以使用Spring Boot启动应用程序(例如):

@ComponentScan
@EntityScan("model")
@EnableJpaRepositories("dao")
public class Main {

    /**
     * Run the application using Spring Boot and an embedded servlet engine.
     *
     * @param args program arguments - ignored.
     */
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Main.class, args);

        EmployeeDao employeeDao = context.getBean("dao", EmployeeDao.class);

        Employee employee = new Employee();
        employee.setFirstName("ib");
        employeeDao.saveEmployee(employee);
    }
}

另外,不要忘记在DAO类中添加@Component注释:

@Component("dao")
public class EmployeeDao