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