Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring数据Jpa测试即使在保存子项后也返回空列表_Spring_Hibernate_Jpa_Spring Data_Spring Data Jpa - Fatal编程技术网

Spring数据Jpa测试即使在保存子项后也返回空列表

Spring数据Jpa测试即使在保存子项后也返回空列表,spring,hibernate,jpa,spring-data,spring-data-jpa,Spring,Hibernate,Jpa,Spring Data,Spring Data Jpa,上述测试失败,因为game2.getPlayers()返回null 已经通过了,但不知道如何解决 上述代码中使用的方法flushAndClear为空,如下所示: @Test public void testAddPlayerToGame() { gameRepository.save(createTestGame()); Game game = gameRepository.findOne(1l); assertTrue(game.getId() > 0);

上述测试失败,因为
game2.getPlayers()返回null

已经通过了,但不知道如何解决

上述代码中使用的方法
flushAndClear
为空,如下所示:

@Test
public void testAddPlayerToGame() {
    gameRepository.save(createTestGame());
    Game game = gameRepository.findOne(1l);
    assertTrue(game.getId() > 0);
    Player p = new Player();
    p.setName("test 1");
    p.setGame(game);
    p.setChips(5000);
    assertTrue(p.getId() == null);
    playerRepository.saveAndFlush(p);
    assertTrue(p.getId() != null);

    flushAndClear();
    Game game2 = gameRepository.findOne(1l);
    assertEquals(1, game2.getPlayers().size());
}
非常感谢您的帮助

更新

游戏和玩家映射代码:

protected void flushAndClear() {
//        sessionFactory.getCurrentSession().flush();
//        sessionFactory.getCurrentSession().clear();
    }

您需要编写一个有效的FlushAndClear方法。如果不是
game2
,则不会为数据库加载,而是从内部缓存加载。如果未加载,则关系不会更新

@Configuration
@EnableJpaRepositories(basePackages = "com.nitinsurana.repos")
@EnableTransactionManagement
class TestDataConfig {

    @Bean(name = "transactionManager")
    @Autowired
    public PlatformTransactionManager getTransactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
        return jpaTransactionManager;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.nitinsurana.domain");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(getHibernateProperties());
        em.afterPropertiesSet();
        return em.getObject();
    }

//    @Bean
//    public EntityManager entityManager(HibernateEntityManagerFactory entityManagerFactory) {
//        HibernateEntityManager entityManager = (HibernateEntityManager) entityManagerFactory.createEntityManager();
//        entityManager.setFlushMode(FlushModeType.AUTO FlushMode.ALWAYS);
//        return entityManager;
//    }

    private Properties getHibernateProperties() {
        Properties prop = new Properties();
        prop.put("hibernate.show_sql", "false");
        prop.put("hibernate.hbm2ddl.auto", "create");
        prop.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        return prop;
    }

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.HSQL)
//                .addScript("classpath:com/bank/config/sql/schema.sql")
//                .addScript("classpath:com/bank/config/sql/test-data.sql")
                .build();
    }
}

什么是
flushAndClear()?这是测试用例的一种方法吗?-它真的做到了它的名字所说的吗如果flushAndClear是正确的,那么我认为问题与关系映射有关-请发布相关的代码片段。@Ralph我正在尝试将一个项目移动到
spring data
,它包含
flushAndClear
实现,到目前为止,该方法为空。用相关的代码片段更新了问题。如果为空,则不会执行任何操作,因此从最后一个findOne()检索的game2是之前存储在会话缓存中的一个,并且具有空玩家列表。所以你所看到的是预期的。让它真正刷新和清晰。@JBNizet是的,无法使用
spring data jpa
解决这个问题。在测试中插入EntityManager(使用
@PersistenceContext
注释),并对其调用flush()和clear()。调用refresh()的这种形式有什么不同在给定实体的EntityManager上?
@Configuration
@EnableJpaRepositories(basePackages = "com.nitinsurana.repos")
@EnableTransactionManagement
class TestDataConfig {

    @Bean(name = "transactionManager")
    @Autowired
    public PlatformTransactionManager getTransactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory);
        return jpaTransactionManager;
    }

    @Bean
    public EntityManagerFactory entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan("com.nitinsurana.domain");
        em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        em.setJpaProperties(getHibernateProperties());
        em.afterPropertiesSet();
        return em.getObject();
    }

//    @Bean
//    public EntityManager entityManager(HibernateEntityManagerFactory entityManagerFactory) {
//        HibernateEntityManager entityManager = (HibernateEntityManager) entityManagerFactory.createEntityManager();
//        entityManager.setFlushMode(FlushModeType.AUTO FlushMode.ALWAYS);
//        return entityManager;
//    }

    private Properties getHibernateProperties() {
        Properties prop = new Properties();
        prop.put("hibernate.show_sql", "false");
        prop.put("hibernate.hbm2ddl.auto", "create");
        prop.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
        return prop;
    }

    @Bean
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.HSQL)
//                .addScript("classpath:com/bank/config/sql/schema.sql")
//                .addScript("classpath:com/bank/config/sql/test-data.sql")
                .build();
    }
}
class TestXXX {

   @PersistenceContext
   private EntityManager em.

   private flushAndClear() {
      em.flush();
      em.clear();
   }

}