spring @Autowired a repository returns null

Jian Huang

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

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Spring Boot Autowired Repository null

From Dev

Spring @Autowired gives null in a @Repository bean

From Dev

Spring Boot JPA : Autowired JPA repository is null

From Dev

Autowired Repository null in Controller for a Spring Boot application

From Dev

Spring boot mongoDB autowired null repository

From Dev

Why is my Spring @Autowired field returns null?

From Dev

Spring boot @Autowired repository instance null when using spring security

From Dev

Spring Autowired null if not found

From Dev

Spring Boot Autowired null

From Dev

Spring @Autowired comes as null

From Dev

Spring - autowired fields are null

From Dev

Spring @Autowired field is null?

From Dev

Spring - autowired fields are null

From Dev

Spring autowired is null

From Dev

Autowired annotation returns null in AuthenticationSuccessHandler

From Dev

Spring data repository not injecting with @Mock in test case class returns null?

From Dev

Spring Neo4j - autowire repository returns null

From Dev

Spring Data JPA + OpenJPA @autowired repository field null in webapp but fine for junit

From Dev

Spring Data JPA + OpenJPA @autowired repository field null in webapp but fine for junit

From Dev

Mocked repository returns null

From Dev

Autowired is null and not working with Jersey + Spring

From Dev

Spring MVC Autowired null in Component

From Dev

Spring @Autowired(required = true) is null

From Dev

Jersey 2 + Spring: @Autowired is null

From Dev

Spring @Autowired not working and returning null

From Dev

Spring @Autowired(required = true) is null

From Dev

Spring @Autowired bean giving null

From Dev

Spring MVC - Autowired Repository NullPointerException in static context

From Dev

Spring @Autowired can not wire Jpa repository

Related Related

  1. 1

    Spring Boot Autowired Repository null

  2. 2

    Spring @Autowired gives null in a @Repository bean

  3. 3

    Spring Boot JPA : Autowired JPA repository is null

  4. 4

    Autowired Repository null in Controller for a Spring Boot application

  5. 5

    Spring boot mongoDB autowired null repository

  6. 6

    Why is my Spring @Autowired field returns null?

  7. 7

    Spring boot @Autowired repository instance null when using spring security

  8. 8

    Spring Autowired null if not found

  9. 9

    Spring Boot Autowired null

  10. 10

    Spring @Autowired comes as null

  11. 11

    Spring - autowired fields are null

  12. 12

    Spring @Autowired field is null?

  13. 13

    Spring - autowired fields are null

  14. 14

    Spring autowired is null

  15. 15

    Autowired annotation returns null in AuthenticationSuccessHandler

  16. 16

    Spring data repository not injecting with @Mock in test case class returns null?

  17. 17

    Spring Neo4j - autowire repository returns null

  18. 18

    Spring Data JPA + OpenJPA @autowired repository field null in webapp but fine for junit

  19. 19

    Spring Data JPA + OpenJPA @autowired repository field null in webapp but fine for junit

  20. 20

    Mocked repository returns null

  21. 21

    Autowired is null and not working with Jersey + Spring

  22. 22

    Spring MVC Autowired null in Component

  23. 23

    Spring @Autowired(required = true) is null

  24. 24

    Jersey 2 + Spring: @Autowired is null

  25. 25

    Spring @Autowired not working and returning null

  26. 26

    Spring @Autowired(required = true) is null

  27. 27

    Spring @Autowired bean giving null

  28. 28

    Spring MVC - Autowired Repository NullPointerException in static context

  29. 29

    Spring @Autowired can not wire Jpa repository

HotTag

Archive