Reputation: 31
I have a User hibernate @Entity which has a column CreatedBy. I have this user info (created by) stored in User Session.
@Entity
public User
@Column(name="CreatedBy")
public createdBy;
}
I want that @CreatedBy column should automatically read from user Session. I dont want to set it using setter method.
I am using Spring framework with Hibernate.
Thanks
Upvotes: 0
Views: 196
Reputation: 3637
You can use @PrePersist and @PreUpdate annotations, specifically the @PrePersist, like this
@PrePersist
public void audit () {
this.createdBy = SecurityContextHolder.getContext().getAuthentication().getName();
}
You can read more from here
Sorry for my bad english, cheers
Upvotes: 1