Spring数据测试自定义存储库数据不会更新

时间:2014-05-07 10:42:29

标签: java hibernate spring-data querydsl

我正在尝试为自定义spring数据存储库编写测试。我也使用QueryDSL。 我是spring-data的新手。我在测试中使用Spring支持HSQL DB。 MySQL for dev。

问题:如果我使用自定义存储库,我在测试中看不到更新的数据。

public interface AuctionRepository extends AuctionRepositoryCustom, CrudRepository<Auction, Long>, QueryDslPredicateExecutor<Auction> {
// needed for spring data crud
}

public interface AuctionRepositoryCustom {
    long renameToBestName();
}

public class AuctionRepositoryImpl extends QueryDslRepositorySupport implements AuctionRepositoryCustom {
    private static final QAuction auction = QAuction.auction;

public AuctionRepositoryImpl() {
    super(Auction.class);
}

@Override
public long renameToBestName() {
    return update(auction)
        .set(auction.name, "BestName")
        .execute();
}
}

我的测试
不知何故在最后一行失败

public class CustomAuctionRepositoryImplTest extends AbstractIntegrationTest {
@Inject
AuctionRepository auctionRepository;

@Test
public void testDoSomething() {
    Auction auction = auctionRepository.findOne(26L);
    assertEquals("EmptyName", auction.getName());

    // test save
    auction.setName("TestingSave");
    auctionRepository.save(auction);
    Auction saveResult = auctionRepository.findOne(26L);
    assertEquals("TestingSave", saveResult.getName());

    // test custom repository
    long updatedRows = auctionRepository.renameToBestName();
    assertTrue(updatedRows > 0);
    Auction resultAuction = auctionRepository.findOne(26L);
    assertEquals("BestName", resultAuction.getName()); // FAILS expected:<[BestNam]e> but was:<[TestingSav]e>
}
}

我无法弄清楚在使用自定义存储库时数据无法更新的原因。如果我在开发模式下启动应用程序,并通过控制器调用renameToBestName(),一切都按预期工作,名称更改。

以下是测试配置(如果需要)

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@ActiveProfiles("test")
@ContextConfiguration(classes = {TestBeans.class, JpaConfig.class, EmbeddedDataSourceConfig.class})
@ComponentScan(basePackageClasses = IntegrationTest.class, excludeFilters = @Filter({Configuration.class}))
public abstract class AbstractIntegrationTest {
}

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackageClasses = Application.class)
class JpaConfig {

    @Value("${hibernate.dialect}")
    private String dialect;
    @Value("${hibernate.hbm2ddl.auto}")
    private String hbm2ddlAuto;
    @Value("${hibernate.isShowSQLOn}")
    private String isShowSQLOn;

    @Autowired
    private DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(dataSource);
        entityManagerFactory.setPackagesToScan("auction");
        entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());

        Properties jpaProperties = new Properties();
        jpaProperties.put(org.hibernate.cfg.Environment.DIALECT, dialect);
        if ( !hbm2ddlAuto.isEmpty()) {
            jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, hbm2ddlAuto);
        }
        jpaProperties.put(org.hibernate.cfg.Environment.SHOW_SQL, isShowSQLOn);
        jpaProperties.put(org.hibernate.cfg.Environment.HBM2DDL_IMPORT_FILES_SQL_EXTRACTOR, "org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor");
        entityManagerFactory.setJpaProperties(jpaProperties);

        return entityManagerFactory;
    }

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

1 个答案:

答案 0 :(得分:2)

这是因为通过您的代码发出的更新查询被定义为不会驱逐EntityManager中查询可能触及的对象。请在this answer中详细了解。