Why aspect not triggered for owner side in OneToOne relationship?

泄露秘密 提交于 2020-01-25 08:42:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!