我在向现有项目添加修复程序时遇到了阻碍。主要问题是我希望在 POJO
中使用 @Prepersist
和 @PreUpdate
来使用带有 session 的 JPA 的 hibernate 实现来处理 LastModified
字段(插入和更新)。
原因?:
需要进行此更改,因为需要使用 liquibase 1.9.5
并且我知道(因为我之前遇到过这种情况)liquibase 将时间戳转换为默认 current_timestamp 的日期时间,这对于 mysql 数据库来说太糟糕了。
所以我需要一种方法来在代码中而不是在数据库中设置此设置,以便我可以安全地将时间戳字段更改为日期时间。然后 liquibase 很高兴,我很高兴。
现在看来这些解释器没有被执行,经过很少的搜索,我发现它适合使用 entityManager
。目前这是没有问题的。所以我想知道是否有一个 SIMPLE 方法解决我的问题,这意味着有 @Prepersist
或 @PreUpdate
或什至其他解决方法来设置仍使用 session 的 lastModified
字段
最佳答案
Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager.
是的,如果您使用 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
据我所知,没有简单的解决方法(如果您使用 Spring,也许看看 this post)。
我对基于 Hibernate 的解决方案的建议是使用 events (以及一个或多个接口(interface))。查看 Hibernate and last modified date 的示例。
关于java - hibernate/JPA 中的 @PreUpdate 和 @Prepersist (使用 session ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29598989/