休眠/JPA 中的@PreUpdate 和@Prepersist(使用会话) [英] @PreUpdate and @Prepersist in hibernate/JPA (using session)

查看:59
本文介绍了休眠/JPA 中的@PreUpdate 和@Prepersist(使用会话)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向现有项目添加修复程序时遇到了阻止程序.主要问题是我想在 @Prepersist@PreUpdate 中使用 @Prepersistcode>POJO 使用带有会话的 JPA 的休眠实现来处理 LastModified 字段(插入和更新).

原因?:需要进行这种更改,因为需要使用 liquibase 1.9.5 并且我知道(因为我之前遇到过这种情况)liquibase 将时间戳转换为默认 current_timestamp 的日期时间,这太糟糕了用于mysql数据库.

所以我需要一种方法来在代码中而不是在数据库中设置这个,这样我就可以安全地将时间戳字段更改为日期时间.然后 liquibase 很高兴,我很高兴.

现在似乎这些解释器没有被执行,我通过很少的搜索发现它适合使用 entityManager.这是目前没有问题的.所以我想知道是否有SIMPLE 解决我的问题的方法,这意味着使用 @Prepersist@PreUpdate 或什至其他解决方法来设置 lastModified字段仍在使用会话

解决方案

现在似乎这些解释器没有被执行,我通过很少的搜索发现它适合使用 entityManager.

是的,如果您使用 Session API,JPA 回调将不起作用.

<块引用>

所以我想知道是否有解决我的问题的简单方法,这意味着使用 @PrePersist 或 @PreUpdate 或什至其他解决方法来设置 lastModified 字段仍然使用会话

据我所知,没有简单的方法可以解决(如果您使用的是 Spring,可能请查看这篇文章.

我对基于 Hibernate 的解决方案的建议是使用 事件(以及一个或多个接口).以休眠和上次修改日期为例.>

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.

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.

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.

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

解决方案

Now it seems that those interpreters are not been executed, with little search i found out that it's suitable using entityManager.

Yes, the JPA callbacks won't work if you're using the Session API.

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

To my knowledge, there is no simple way around (if you're using Spring, MAYBE have a look at this post though).

My suggestion for an Hibernate based solution would be to use events (and one or more interface(s)). Check Hibernate and last modified date for an example.

这篇关于休眠/JPA 中的@PreUpdate 和@Prepersist(使用会话)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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