English 中文(简体)
Setting createdBy and updatedBy in JPA entities automatically
原标题:

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.

问题回答

You can have a look at one of these approaches to pass the user ID as context in the business layer:

(The posts may still be relevant even if you re not using EJB. The second post make sense however only if you use Spring with JTA)

I personally discourage these approach, as I perceive two problem with it:

  • Testability: contextual data will need to be set up in the test
  • Contract: contextual data participate in the contract to use the entity but is not clearly visible in the interface.

Passing userID "all over the place" may seem like a big job, but I think it s cleaner.

To set the date and user ID automatically when entity is created or update, you can use an EntityListener or lifecycle callbacks (maybe you re already doing that). Hope it helps...

I d create a class like this:


@MappedSuperclass
public abstract class AuditableDomainClass {
  private long createdBy;
  private long updatedBy;

  //getters and setters

Your entity classes that have the requirement you ve described would simply extend this class, you d set you variables in the layer you need to (controller for example) and you don t need to worry about it all the way down in the DAOs.





相关问题
Multiple Hibernate instances using C3P0

I am facing a weird problem and it seems to be c3p0 related. I am starting two instances of an app in the same java vm which interact with each other. After some operations "APPARENT DEADLOCK" ...

Hibernate vs Ibatis caching

We can speed up a hibernate app easyly with 2nd level cache using infinispan or ehcache/terracotta,... but ibatis only have a simple interface to implement for caching. And hibernate knows more ...

Using annotations to implement a static join in hibernate

I m relatively new to hibernate and was wondering if someone could help me out. While I have no issues implementing a normal join on multiple columns in hibernate using the @JoinColumns tag, I m ...

Hibernate query with fetch question

I have a 2 entities in a One-To-Many relationship: OfficeView.java: public class OfficeView implements java.io.Serializable { private Integer officeId; private String addr1; private ...

hibernate interceptors : afterTransactionCompletion

I wrote a Hibernate interceptor : public class MyInterceptor extends EmptyInterceptor { private boolean isCanal=false; public boolean onSave(Object entity, Serializable arg1, Object[] arg2, String[]...

How to prevent JPA from rolling back transaction?

Methods invoked: 1. Struts Action 2. Service class method (annotated by @Transactional) 3. Xfire webservice call Everything including struts (DelegatingActionProxy) and transactions is configured ...

Hibernate/GORM: collection was not processed by flush()

I have an integration test in my Grails application that fails when I try to save an entity of type Member invitingMember.save(flush: true) This raises the following exception org.hibernate....

Hibernate Criteria API equivalent for "elements()"

Is it possible to implement the following query using Criteria API? select order from ORDER as order,ITEM as item where item.itemID like ITM_01 and item in elements(order.items)

热门标签