user5657936
user5657936

Reputation:

When I use spring data to save an entity, the Repository returns a null pointer exception

I've been working with Spring Data with the JavaConfig documentation I have set up a Spring JPA configuration, but when calling the save method in my repository a null pointer exception is thrown.

this is the github project : https://github.com/Ibrahimhizeoui/HRessources.git My repository:

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

My class :

@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

The class which uses my repo :

public class EmployeeDao {

@Autowired
private EmployeeRepository employeeRepository;

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

}}

And this is my 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();
 }}

I am fairly new to Spring Data, so I do think it's something within the configuration that I am missing. Any advice would be greatly appreciated!

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

this is the github project : https://github.com/Ibrahimhizeoui/HRessources.git My repos

Upvotes: 1

Views: 5099

Answers (1)

Kirill Simonov
Kirill Simonov

Reputation: 8481

If you're calling saveEmployee from your Main.java main method, then of course it won't work because you have no spring context created and hence your EmployeeRepository is not autowired and equals to null. You can launch your application using Spring Boot (for example):

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

Also don't forget to add a @Component annotation to your DAO class:

@Component("dao")
public class EmployeeDao

Upvotes: 1

Related Questions