2017-06-09 93 views
2

在一个单元测试,我试图删除通过Spring的CrudRepository从我的数据库中的条目,但它似乎像什么也没有发生。春CrudRepository删除()什么也不做

实体:

@Entity @Table(name = "FACTION") 
public class Faction implements Serializable 
{ 
    private static final long serialVersionUID = 1L; 

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "ID", columnDefinition = "int", nullable = false, unique = true) 
    private Integer id; 

    public Integer getId() 
    { 
     return this.id; 
    } 
} 

存储库:

public interface FactionDao extends CrudRepository<Faction, Integer> 
{ 

} 

我的测试类:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classes = Config.class) 
@Sql({ "/delete-testData.sql", "/insert-testData.sql" }) 
public class TestFactionDao 
{ 
    @Autowired 
    private FactionDao dao; 

    @Test 
    public void testDelete() 
    { 
     System.out.println(this.dao.findOne(1)); 
     this.dao.delete(1); 
     System.out.println(this.dao.findOne(1)); 
    } 
} 

Spring配置:

@Configuration 
@EnableJpaRepositories(basePackageClasses = Config.class) 
@EnableTransactionManagement 
public class Config 
{ 
    @Bean 
    public DataSource dataSource() 
    { 
     return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build(); 
    } 

    @Bean 
    public PlatformTransactionManager transactionManager(DataSource pDataSource) 
    { 
     return new DataSourceTransactionManager(pDataSource); 
    } 

    @Bean 
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource pDataSource) 
    { 
     HibernateJpaVendorAdapter tJpaVendorAdapter = new HibernateJpaVendorAdapter(); 
     tJpaVendorAdapter.setDatabase(Database.H2); 
     tJpaVendorAdapter.setGenerateDdl(true); 
     tJpaVendorAdapter.setShowSql(true); 
     LocalContainerEntityManagerFactoryBean tEntityManagerFactory = new LocalContainerEntityManagerFactoryBean(); 
     tEntityManagerFactory.setJpaVendorAdapter(tJpaVendorAdapter); 
     tEntityManagerFactory.setDataSource(pDataSource); 
     tEntityManagerFactory.setPackagesToScan(Config.class.getPackage().getName()); 
     return tEntityManagerFactory; 
    } 
} 

SQL脚本:

-- delete-testData.sql 

delete from FACTION; 

-- insert-testData.sql 

insert into FACTION (ID) values 
(1); 

下面是测试例中的控制台输出。正如你所看到的,不执行删除操作,我仍然可以阅读我刚刚删除的实体:

Hibernate: create table FACTION (ID int generated by default as identity, primary key (ID)) 
Hibernate: select faction0_.ID as ID1_3_0_ from FACTION faction0_ where faction0_.ID=? 
[email protected] 
Hibernate: select faction0_.ID as ID1_3_0_ from FACTION faction0_ where faction0_.ID=? 
Hibernate: select faction0_.ID as ID1_3_0_ from FACTION faction0_ where faction0_.ID=? 
[email protected] 

我试图annoting我的@Rollback(假)的测试方法,但它似乎并没有做任何区别。调用dao上的flush()会引发异常,表示没有挂起的更新。

回答

1

我测试你的代码,并且解决方案是将TransactionManager类型从PlatformTransactionManager更改为JpaTransactionManager。 (我认为,当函数结束,也许只有PlatformTransactionManager的承诺删除。)

因此,代码为:

Config.java

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

@Bean 
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource pDataSource) { 
    HibernateJpaVendorAdapter tJpaVendorAdapter = new HibernateJpaVendorAdapter(); 
    ... 
+1

这样做。我猜这个错误不会发生在生产中,因为我可以使用容器管理的事务。 – Iavra

0

必须为updatedelete操作添加交易。

public class FactionService { 
    @Autowired 
    private FactionDao dao; 

    @Transactional 
    public void delete(int id){ 
     dao.delete(id); 
    } 
} 
+0

如果我添加@Transactional的方法(无论是春季或持久性一)删除失败与“org.springframework.dao.EmptyResultDataAccessException:没有类de.iavra.data.Faction实体与id 1存在!”。它似乎没有找到我在该方法之前插入的测试数据。 我以为,春数据处理在仓库里的交易,所以我不必事务添加到我的方法。 – Iavra