[英]@PreUpdate and @Prepersist in hibernate/JPA (using session)
I've hit a blocker adding a fix to an existing project.the main problem is that i'll like to use @Prepersist
and @PreUpdate
in the POJO
to take care of LastModified
field (insert and update) using hibernate implementation of JPA with session.我遇到了一个阻止程序向现有项目添加修复程序。主要问题是我想在
POJO
使用@Prepersist
和@PreUpdate
来处理使用 JPA 的休眠实现的LastModified
字段(插入和更新)会议。
Reason ?原因 ? : That change is required because there is a need to use
liquibase 1.9.5
and i know (since i've faced this before) that liquibase translate timestamp fied to datetime with default current_timestamp, and that is too bad for mysql database. :需要进行这种更改,因为需要使用
liquibase 1.9.5
而且我知道(因为我以前遇到过这种情况)liquibase 将时间戳转换为使用默认 current_timestamp 的日期时间,这对 mysql 数据库来说太糟糕了。
So i needed a way to have this set in code rather than in database so i could safely change timestamp field to datetime.then liquibase is happy, i'm happy.所以我需要一种方法来在代码中而不是在数据库中设置这个,这样我就可以安全地将时间戳字段更改为日期时间。然后 liquibase 很高兴,我很高兴。
Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager
.That is currently out of question.So i'll like to know if is there a SIMPLE way around my problem, meaning having @Prepersist
or @PreUpdate
or even other workaround to set the lastModified
field still using session现在似乎这些解释器没有被执行,经过很少的搜索,我发现它适合使用
entityManager
。这是目前没有问题的。所以我想知道是否有解决我的问题的简单方法,这意味着有@Prepersist
或@PreUpdate
或什至其他解决方法来设置仍在使用会话的lastModified
字段
Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager.
现在似乎这些解释器没有被执行,通过很少的搜索我发现它适合使用 entityManager。
Yes, the JPA callbacks won't work if you're using the Session
API.是的,如果您使用的是
Session
API,则 JPA 回调将不起作用。
So I'll like to know if is there a SIMPLE way around my problem, meaning having @PrePersist or @PreUpdate or even other workaround to set the lastModified field still using session
所以我想知道是否有解决我的问题的简单方法,这意味着使用 @PrePersist 或 @PreUpdate 或什至其他解决方法来设置 lastModified 字段仍然使用会话
To my knowledge, there is no simple way around (if you're using Spring, MAYBE have a look at this post though).据我所知,没有简单的方法(如果您使用的是 Spring,也许可以看看这篇文章)。
My suggestion for an Hibernate based solution would be to use events (and one or more interface(s)).我对基于 Hibernate 的解决方案的建议是使用事件(以及一个或多个接口)。 Check Hibernate and last modified date for an example.
以Hibernate 和上次修改日期为例。
Mainly @PrePersist and @PreUpdate annotations are used in entity in hibernate or jpa.主要是@PrePersist 和@PreUpdate 注解用于hibernate 或jpa 中的entity。 When we save an entity on that time @PrePersist method is called.
当我们在那个时候保存一个实体时 @PrePersist 方法被调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.