I have an requirement where we an entity with self entity and want to audit them. Have a look at it below:
@Entity
@Table(name = TableNames.CLIENT)
@EqualsAndHashCode(exclude="clientContacts")
@Audited
@AuditTable(value = TableNames.CLIENT_HISTORY)
public class Client implements Serializable {
private static final long serialVersionUID = -2789655782782839286L;
@Id
@Column(name = "ID")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "clientGenerator")
@SequenceGenerator(name = "clientGenerator", sequenceName =
"MEMBERSHIP_CLIENT_SQ",
allocationSize = 1)
private Long id;
@Column(name = "PARENT_ID")
private Long parentId;
@Column(name = "LEGAL_NAME")
private String legalName;
@Column(name = "LEI")
private String lei;
@Column(name = "CICI")
private String cici;
@Column(name = "BIC")
private String bic;
@Column(name = "LCH_UNIQUE_ID")
private String lchUniqueId;
@Column(name = "SALESFORCE_ID")
private String salesforceId;
@Column(name = "FUND_MANAGER")
private String fundManager;
@Column(name="INCORPORATION_COUNTRY_ID")
private Long incorporationCountryId;
@Column(name="FINANCIAL_CATEGORY_ID")
private Long financialCategoryId;
@Embedded
@JsonUnwrapped
private Address address;
@OneToMany(mappedBy = "client")
private Set<ClientContact> clientContacts;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PARENT_ID", insertable = false, updatable = false)
@Getter(onMethod = @__({@JsonIgnore, @Transient}))
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Client parent;
}
As you can see we have client object as parent within the client object. We also have the parentId field, which has the id of the parent if one has the parent.
Here I mark the relation as NOT_AUDITED, so would expect the parent to be fetched from the main table rather than the history table. But I noticed that it is querying the history table for parent as well.
Also, is this the right way to reference the self object. Is there any better way to represent it?
I am using envers version 5.3.7.Final.
Log to show the data is being retrieved from the history table instead of the main table:
Hibernate: select client_his0_.id as id1_39_0_, client_his0_.revision as revision2_39_0_, customrevi1_.id as id1_5_1_, client_his0_.revision_type as revision_type3_39_0_, client_his0_.address_line1 as address_line4_39_0_, client_his0_.address_line2 as address_line5_39_0_, client_his0_.address_line3 as address_line6_39_0_, client_his0_.address_line4 as address_line7_39_0_, client_his0_.address_line5 as address_line8_39_0_, client_his0_.address_postcode as address_postcode9_39_0_, client_his0_.bic as bic10_39_0_, client_his0_.cici as cici11_39_0_, client_his0_.financial_category_id as financial_categor12_39_0_, client_his0_.fund_manager as fund_manager13_39_0_, client_his0_.incorporation_country_id as incorporation_cou14_39_0_, client_his0_.lch_unique_id as lch_unique_id15_39_0_, client_his0_.legal_name as legal_name16_39_0_, client_his0_.lei as lei17_39_0_, client_his0_.parent_id as parent_id18_39_0_, client_his0_.salesforce_id as salesforce_id19_39_0_, customrevi1_.timestamp as timestamp2_5_1_, customrevi1_.username as username3_5_1_ from membership_client_history client_his0_ cross join audit_revision customrevi1_ cross join audit_revision customrevi2_ where client_his0_.revision=customrevi2_.id and client_his0_.id=? and client_his0_.revision=customrevi1_.id order by customrevi2_.timestamp asc
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [BIGINT] - [359]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([id1_39_0_] : [BIGINT]) - [359]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([revision2_39_0_] : [INTEGER]) - [803107]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([id1_5_1_] : [INTEGER]) - [803107]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([revision_type3_39_0_] : [INTEGER]) - [1]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([address_line4_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([address_line5_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([address_line6_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([address_line7_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([address_line8_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([address_postcode9_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([bic10_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([cici11_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([financial_categor12_39_0_] : [BIGINT]) - [8]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([fund_manager13_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([incorporation_cou14_39_0_] : [BIGINT]) - [19]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([lch_unique_id15_39_0_] : [VARCHAR]) - [LCH00000359]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([legal_name16_39_0_] : [VARCHAR]) - [Dupont Denant Contrepartie]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([lei17_39_0_] : [VARCHAR]) - [null]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([parent_id18_39_0_] : [BIGINT]) - [57]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([salesforce_id19_39_0_] : [VARCHAR]) - [0012000000zMKFYAA4]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([incorporation_cou14_39_0_] : [BIGINT]) - [19]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([financial_categor12_39_0_] : [BIGINT]) - [8]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([parent_id18_39_0_] : [BIGINT]) - [57]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([timestamp2_5_1_] : [BIGINT]) - [1562172380000]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.t.descriptor.sql.BasicExtractor - extracted value ([username3_5_1_] : [VARCHAR]) - [risk-portal.schedule]
Hibernate: select client_his0_.id as id1_39_, client_his0_.revision as revision2_39_, client_his0_.revision_type as revision_type3_39_, client_his0_.address_line1 as address_line4_39_, client_his0_.address_line2 as address_line5_39_, client_his0_.address_line3 as address_line6_39_, client_his0_.address_line4 as address_line7_39_, client_his0_.address_line5 as address_line8_39_, client_his0_.address_postcode as address_postcode9_39_, client_his0_.bic as bic10_39_, client_his0_.cici as cici11_39_, client_his0_.financial_category_id as financial_categor12_39_, client_his0_.fund_manager as fund_manager13_39_, client_his0_.incorporation_country_id as incorporation_cou14_39_, client_his0_.lch_unique_id as lch_unique_id15_39_, client_his0_.legal_name as legal_name16_39_, client_his0_.lei as lei17_39_, client_his0_.parent_id as parent_id18_39_, client_his0_.salesforce_id as salesforce_id19_39_ from membership_client_history client_his0_ where client_his0_.revision=(select max(client_his1_.revision) from membership_client_history client_his1_ where client_his1_.revision<=? and client_his0_.id=client_his1_.id) and client_his0_.revision_type<>? and client_his0_.id=?
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [1] as [INTEGER] - [803107]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [2] as [INTEGER] - [2]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] TRACE o.h.type.descriptor.sql.BasicBinder - binding parameter [3] as [BIGINT] - [57]
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] DEBUG o.s.s.w.h.writers.HstsHeaderWriter - Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@2021d30
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] DEBUG o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
2019-07-12 14:48:12 [http-nio-0.0.0.0-8899-exec-6] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [/risk-portal] threw exception [Request processing failed; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.lch.grouprisk.riskportal.entity.crud.Client with id 57] with root cause
javax.persistence.EntityNotFoundException: Unable to find com.lch.grouprisk.riskportal.entity.crud.Client with id 57