How do I create custom field to store user credent

2019-09-13 00:28发布

问题:

We are using hibernate-envers and having *_AUD table that stores historical state of entities. There is also a global REVINFO table which contains revision number, timestamp.

I need to add user as field in REVINFO table. How to add "user" field in REVINFO table?

回答1:

You can definitely create your custom RevisionInfo entity. The custom revisions entity must have an integer-valued unique property (preferably the primary id) annotated with {@link RevisionNumber} and a long-valued property annotated with {@link RevisionTimestamp}.

The {@link DefaultRevisionEntity} already has those two fields, so you may extend it, but you may also write your own revision entity from scratch. So in your case the revision entity may look like following:

@Entity
@RevisionEntity()
public class RevisionsInfo extends DefaultRevisionEntity {

  private Long userId;

  public Long getUserId() { return userId; }

  public void setUserId(final Long uid) { this.userId = uid; }

}

In addition to that you can also give your custom RevisionListener for any other special needs . See following example:

public class RevisionListener implements org.hibernate.envers.RevisionListener
{
    /**
     * {@inheritDoc}
     */
    public void newRevision(final Object revisionInfo)
    {
        // updateInfo your info here if required
    }
}

The custom RevisionListener can be provided as an argument to RevisionEntity annotation.

@RevisionEntity(RevisionListener.class)