# Envers: custom fields on RevisionEntity don't get persisted

**URL:** <https://discourse.hibernate.org/t/envers-custom-fields-on-revisionentity-dont-get-persisted/10309>\
**Category:** Hibernate ORM\
**Created:** [September 11, 2024, 9:27am UTC](https://discourse.hibernate.org/t/envers-custom-fields-on-revisionentity-dont-get-persisted/10309 "2024-09-11T09:27:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![khkalli](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/khkalli/32/3467_2.png) [@khkalli](https://discourse.hibernate.org/u/khkalli)\
**Post date:** [September 11, 2024, 9:27am UTC](https://discourse.hibernate.org/t/envers-custom-fields-on-revisionentity-dont-get-persisted/10309/1 "2024-09-11T09:27:45Z")

</div>

I’ve been trying to use envers to automatically audit the below entity whenever there is an update performed on it. This does work and a table “mytable\_aud” gets created with the columns  
id, rev, revtype, name, offval, onval.

```auto
@Entity
@Getter
@Setter
@Table(name = "mytable")
@Audited
@EntityListeners(MyAuditManager.class)
public class MyEntity {
    @Id
    @Column(name = "id", nullable = false)
    @SequenceGenerator(allocationSize = 1, name = "mytable_seq", sequenceName = "mytable_seq")
    @GeneratedValue(generator = "mytable_seq", strategy = GenerationType.SEQUENCE)
    private Long id;

    @Column
    private String name;

    @Column(name = "onval")
    private String onVal;

    @Column(name = "offval")
    private String offVal;
    
    @Column
    private Date modified;

}

```

Now I wanted to store some additional information - 2 columns, that only exist in the audit table.  
I followed the documentation and various guides on the internet but nothing seems to work:

```auto
@Getter
@Setter
@Entity
@RevisionEntity(MyAuditManager.class)
public class MyEntityRevision extends DefaultRevisionEntity {

    private Date fromDate;

    private Date toDate;

}

```

This is my RevisionListener:

```auto
@NoArgsConstructor
@AllArgsConstructor
public class MyAuditManager implements RevisionListener {

    private static final ThreadLocal<MyEntity> currentEntity = new ThreadLocal<>();

    private EntityManager entityManager ;

    @Override
    public void newRevision(Object revisionEntity) {
            MyEntityRevision rev = MyEntityRevision.class.cast(revisionEntity);
            rev.setToDate(new Date());
            rev.setFromDate(currentEntity.get().getModified());
            clearEntity();
    }

    @PrePersist
    @PreUpdate
    public void setEntity(Object entity) {
        if (entity instanceof MyEntity) {
            currentEntity.set((MyEntity) entity);
        }
    }

    public void clearEntity() {
        currentEntity.remove();
    }

```

The Listener is working correcly and the @PrePersist method as well as the newRevision method are being called correctly.  
However, when the entity gets updated and the audit gets inserted, there are no columns “fromDate” and “toDate” to be found in the audit table.  
Is this not the correct way to extend the default revision?

I’m Using hibernate 5.6.12 and Java 17

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [September 11, 2024, 2:00pm UTC](https://discourse.hibernate.org/t/envers-custom-fields-on-revisionentity-dont-get-persisted/10309/2 "2024-09-11T14:00:04Z")

</div>

> [@khkalli](#):
>
> However, when the entity gets updated and the audit gets inserted, there are no columns “fromDate” and “toDate” to be found in the audit table.

What do you mean by that? Do you get an error?

---

<div class="post-metadata">

**Author:** ![khkalli](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/khkalli/32/3467_2.png) [@khkalli](https://discourse.hibernate.org/u/khkalli)\
**Post date:** [September 11, 2024, 2:32pm UTC](https://discourse.hibernate.org/t/envers-custom-fields-on-revisionentity-dont-get-persisted/10309/3 "2024-09-11T14:32:53Z")

</div>

@beikov I didn’t get any error but it seems to be working when I add @Table(name=“mytable\_revinfo”) on my RevisionEntity class.

By doing so, my entity object gets stored in the mytable\_aud table  
and the custom columns get stored in “mytable\_revinfo”

My guess is that hibernate tried to create a table ‘revinfo’ (I believe that’s the default name?) but couldn’t because it already existed in my project.
