2016-12-24 97 views
0

我是spring和jpa的新手。我搜索了类似的话题,但仍然无法解决我的问题。我尝试在我的测试文件中自动装入我的EmployeeRepository(Imp),但它总是返回null ...所有代码都在同一个包中。非常感谢您的时间。spring @Autowired存储库返回null

另一个问题是,我应该使用哪一个(我没有运气都尝试)

@Autowired 
    private EmployeeRepositoryImp er; 

@Autowired 
    private EmployeeRepository er; 

下面是我的代码...

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

回答

0

终于解决了这个问题。要运行JUNIT测试,需要使用@RunWith。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes=AppConfig.class, loader = AnnotationConfigContextLoader.class) 
public class RepositoryTest {...} 

而且也在AppConfig.java需要有以下标注:

@Configuration 
@EnableJpaRepositories 
public class AppConfig {...} 

最后,好像休眠OGM不jparepository很好地工作。仍在研究...

+0

如果你使用Spring,那么我建议你试试[Spring Data Neo4j](https://github.com/spring-projects/spring-data-neo4j)。它受到Neo4j和Spring背后的开发者的支持。 – digx1

0

您不需要EmployeeRepository的具体实现。注释掉该类并自动装入EmployeeRepository。

此外,您不需要测试上的@Controller注释。

+0

谢谢。我试过你的建议,但它仍然不工作... –

+0

EmployeeRepository为null,因为spring无法执行依赖注入,这可能是组件扫描的许多原因,而不是扫描所有包或嵌套的依赖关系失败的原因。我建议使用基于maven的目录结构,并使用SpringBootTest来测试您的存储库。 –