我是春天和jpa的新手。我搜索了类似的主题,仍然无法解决我的问题。我试图在我的测试文件中自动装配我的EmployeeRepository(Imp),但它总是返回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 :(得分:0)
您不需要EmployeeRepository的具体实现。注释掉该类并自动装配EmployeeRepository。
此外,您不需要在测试中使用@Controller注释。
答案 1 :(得分:0)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=AppConfig.class, loader = AnnotationConfigContextLoader.class)
public class RepositoryTest {...}
同样在AppConfig.java中需要有以下注释:
@Configuration
@EnableJpaRepositories
public class AppConfig {...}
最后,似乎hibernate ogm与jparepository不兼容。还在学习......