Skip to main content

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

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

Back to the top