I am still a newbie to Seam ...
I am using a POJO based seam-gen created application.
I have declared my User entity class like so
@Entity @Table(name = "APPUSER", uniqueConstraints = @UniqueConstraint(columnNames = "USER_NAME")) @Name("user") @Role(name="currentUser", scope=ScopeType.SESSION) public class User implements java.io.Serializable { ... }
and in my authenticate method
@Out(required = false, scope = SESSION) private User user;
The validated user instance is successfully stored in session scope as currentUser after authentication.
What I am trying to do is update audit columns for another entity called Burst managed by a EntityHome.
In Burst entity I have this
@PrePersist @PreUpdate public void beforeSave() { System.out.println("Entering beforeSave method"); User user = (User) Contexts.getSessionContext().get("currentUser"); if ( getBurstId() == null ) { System.out.println("setting updates for new entity instance"); //new instance this.setAddedByUser(user); this.setModifiedByUser(user); this.dateCreated = new java.sql.Date(System.currentTimeMillis()); this.dateModified = new java.sql.Date(System.currentTimeMillis()); } else { System.out.println("setting updates for entity instance "+ getBurstName()); this.setModifiedByUser(user); this.dateModified = new java.sql.Date(System.currentTimeMillis()); } }
This works but my questions are
Is this the right approach to update these values. Should
I have declared a EntityListener class instead to do this?
Secondly, if I were to inject
@(currentUser
) into my Burst entity class, it gives me a null pointer - is that because Seam bijection does not work in a entity class ?
Can anyone share the right way to update these columns after a persist/update operation.
Thanks
Franco