|
4 | 4 | */
|
5 | 5 | package org.hibernate.envers.event.spi;
|
6 | 6 |
|
| 7 | +import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer; |
| 8 | +import org.hibernate.bytecode.enhance.spi.interceptor.LazyAttributeLoadingInterceptor; |
| 9 | +import org.hibernate.bytecode.spi.BytecodeEnhancementMetadata; |
7 | 10 | import org.hibernate.envers.boot.internal.EnversService;
|
8 | 11 | import org.hibernate.envers.internal.synchronization.AuditProcess;
|
9 | 12 | import org.hibernate.envers.internal.synchronization.work.AuditWorkUnit;
|
@@ -71,12 +74,30 @@ private Object[] postUpdateDBState(PostUpdateEvent event) {
|
71 | 74 | final Object[] newDbState = event.getState().clone();
|
72 | 75 | if ( event.getOldState() != null ) {
|
73 | 76 | final EntityPersister entityPersister = event.getPersister();
|
| 77 | + final Object entity = event.getEntity(); |
| 78 | + final BytecodeEnhancementMetadata instrumentationMetadata = entityPersister.getInstrumentationMetadata(); |
| 79 | + final LazyAttributeLoadingInterceptor lazyAttributeLoadingInterceptor; |
| 80 | + if ( instrumentationMetadata.isEnhancedForLazyLoading() ) { |
| 81 | + lazyAttributeLoadingInterceptor = instrumentationMetadata.extractInterceptor( entity ); |
| 82 | + } |
| 83 | + else { |
| 84 | + lazyAttributeLoadingInterceptor = null; |
| 85 | + } |
74 | 86 | for ( int i = 0; i < entityPersister.getPropertyNames().length; ++i ) {
|
75 | 87 | if ( !entityPersister.getPropertyUpdateability()[i] ) {
|
76 | 88 | // Assuming that PostUpdateEvent#getOldState() returns database state of the record before modification.
|
77 | 89 | // Otherwise, we would have to execute SQL query to be sure of @Column(updatable = false) column value.
|
78 | 90 | newDbState[i] = event.getOldState()[i];
|
79 | 91 | }
|
| 92 | + // Properties that have not been initialized need to be fetched in order to bind their value in the |
| 93 | + // AUDIT insert statement. |
| 94 | + if ( newDbState[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY ) { |
| 95 | + assert lazyAttributeLoadingInterceptor != null : "Entity:`" + entityPersister.getEntityName() + "` with uninitialized property:` " + entityPersister.getPropertyNames()[i] + "` hasn't an associated LazyAttributeLoadingInterceptor"; |
| 96 | + event.getOldState()[i] = newDbState[i] = lazyAttributeLoadingInterceptor.fetchAttribute( |
| 97 | + entity, |
| 98 | + entityPersister.getPropertyNames()[i] |
| 99 | + ); |
| 100 | + } |
80 | 101 | }
|
81 | 102 | }
|
82 | 103 | return newDbState;
|
|
0 commit comments