Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [eclipselink-users] Getting context information in @PrePersist-Lifecycle method

I know we're doing it now in our Spring MVC app. I just took a peek at
the code, here's how it's done. We use a combination of a @PrePerist
on an EntityListener that we attach to a @MappedSuperClass that has
our createdBy/createdOn's.

In spring, for our authentication, we use an interceptor, itintercepts
requests and auth's them. There we store the current user name in a
static string variable.

Now here's where it gets weird :) I didn't write this, a colleague
did, I don't really understand it but it's worked so far (not
production tested).

But the string variable is a ThreadLocal variable
http://java.sun.com/javase/6/docs/api/java/lang/ThreadLocal.html

So it's like private ThreadLocal<String> currentUser;

and the static getter in the interceptor looks like this:

 public static String getCurrentUserName() {
	                return currentUser.get();
	        }

Then in our EntityListener we call the static getter to get the user name.

Hopefully that helps!

This way if you want to take advantage of the functionality, you
simply extend the MappedSuperclass, and you're done.

Let me know how that goes, also if anyone knows why this is incredibly
stupid or knows of some Spring IoC voodoo that would be more graceful,
I'm all ears :)

./tch



On Fri, Jul 17, 2009 at 10:32 AM, Johannes Michler<orgler@xxxxxxxxx> wrote:
> Hi,
>
> I'm writing some Data Access Layer using Eclipselink. My Database-Tables
> have columns for the creator, updator, createDate and updateDate of the
> table-entries. I'd like to update these entries on each persist/update. So
> far, I've found 3 ways of doing so:
>
> 1) in all my setter-methods in my Entity-Objects, du a setUpdated("now") and
> setUpdator(Username). This is quite ugly, because it needs my to write this
> code very often (10 tables=classes with 5 methods each approximately). And
> furthermore, the Entity-Objects don't know the current user, and this
> information cannot be retrieved in a static way. So I'd have to add a
> username-parameter to all my setter methods or do the setCreator() call
> after the "real-setter" call every time I call a setter.
>
> 2) manually set the data when calling entitymanager.commit(). It's easy to
> get the username there, but Cascade.Persist and other complex operations
> during the transaction make it quite heavy to find all newly created or
> changed objects.
>
> 3) Using @PrePersist and @PreUpdate. This would solve many problems, and the
> for the create/updates dates everything works fine. But how to get the
> current username there? the Entity-object doesn't know it of course, and
> static isn't an option. I'd need some kind of injection. Maybe I could set
> the username as a custom property of the entitymanager? But how could I get
> the current EntityManager in the PrePersist-Method?
>
> Or is there a way to attach a Persist-Listener to an entityManager? This
> would be perfect, but I found no such addPersistListener-Method on
> EntityManager :-(
>
>
> Any help would be great, cause Options 1 or 2 would cause me lots of work
> :-(
>
>
> -Johannes
>
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
>
>


Back to the top