I am new to spring and jpa. I have searched the similar topics and still not able to solve my issue. I tried to autowire my EmployeeRepository(Imp) in my test file, but it always return null... All the code is under the same package. Thanks a lot for your time.
Another question is that which one I should use (I had tried both with no luck)
@Autowired
private EmployeeRepositoryImp er;
and
@Autowired
private EmployeeRepository er;
Below is my code...
package com.rw.examples.hibernate_ogm_neo4j;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public EntityManager entityManager() {
return entityManagerFactory().createEntityManager();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
return Persistence.createEntityManagerFactory("ogm-neo4j");
}
}
package com.rw.examples.hibernate_ogm_neo4j;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface EmployeeRepository extends JpaRepository<Employee, Long>{
}
package com.rw.examples.hibernate_ogm_neo4j;
import javax.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public class EmployeeRepositoryImp extends SimpleJpaRepository<Employee, Long> implements EmployeeRepository{
private EntityManager entityManager;
@Autowired
public EmployeeRepositoryImp(Class<Employee> domainClass, EntityManager em) {
super(domainClass, em);
// TODO Auto-generated constructor stub
this.entityManager = em;
}
}
package com.rw.examples.hibernate_ogm_neo4j;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.stereotype.Controller;
@Controller
@EnableJpaRepositories (basePackages = {"com.rw.examples.hibernate_ogm_neo4j"})
@ComponentScan(basePackages = {"com.rw.examples.hibernate_ogm_neo4j"})
public class RepositoryTest {
@Autowired
private EmployeeRepositoryImp er;
@Test
public void testRepository() {
ApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);
EntityManagerFactory emf = ctx.getBean(EntityManagerFactory.class);
EntityManager em = emf.createEntityManager();
System.out.println("testRepository");
er.save(new Employee("Frank"));
System.out.println("list employees using repository");
Iterable<Employee> employees = er.findAll();
employees.forEach(e->System.out.println(e.toString()));
}
}
finally solved the problem. To run JUNIT test, need to use @RunWith.
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class RepositoryTest {...}
And also in the AppConfig.java need to have the following annotation:
@Configuration
@EnableJpaRepositories
public class AppConfig {...}
finally, it seems like hibernate ogm doesn't work well with jparepository. still studying...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments