views:

455

answers:

1

I'd like to store username that has last modified table row to as a field in every table.

I have following setup: an user logs in, web layer calls some EJB 3.0 beans. Those EJB beans create and modify some JPA entities. Now, I'd like that username (from weblayer) would be automatically stored to every JPA entity (and database row) that is created or modified during EJB method call.

I have this kind of table, (that is: every table has field modifier):

CREATE TABLE my_table (
  some_data INTEGER,
  modifier VARCHAR(20)
);

By automatically I mean, that I wouldn't need to set manually username to each entity inside EJB methods.

You could achive this by storing username to ThreadLocal variable and fetching username from ThreadLocal in JPA entity's EntityListener. However this works only you are using a local EJB call, it will not work with EJB calls that cross JVM boundaries. I'd like to make this work over remote EJB method calls.

It this possible at all?

I am using Weblogic Server 10.3, EJB3.0 and EclipseLink JPA. I am also interested in hearing if this works with other JPA implementations.

+3  A: 

You can use an EJB interceptor (@Around) on the EJB class to get the current user (using standard EJB api) and storing the name in the threadlocal variable. This would work transparently for remote and local calls.

Antonio