本文介绍了Hibernate Envers:“withModifiedFlag”在INT列值更改为NULL时未设置为1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要在我的审计表中捕获所有需要捕获的实体属性的 @Audited(withModifiedFlag = true)注释。对于所有情况,除了价值删除之外,它都工作得很好。意思是,如果我的列的值从NULL / INT设置为某个非空值,那么相应的 modifiedColumn 值设置为1,但是如果设置了该值从任何INT值到NULL,那么 modifiedColumn 被设置为0(表示未修改)。我不知道为什么会发生这种情况。我已经检查了Envers文件和Envers公开问题清单,但没有找到任何有关它的信息。 顺便说一句,我使用Hibernate& Hibernate-Envers 4.1.12.Final版本。 更新1 以下是我的实体详情: @Entity @Audited(withModifiedFlag = true) @AuditTable(AuditLatency) public class Latency { @Id @Column(name =Id)私人长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()&审计表结果: 感谢您的阅读!解决方案 div> 你的场景必须有错误。 我在4.1.12.Final上使用了以下实体映射和代码,并且观察修改后的标志设置正确。 @Id @GeneratedValue private整数ID; 私人整数值; 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(); //修订版2,将值更改为非空 entityManager.getTransaction()。begin(); ive.setValue(25); entityManager.getTransaction()。commit(); //修订3,将值更改为空 entityManager.getTransaction()。begin(); ive.setValue(null); entityManager.getTransaction()。commit(); final布尔型rev1 = getRevisionValueModifiedFlag(1); final布尔型rev2 = getRevisionValueModifiedFlag(2); final布尔rev3 = getRevisionValueModifiedFlag(3); assertEquals(Revision 1 should be false,false,rev1); assertEquals(Revision 2 should be true,true,rev2); assertEquals(Revision 3 should be true,true,rev3); } catch(Exception e){ if(entityManager.getTransaction()。isActive()){ entityManager.getTransaction()。rollback(); } throw e; } finally { entityManager.close(); 全部 getRevisionValueModifiedFlag does执行一个本地查询,并执行以下操作: SELECT value_MOD FROM IntegerValueEntity_AUD WHERE REV =:rev 如果您的实体更复杂或者您的业务逻辑更加复杂,一旦分享了这些信息,我会更新相应地回答。 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 1Here 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! 解决方案 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 */}@Testpublic 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 = :revIf your entity is more complex or your business logic is more complex, once you share that information I'll update this answer accordingly. 这篇关于Hibernate Envers:“withModifiedFlag”在INT列值更改为NULL时未设置为1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 17:37