问题
For example, I have a bidirectional one-to-one relationship:
public class Document {
@OneToOne(mappedBy = "document", cascade = CascadeType.ALL)
private DocumentMetadata documentMetadata;
}
public class DocumentMetadata {
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "document_id")
private Document document;
}
When I save the Document
entity by using Spring Data, it also saves the DocumentMetadata
entity:
DocumentMetadata documentMetadata = DocumentMetadata.builder()
/*some fields are set here*/
.build();
Document document = Document.builder()
/*some fields are set here*/
.documentMetadata(documentMetadata)
.build();
documentMetadata.setDocument(document);
documentRegistry.save(document);
I noticed that the advice triggered only before saving the Document
entity, but for owner side it doesn't work. I use the following pointcut and advice:
@Pointcut("execution(* org.springframework.data.repository.CrudRepository+.save(*)) || " +
"execution(* org.springframework.data.repository.CrudRepository+.saveAndFlush(*))")
public void beforeSavingPointcut() { }
@Before(value = "beforeSavingPointcut() && args(entity)", argNames = "entity")
public void addAudit(Object entity) { /*some logic here*/ }
Is there a way to perform some logic in aspect before saving the owner side (DocumentMetadata
in this case)?
来源:https://stackoverflow.com/questions/57975679/why-aspect-not-triggered-for-owner-side-in-onetoone-relationship