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

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

问题描述

我打了一个阻止器,为现有的项目添加了一个修复程序。主要问题是我想要使用 @Prepersist POJO 中使用@PreUpdate 来处理 LastModified 字段(插入和更新) hibernate使用会话实现JPA。
$ b $ p

原因
需要更改,因为存在需要使用 liquibase 1.9.5 并且我知道(因为我之前遇到过这种情况),liquibase将timestamp转换为datetime并使用默认current_timestamp,这对于mysql数据库。

因此,我需要一种方法来在代码中而不是在数据库中设置此设置,以便我可以安全地将timestamp字段更改为datetime.then liquibase很高兴,现在看来这些解释器没有被执行,很少有搜索,我发现它适合使用 entityManager $ c>。目前没有问题,所以我会喜欢要知道我的问题是否存在 SIMPLE 方式,意味着 @Prepersist @PreUpdate 甚至是其他解决方法来设置 lastModified 字段,仍然使用会话

解决方案

< blockquote>

现在看起来这些解释器并没有被执行,很少有搜索,我发现它适合使用entityManager。


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


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


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

我对基于Hibernate的解决方案的建议是使用事件(和一个或多个界面)。查看 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.

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

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