Description
when i try to persist an object with the help of the entity manager it seems that the entity manager is null, not instantiated even though i used the annotation (@PersistenceContext)!
any recommendations?
Entity Manager
@Repository
@Transactional
public class ImplEmployee implements EmployeeInterface{
@PersistenceContext
EntityManager manager;
@Override
public void save(Employee employee) {
manager.persist(employee);
}
//other methods...
Main
@SpringBootApplication
public class JpaSpringApplication {
public static void main(String[] args) {
SpringApplication.run(JpaSpringApplication.class, args);
Employee e = new Employee();
e.setId(4444);
e.setName("Nathan");
e.setSalary(543.87);
ImplEmployee i = new ImplEmployee();
i.save(e);
}
}
output
Exception in thread "main" java.lang.NullPointerException: Cannot invoke
"javax.persistence.EntityManager.persist(Object)" because "this.manager" is null
at com.spring.jpa.demo.implemployee.ImplEmployee.save(ImplEmployee.java:25)
at com.spring.jpa.demo.main.JpaSpringApplication.main(JpaSpringApplication.java:19)