Spring Data REST - @PrePersist和@HandleBeforeCreate之间的区别? [英] Spring Data REST - difference between @PrePersist and @HandleBeforeCreate?

查看:491
本文介绍了Spring Data REST - @PrePersist和@HandleBeforeCreate之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring Data Rest 超过 JPA 映射。

JPA为数据库中en实体的持久性之前调用的方法提供 @PrePersist 注释。

JPA provides @PrePersist annotation for methods to be called before the persistence of en entity in the DB.

Spring Data Rest为捕获实体创建事件时要调用的方法提供 @HandleBeforeCreate 注释。

Spring Data Rest provides @HandleBeforeCreate annotation for method to be called when catching an entity creation event.

这似乎相当于我。什么时候应该使用另一个?我应该何时使用另一个?

This seems rather equivalent to me. When should I use one and when should I use the other ?

推荐答案


  1. @HandleBeforeCreate仅被调用当REST请求进入但在实体生命周期期间调用@PrePersist时。因此,如果您的调用路径不是通过REST(例如通过直接调用实体管理器或由于JPA impl的内部级联操作),则无法使用@HandleBeforeCreate捕获事件。

  2. 由于Spring调用了@HandleBeforeCreate,因此很容易将它放入bean中并享受所有Spring功能。实体监听器的生命周期由JPA impl管理,因此通常需要一些技巧连接到Spring生态系统。

例如,我使用@HandleBeforeCreate而不是@PrePersist进行安全检查。由于第1项,我只想检查暴露休息操作的安全性,由于第2项,我可以轻松地使用@Secured或@PreAuth注释和我的方法进行检查。

For instance, I use @HandleBeforeCreate rather than @PrePersist for something like security checks. Since due to item 1, I only want to check the security for exposed rest operations and due to item 2, I can easily use @Secured or @PreAuth annotations with my methods to do the check.

这篇关于Spring Data REST - @PrePersist和@HandleBeforeCreate之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆