rajneesh
rajneesh

Reputation: 31

Pass UserID from user session as @Column in hibernate Entity

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

Answers (1)

Arturo Volpe
Arturo Volpe

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

Related Questions