简体   繁体   English

休眠/JPA 中的@PreUpdate 和@Prepersist(使用会话)

[英]@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.

相关问题 JPA PrePersist和PreUpdate - JPA PrePersist and PreUpdate Hibernate不会删除@PrePersist和@PreUpdate - Hibernate does not delete, @PrePersist and @PreUpdate 如何在不使用JPA的情况下Spring数据MongoDB @prepersist @preupdate - how to Spring-data MongoDB @prepersist @preupdate without using JPA 使用Spring Data JPA的Mongodb文档中的PrePersist和PreUpdate - PrePersist and PreUpdate in Mongodb document with Spring Data JPA 春季数据MongoDB的跨商店的插件和JPA PrePersist和更新前 - Spring Data MongoDB Cross-Store plugin and JPA PrePersist and PreUpdate 使用JPA事件@PrePersist和@PreUpdate审核Spring用户级上下文数据 - Auditing Spring User Level Context Data with JPA Events @PrePersist and @PreUpdate JPA - EntityListener - @PrePersist 和 @PreUpdate 成功运行,但 @PostPersist 和 @PostUpdate 不起作用 - JPA - EntityListener - @PrePersist and @PreUpdate work successfully, but @PostPersist and @PostUpdate not working 在Ebean和Play 2.5.x中使用@PrePersist和@PreUpdate无法正常工作? - Using @PrePersist and @PreUpdate with Ebean and Play 2.5.x not working? 使用@PrePersist和@PreUpdate时,创建日期字段在更新时为空 - Created date field becomes null on update while using @PrePersist and @PreUpdate JPA / Hibernate preUpdate不更新父对象 - JPA/Hibernate preUpdate doesn't update parent object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM