While doing delete operation few fields are not audited its take value as null while persisting in db
Asked Answered
S

2

6

Using Hibernate Envers, while doing create operation audit functionality is properly working data is correctly filled up in main as well as audit table but while deleting an entry for few fields its taking null following is the code sample:

@Audited
@MappedSuperclass
public abstract class A implements Serializable
{
private static final long   serialVersionUID    = 1L;
@NotNull
private Long                id;
@NotNull
private String              user;   
private Timestamp           time;
}

@Entity
@Audited
public class B extends A
{
private static final long               serialVersionUID    = 1L;

@EmbeddedId
private EmbeddedId  embeddedId;

@ManyToOne
@JoinColumn(name = "CODE")
private Code                    code;
}

@Audited
@Embeddable
public class EmbeddedId implements Serializable
{
private static final long                   serialVersionUID    = 1L;

@ManyToOne
@JoinColumn(name = "C")
private C                       c;

@ManyToOne(optional = false)
@JoinColumns(value =
{ @JoinColumn(name = "C_ID", referencedColumnName = "C_ID"),
        @JoinColumn(name = "D_ID", referencedColumnName = "D_ID") })
private D   d;

}   

@Entity
@Audited
public class Code extends A
{
private static final long   serialVersionUID    = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long                cId;

private String              name;
}

All the classes are annotated with @Audited, when delete operation is fired Class B data is not properly getting audited. Data corresponding to the embeddedId (composite primary key) is reflecting in audit table but other data like (code, id, user, time) i.e member variable of that class as well as coming from inheritance are going as null in audit table.

Please provide with the detailed solution for this issue as i am new to Envers.

Sill answered 10/5, 2017 at 13:58 Comment(0)
M
10

If you want non-identifier attributes to be stored when you perform a DELETE operation, you must explicitly tell Envers that you wish to enable this behavior because by default it doesn't do that.

Simply toggle the configuration org.hibernate.envers.store_data_at_delete to true and Envers will begin to capture and store the non-identifier audited attributes during the DELETE database operation.

You set this configuration where you supply other hibernate configuration properties.

Mf answered 10/5, 2017 at 14:44 Comment(2)
thanks, now while performing Delete operation also, i am able to store the data into the database.but what exactly it means non-identifier attribute ??Sill
Envers inherently stores the @Id or @EmbeddedId properties from your class when you create any type of revision (insert, update, or delete); however, it normally only stores the other audited attributes when you perform an insert or update only. By setting this property, you cause Envers to store all audited attributes of your entity class and related classes when you perform a deletion of an audited entity class.Mf
B
3

Just a minor addition: In our environment using spring boot and Hibernate - the following variable made the trick (since the one offered here dod not - but gave me the idea about how to address it):

spring.jpa.properties.org.hibernate.envers.store_data_at_delete=true

Brasier answered 29/9, 2021 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.