在JPA实体中自动设置createdBy和updatedBy [英] Setting createdBy and updatedBy in JPA entities automatically

查看:770
本文介绍了在JPA实体中自动设置createdBy和updatedBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究JPA(Hibernate实现),Spring和Stripes Web应用程序。我有许多JPA实体,它们具有以下用于审计和查询的共同字段:
$ b createdBy - 创建实体的人的用户标识。
createdOn - 创建实体的日期
updatedBy - 上次更新实体的人员的用户ID
updatedOn - 实体上次更新的日期



我有我的应用程序工作,以便createdOn和updatedOn在实体被持久保存时自动设置,但我不确定如何获取createdBy和updatedBy字段而无需通过当前从控制器类到DAO一直登录用户的ID。



有没有人有任何建议,我可以做到这一点,而不是在所有地方传递用户ID?请注意,当前的用户ID存储在HttpSession对象中,所以我的后端需要以某种方式访问​​这些数据...



谢谢!

解决方案

我已经决定ThreadLocal可能是在我的应用程序中执行此操作的最简洁的方法。


I'm working on a JPA (Hibernate implementation of), Spring and Stripes web app. I have a number of JPA entities that have the following fields in common for both audit and query purposes:

createdBy - the user ID of the person who created the entity. createdOn - the date the entity was created updatedBy - the user ID of the person who last updated the entity updatedOn - the date the entity was last updated

I've got my app working so that createdOn and updatedOn are set automatically when the entity is persisted but I'm not sure how I can get the createdBy and updatedBy fields populated without having to pass through the currently logged in user's ID all the way from the controller class to the DAOs.

Does anyone have any suggestions on how I might do this without passing userIDs all over the place? Note that the current user ID is stored in the HttpSession object at the moment, so my backend needs to somehow access this data...

Thanks!

解决方案

I've decided that a ThreadLocal is probably the cleanest way to do this in my application.

这篇关于在JPA实体中自动设置createdBy和updatedBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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