Hibernate Envers: “withModifiedFlag” is not set to 1 on INT column value changed to NULL

安稳与你 提交于 2019-12-08 12:43:56

问题


I have the @Audited(withModifiedFlag = true) annotation on all the Entity properties which needs to be captured in my Audit table. It is working great for all the cases, except for value deletion. Meaning, If the value of my column is set from NULL/INT to some non-null value, then the corresponding modifiedColumn value is set to 1, But if the value is set from any INT value to NULL then the modifiedColumn is set to 0(indicates not modified). I have no clue why this is happening. I have checked in the Envers documents and also in the Envers open issues list and didn't find anything about it.

By the way, I use Hibernate & Hibernate-Envers 4.1.12.Final version.

Update 1

Here are my entity details:

@Entity
@Audited(withModifiedFlag = true)
@AuditTable("AuditLatency")
public class Latency {

    @Id
    @Column(name = "Id")
    private Long id;

    @Column(name = "DataCenter1")
    private Long dataCenter1;

    @Column(name = "DataCenter2")
    private Long dataCenter2;

    @Column(name = "DataCenter3")
    private Long dataCenter3;

    @Column(name = "StatusId")
    @Type(type = "enum_status_active_inactive_type")
    private Status statusId;

    //getXXX() & setXXX()

}

Audit Table result:

Pls notice that in each revision, even one of the column gets updated all other flags set to 1(true). And, for NULL values flag is set to 0.

Thanks for reading!


回答1:


There must be awry with your scenario.

I used the following entity mapping and code on 4.1.12.Final and observed the modified flag was set correctly.

@Entity
@Audited(withModifiedFlag = true)
public class IntegerValueEntity {
  @Id
  @GeneratedValue
  private Integer id;
  private Integer value;
  /* getter/setters */
}

@Test
public void runTest() {
    EntityManager entityManager = getEntityManager();
    try {
        // revision 1, everything null
        SimpleEntity ive = new IntegerValueEntity();
        entityManager.getTransaction().begin();
        entityManager.persist( ive );
        entityManager.getTransaction().commit();

        // revision 2, change value to non null
        entityManager.getTransaction().begin();
        ive.setValue( 25 );
        entityManager.getTransaction().commit();

        // revision 3, change value to null
        entityManager.getTransaction().begin();
        ive.setValue( null );
        entityManager.getTransaction().commit();

        final Boolean rev1 = getRevisionValueModifiedFlag( 1 );
        final Boolean rev2 = getRevisionValueModifiedFlag( 2 );
        final Boolean rev3 = getRevisionValueModifiedFlag( 3 );

        assertEquals( "Revision 1 should have been false", false, rev1 );
        assertEquals( "Revision 2 should have been true", true, rev2 );
        assertEquals( "Revision 3 should have been true", true, rev3 );
    }
    catch ( Exception e ) {
        if ( entityManager.getTransaction().isActive() ) {
            entityManager.getTransaction().rollback();
        }
        throw e;
    }
    finally {
        entityManager.close();
    }
}

All getRevisionValueModifiedFlag does is executes a native query with the following:

SELECT value_MOD FROM IntegerValueEntity_AUD WHERE REV = :rev

If your entity is more complex or your business logic is more complex, once you share that information I'll update this answer accordingly.



来源:https://stackoverflow.com/questions/38974235/hibernate-envers-withmodifiedflag-is-not-set-to-1-on-int-column-value-changed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!