JPA实体生命周期事件与数据库触发器 [英] JPA Entity Lifecycle Events vs database trigger

查看:178
本文介绍了JPA实体生命周期事件与数据库触发器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JPA的新手.我试图弄清楚如何使用JPA实体生命周期事件代替数据库触发器.

当然差不多,除了在更新之前"数据库触发器(例如Postgresql)中,您总是拥有OLD和NEW记录,其中包含更新前后的值.

>

我找不到有关JPA的文档. Javadoc文档在 PreUpdate 注释中没有说太多. /p>

谢谢!

解决方案

生命周期事件 是在JPA中定义的. 它们是:PerPersist,PostPersist,PreRemove,PostRemove,PreUpdate,PostUpdate和PostLoad. 您可以使用适当的注释(上述方法之一)注释实体类中的方法. 方法签名是

void someMethodName()

它可以具有任何访问修饰符. 回调内部的操作有很多限制,例如,在实体管理器上调用方法.

特别是在PreUpdate上:确保在更新数据库之前的某个时刻调用PreUpdate,但是什么时候什么也没说.

I'm new to JPA. I'm trying to figure out how to use JPA Entity Lifecycle Events as a replacement for database triggers.

It is of course more or less the same, except that in a "before update" database trigger for example (Postgresql), you always have the OLD and NEW records, that contain the values before and after the update.

I was not able to find documentation on that for JPA. Javadoc documentation does not say much on PreUpdate annotation.

Thanks!

解决方案

Lifecycle events are defined in JPA. They are : PerPersist, PostPersist, PreRemove, PostRemove, PreUpdate, PostUpdate and PostLoad. You annotate methods in your entity class with the appropriate annotation (one of the above). The method signature is

void someMethodName()

It can have any access modifier. There are a number of limitations on what you can do inside a callback, such as invoking methods on an entity manager.

Specifically on PreUpdate: The PreUpdate is guaranteed to be called at some point before updating the db but says nothing on when.

这篇关于JPA实体生命周期事件与数据库触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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