我有三张表,关系如下: ALD_BATCH -> ALD_BATCH_EVENT -> ALD_BATCH_ITEM 从左向右均是一对多关系。 插入测试数据如下:
创建entity:
@Entity
@Table ( name ="ald_batch" )
public class AldBatch implements Serializable {
private static final long serialVersionUID = 2730972389585980577L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "batch_id" )
private Long batchId;
@Column(name = "created_dt" )
private Date createdDt;
@Column(name = "modified_dt" )
private Date modifiedDt;
@Column(name = "batch_type_id" )
private Long batchTypeId;
@Column(name = "batch_c" )
private String batchC;
@Column(name = "remark_x" )
private String remarkX;
@OneToMany(mappedBy = "aldBatch",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<AldBatchEvent> aldBatchEventList;
@Entity
@Table ( name ="ald_batch_event" )
public class AldBatchEvent implements Serializable {
private static final long serialVersionUID = 2544903594305061336L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "batch_event_id" )
private Long batchEventId;
@Column(name = "event_cfg_id" )
private Long eventCfgId;
/**
* 配置联系人到客户的多对一关系
* 使用注解的形式配置多对一关系
* 1.配置表关系
* @ManyToOne : 配置多对一关系
* targetEntity:对方的实体类字节码
* 2.配置外键(中间表)
*
* * 配置外键的过程,配置到了多的一方,就会在多的一方维护外键
*
*/
@ManyToOne(targetEntity = AldBatch.class,fetch = FetchType.LAZY)
@JoinColumn(name = "batch_id",referencedColumnName = "batch_id")
private AldBatch aldBatch;
@OneToMany(mappedBy = "aldBatchEvent",cascade = CascadeType.ALL,fetch = FetchType.LAZY)
private List<AldBatchItem> aldBatchItemList;
然后正常创建了对应的 dao接口继承了 JpaRepository 和 JpaSpecificationExecutor
现在想做的测试:
- 根据ID搜索出batch数据,然后删除第一个event。 这只是个简单的测试目的,没有什么业务需求。 然后在Junit 进行单元测试。
@Test
@Transactional
@Rollback(value = false)
public void testSearchAndDeleteEvent(){
AldBatch batch = batchDao.findById(7l).get();
System.out.println(batch.getAldBatchEventList());
AldBatchEvent event = batch.getAldBatchEventList().get(0);
batchEventDao.delete(event);
System.out.println(batch.getAldBatchEventList());
}
为何delete 语句没有打印,数据库也没有执行。 log:
2021-07-01 13:58:47.390 INFO 1564 --- [ Test worker] c.ald.erp.dao.cfg.price.AldBatchDaoTest : Started AldBatchDaoTest in 23.757 seconds (JVM running for 29.085)
2021-07-01 13:58:47.766 INFO 1564 --- [ Test worker] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [DefaultTestContext@25d83e1e testClass = AldBatchDaoTest, testInstance = com.ald.erp.dao.cfg.price.AldBatchDaoTest@5b9541d1, testMethod = testSearchAndDeleteEvent@AldBatchDaoTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3963a64e testClass = AldBatchDaoTest, locations = '{}', classes = '{class com.ald.erp.AldErpApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1daa2e8e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@bbda85e, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@2796efc0, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@247db848, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@4980b8df, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5fbe86], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]; transaction manager [org.springframework.orm.jpa.JpaTransactionManager@78756cc9]; rollback [false]
2021-07-01 13:58:48.499 DEBUG 1564 --- [ Test worker] org.hibernate.SQL : select aldbatch0_.batch_id as batch_id1_0_0_, aldbatch0_.batch_c as batch_c2_0_0_, aldbatch0_.batch_type_id as batch_ty3_0_0_, aldbatch0_.created_dt as created_4_0_0_, aldbatch0_.modified_dt as modified5_0_0_, aldbatch0_.remark_x as remark_x6_0_0_ from ald_batch aldbatch0_ where aldbatch0_.batch_id=?
2021-07-01 13:58:49.379 TRACE 1564 --- [ Test worker] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [7]
2021-07-01 13:58:49.547 DEBUG 1564 --- [ Test worker] org.hibernate.SQL : select aldbatchev0_.batch_id as batch_id3_1_0_, aldbatchev0_.batch_event_id as batch_ev1_1_0_, aldbatchev0_.batch_event_id as batch_ev1_1_1_, aldbatchev0_.batch_id as batch_id3_1_1_, aldbatchev0_.event_cfg_id as event_cf2_1_1_ from ald_batch_event aldbatchev0_ where aldbatchev0_.batch_id=?
2021-07-01 13:58:49.550 TRACE 1564 --- [ Test worker] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [7]
[AldBatchEvent{batchEventId=3, eventCfgId=1, aldBatch=AldBatch{batchId=7, createdDt=2021-07-01 00:00:00.0, modifiedDt=2021-07-01 00:00:00.0, batchTypeId=1, batchC='ALD_P_20210701_001', remarkX='null'}}, AldBatchEvent{batchEventId=4, eventCfgId=2, aldBatch=AldBatch{batchId=7, createdDt=2021-07-01 00:00:00.0, modifiedDt=2021-07-01 00:00:00.0, batchTypeId=1, batchC='ALD_P_20210701_001', remarkX='null'}}]
2021-07-01 13:58:49.612 DEBUG 1564 --- [ Test worker] org.hibernate.SQL : select aldbatchit0_.batch_event_id as batch_ev4_2_0_, aldbatchit0_.batch_item_id as batch_it1_2_0_, aldbatchit0_.batch_item_id as batch_it1_2_1_, aldbatchit0_.batch_event_id as batch_ev4_2_1_, aldbatchit0_.item_cfg_id as item_cfg2_2_1_, aldbatchit0_.price_a as price_a3_2_1_ from ald_batch_item aldbatchit0_ where aldbatchit0_.batch_event_id=?
2021-07-01 13:58:49.615 TRACE 1564 --- [ Test worker] o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [BIGINT] - [3]
[AldBatchEvent{batchEventId=3, eventCfgId=1, aldBatch=AldBatch{batchId=7, createdDt=2021-07-01 00:00:00.0, modifiedDt=2021-07-01 00:00:00.0, batchTypeId=1, batchC='ALD_P_20210701_001', remarkX='null'}}, AldBatchEvent{batchEventId=4, eventCfgId=2, aldBatch=AldBatch{batchId=7, createdDt=2021-07-01 00:00:00.0, modifiedDt=2021-07-01 00:00:00.0, batchTypeId=1, batchC='ALD_P_20210701_001', remarkX='null'}}]
2021-07-01 13:58:49.748 INFO 1564 --- [ Test worker] o.s.t.c.transaction.TransactionContext : Committed transaction for test: [DefaultTestContext@25d83e1e testClass = AldBatchDaoTest, testInstance = com.ald.erp.dao.cfg.price.AldBatchDaoTest@5b9541d1, testMethod = testSearchAndDeleteEvent@AldBatchDaoTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3963a64e testClass = AldBatchDaoTest, locations = '{}', classes = '{class com.ald.erp.AldErpApplication}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1daa2e8e, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@bbda85e, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@2796efc0, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@247db848, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@4980b8df, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5fbe86], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.populatedRequestContextHolder' -> true, 'org.springframework.test.context.web.ServletTestExecutionListener.resetRequestContextHolder' -> true, 'org.springframework.test.context.event.ApplicationEventsTestExecutionListener.recordApplicationEvents' -> false]]
2021-07-01 13:58:49.872 INFO 1564 --- [ionShutdownHook] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
也做过这样的测试也不好用,但是应该是因为关系维护交给了子表所以不好用。
@Test
@Transactional
@Rollback(value = false)
public void testSearchAndDeleteBatch(){
AldBatch batch = batchDao.findById(7l).get();
System.out.println(batch.getAldBatchEventList());
batch.getAldBatchEventList().remove(0);
System.out.println(batch.getAldBatchEventList());
batchDao.save(batch);
}
最终尝试手动删除,没有问题
@Test
@Transactional
@Rollback(value = false)
public void testDeleteEvent(){
batchEventDao.deleteById(3l);
}
不知道原因,为什么通过主表搜索子表,然后用子表的dao进行删除不好用。