在我的应用程序中,我有这三个类
@Entity
@Indexed
public class CpamEntity {
@Id
private String externalId;
@Field
private String cgr;
@ElementCollection
private Set<AttachmentEntity> attachments;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "externalId")
private Set<DataEntity> data;
}
@Entity
@Indexed
public class DataEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String internalId;
@Field
private String number;
@JsonIgnore
@ManyToOne
@JoinColumn(name = "externalId", updatable = false)
private FrameworkEntity frameworkEntity;
}
下面的代码是将DataEntity(X)插入CpamEntity,然后通过IdChild获得所有附件CpamEntity并在其中插入相同的DataEntity(X)
问题是第一次插入没有发生,并且插入了附件CpamEntity
public void on(Event event) {
CpamEntity cpamEntity = getCpamEntity(event.externalId);
if (cpamEntity != null) {
Set<DataEntity> datas = cpamEntity.getData();
datas.add(new DataEntity(event.internalId, event.number));
cpamEntity.setData(datas);
repository.save(cpamEntity); // not saved
if (CollectionUtils.isNotEmpty(cpamEntity.getAttachments())) {
cpamEntity.getAttachments().forEach(attachment -> {
cpamRepositoryentity = repository.findByCgr(attachment.getIdChild());
if (entity != null) {
Set<Entity> dataEntities = entity.getData();
dataEntities.add(new DataEntity(event.internalId, event.number));
entity.setData(dataEntities);
cpamRepository.save(entity); // saved
}
});
}
}
}