hibernate / JPA中的@PreUpdate和@Prepersist(使用session)

时间:2022-09-16 06:44:43

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的hibernate实现来处理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的datetime,这对于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.

所以我需要一种方法来将这个集合放在代码中而不是数据库中,这样我就可以安全地将时间戳字段更改为datetime.then 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.That目前是不可能的。所以我想知道是否有一个简单的方法解决我的问题,这意味着@ Prepersist或@PreUpdate甚至其他解决方法来设置仍然使用会话的lastModified字段

1 个解决方案

#1


44  

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字段仍然使用session

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

据我所知,没有简单的方法(如果你使用Spring,MAYBE虽然看看这篇文章)。

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.

我对基于Hibernate的解决方案的建议是使用事件(以及一个或多个接口)。检查Hibernate和上次修改日期以获取示例。

#1


44  

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字段仍然使用session

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

据我所知,没有简单的方法(如果你使用Spring,MAYBE虽然看看这篇文章)。

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.

我对基于Hibernate的解决方案的建议是使用事件(以及一个或多个接口)。检查Hibernate和上次修改日期以获取示例。