简体   繁体   English

为什么实体管理器在spring boot test父级中不为空,而在存储库中为空?

[英]Why is the entity manager not null in the spring boot test parent but it is null in the repository?

I've got the entity manager in the parent test class 我在上级考试班里有实体经理

@RunWith(SpringRunner.class)
@SpringBootTest(
    properties = {
            "spring.profiles.active=test",
            "spring.config.name=app-test"})
public abstract class ViewerTestBase extends DbBuilderImpl {

@Autowired EntityManager em;

The entity manager here is OK. 这里的实体管理器还可以。 DbBuilder sets up test data. DbBuilder设置测试数据。 In the @repository it is null 在@repository中,它为null

@Repository public class PaymentTransactionDao {
@Autowired EntityManager em;

Causing the test case to fail. 导致测试用例失败。

The entity manager is mapped to the h2 database for tests. 实体管理器已映射到h2数据库以进行测试。

The persistence config class is boiler plate 持久性配置类是样板

@Configration
@EnableTransactionManagement
public class PersistenceConfig {

  @Bean
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder) {
    LocalContainerEntityManagerFactoryBean em = builder.dataSource(viewerDataSource())
            .packages("viewer.model")
            .persistenceUnit("viewer")
            .build();
    return em;
  }

  @Bean
  public PlatformTransactionManager transactionManager(
        EntityManagerFactory viewerEntityManagerFactory) {
    return new JpaTransactionManager(pspEntityManagerFactory);
  }

  @Bean
  @Primary
  @ConfigurationProperties(prefix = "viewer.dbo.datasource")
  public DataSource viewerDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean
  @ConfigurationProperties(prefix = "viewer.auth.datasource")
  public DataSource authDataSource() {
    return DataSourceBuilder.create().build();
  }

Setup with spring boot starter jpa 使用Spring Boot Starter JPA进行设置

compile group: 'org.springframework.boot', name: 'spring-boot-starter-web'
compile("org.springframework.boot:spring-boot-devtools")
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'

testCompile group: 'junit', name: 'junit', version: '4.12'
testCompile group: 'com.h2database', name: 'h2'
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile group: 'org.springframework.boot', name: 'spring-boot-test'
testCompile group: 'org.springframework.boot', name: 'spring-boot-test-autoconfigure'

In order to put EntityManager within persistence context, change: 为了将EntityManager置于持久性上下文中,请更改:

@Autowired  
private EntityManager entityManager;

To

@PersistenceContext
private EntityManager entityManager;

A persistence context is a set of entity instances in which for any persistent entity identity there is a unique entity instance. 持久性上下文是一组实体实例,其中对于任何持久性实体标识,都有一个唯一的实体实例。 Within the persistence context, the entity instances and their lifecycle is managed by a particular entity manager. 在持久性上下文中,实体实例及其生命周期由特定的实体管理器管理。 The scope of this context can either be the transaction, or an extended unit of work. 此上下文的范围可以是事务,也可以是扩展的工作单元。

Official documentation hibernate definitions 官方文档休眠定义

Solved this through the use of constructor injection. 通过使用构造函数注入解决了这一问题。

Change @Repository constructor 更改@Repository构造函数

public class ViewItemDao {
  @PersistenceContext
  protected EntityManager em;
  public ViewItemDao(EntityManager em) {
    this.em = em;
  }

Change test. 更改测试。 Note that the entity manager was being injected into the test classes with just @RunWith(SpringRunner.class) and @SpringBootTest 请注意,仅使用@RunWith(SpringRunner.class)和@SpringBootTest将实体管理器注入到测试类中

@Test
public void testQueryId() throws InvalidSearchParameterException, SearchFailureException {
    generateTransaction("639051cc-4b19-4383-9c9a-89a80cd2a2f9");

    ViewItemDao viewItemDao = new ViewItemDao(em);

I did change @Autowired to @PersistenceContext without noticing a difference. 我确实将@Autowired更改为@PersistenceContext,而没有注意到差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM