我有一个如下所示的JPA实体具有OneToMany集合,当我在集合中提到的实体上使用save时,我期望flush将自动刷新所做的更改并填充集合,但是这没有发生,有没有办法强制冲洗。
@Entity
@Table(name = "data_clctn_instc")
public class DataCollectionInstance {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator ="data_clctn_instc_seq_gen")
@SequenceGenerator(name = "data_clctn_instc_seq_gen", sequenceName = "data_clctn_instc_seq", allocationSize = 1)
@Column(name = "id")
private Long id;
@ManyToOne(optional = false)
@JoinColumn(name = "data_clctn_tmplt_id")
@JsonBackReference
private DataCollectionTemplate dataCollectionTemplate;
@OneToMany(mappedBy = "dataCollectionInstance")
private Collection<DataCollectionInstanceEvent> events;
@OneToMany(mappedBy = "dataCollectionInstance")
private Collection<PostProcessingInstance> postProcessingInstances;
}
我的jpa存储库类
@Repository
public interface DataCollectionInstanceDao extends CrudRepository<DataCollectionInstance, Long> {
}
@RequestMapping(value = "/{template}/instance", method = RequestMethod.POST, consumes = {"application/json"})
@PreAuthorize("hasAuthority('" + SecurityFunctions.FN_CREATE_INSTANCE + "')")
@ResponseStatus(code = HttpStatus.CREATED)
public DataCollectionInstance createInstance(@PathVariable(TEMPLATE_PATH_VARIABLE) final String templateName,
@RequestBody DataCollectionEntity dataCollectionEntity) {
DataCollectionInstance dataCollectionInstance = dataCollectionInstanceService.createInstance(templateName, dataCollectionEntity);
return dataCollectionInstance;
}
public DataCollectionInstance createInstance(String templateName, DataCollectionEntity dataCollectionEntity) {
// get the template
Optional<DataCollectionTemplate> dataCollectionTemplateOptional = dataCollectionTemplateDao.findByName(templateName);
if (!dataCollectionTemplateOptional.isPresent()) {
throw new IllegalArgumentException("Invalid templateName");
}
DataCollectionTemplate dataCollectionTemplate = dataCollectionTemplateOptional.get();
DataCollectionInstance dataCollectionInstance = createInstance(dataCollectionTemplate, dataCollectionEntity);
createEvent(dataCollectionEntity, dataCollectionTemplate, dataCollectionInstance);
createProcessingInstance(dataCollectionInstance)
return dataCollectionInstance;
}
private void createProcessingInstance(DataCollectionInstance dataCollectionInstance) {
PostProcessingInstance pi = new PostProcessingInstance();
pi.setDataCollectionInstance(dataCollectionInstance);
pi.setCreatedBy(getUserName());
pi.setCreatedDate(new Date());
pi.setResourceName(execution);
pi.setResponse("rspo");
pi.setStatus("adf");
// save to DB
postProcessingInstanceDao.save(pi);
}
private DataCollectionInstanceEvent createEvent(DataCollectionEntity dataCollectionEntity,
HttpMethod eventStatus, DataCollectionInstance dataCollectionInstance) {
DataCollectionInstanceEvent dataCollectionInstanceEvent = new DataCollectionInstanceEvent();
dataCollectionInstanceEvent.setAction(eventStatus);
dataCollectionInstanceEvent.setCreatedBy(getUserName());
dataCollectionInstanceEvent.setData(dataCollectionEntity.getSubmitData().toString());
dataCollectionInstanceEvent.setDataCollectionInstance(dataCollectionInstance);
dataCollectionInstanceEventDao.save(dataCollectionInstanceEvent);
return dataCollectionInstanceEvent;
}
private DataCollectionInstance createInstance(DataCollectionTemplate dataCollectionTemplate, DataCollectionEntity dataCollectionEntity){
DataCollectionInstance dataCollectionInstance = new DataCollectionInstance();
dataCollectionInstance.setStatus(dataCollectionEntity.getStatus());
dataCollectionInstance.setDataCollectionTemplate(dataCollectionTemplate);
dataCollectionInstance.setSubmitData(dataCollectionEntity.getSubmitData().toString());
dataCollectionInstance.setCreatedBy(getUserName());
dataCollectionInstance.setCreatedDate(new Date());
dataCollectionInstance = dataCollectionInstanceDao.save(dataCollectionInstance);
return dataCollectionInstance;
}
我尝试过执行findById以便稍后获取DataCollectionInstance,但是没有运气。
我在做什么错?
答案 0 :(得分:0)
只需将cascadeType
添加到相关关系注释中即可。
例如,如果您想将DataCollectionInstanceEvent列表与DataCollectionInstance保持在一起。
相关属性应为:
@OneToMany(mappedBy = "dataCollectionInstance", cascade = CascadeType.ALL)
private Collection<DataCollectionInstanceEvent> events;