|
11 | 11 | import org.hibernate.engine.spi.SharedSessionContractImplementor;
|
12 | 12 | import org.hibernate.internal.util.collections.ArrayHelper;
|
13 | 13 | import org.hibernate.tuple.NonIdentifierAttribute;
|
| 14 | +import org.hibernate.type.AnyType; |
| 15 | +import org.hibernate.type.BasicType; |
| 16 | +import org.hibernate.type.CollectionType; |
| 17 | +import org.hibernate.type.ComponentType; |
| 18 | +import org.hibernate.type.ManyToOneType; |
| 19 | +import org.hibernate.type.Type; |
| 20 | + |
| 21 | +import org.checkerframework.checker.nullness.qual.Nullable; |
14 | 22 |
|
15 | 23 | /**
|
16 | 24 | * Operations for searching an array of property values for modified elements.
|
@@ -72,6 +80,62 @@ else if ( previousState[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
|
72 | 80 | }
|
73 | 81 | }
|
74 | 82 |
|
| 83 | + /** |
| 84 | + * Determine if any of the given field values are dirty, returning an array containing |
| 85 | + * indices of the dirty fields. |
| 86 | + * <p> |
| 87 | + * If it is determined that no fields are dirty, null is returned. |
| 88 | + * |
| 89 | + * @param propertyTypes The property types that are dirty checkable. null entry for non-dirty checkable properties |
| 90 | + * @param currentState The current state of the entity |
| 91 | + * @param previousState The baseline state of the entity |
| 92 | + * @param includeColumns Columns to be included in the dirty checking, per property |
| 93 | + * @param session The session from which the dirty check request originated. |
| 94 | + * |
| 95 | + * @return Array containing indices of the dirty properties, or null if no properties considered dirty. |
| 96 | + */ |
| 97 | + public static int[] findDirty( |
| 98 | + @Nullable Type[] propertyTypes, |
| 99 | + final Object[] currentState, |
| 100 | + final Object[] previousState, |
| 101 | + final boolean[][] includeColumns, |
| 102 | + final SharedSessionContractImplementor session) { |
| 103 | + int[] results = null; |
| 104 | + int count = 0; |
| 105 | + int span = propertyTypes.length; |
| 106 | + |
| 107 | + for ( int i = 0; i < span; i++ ) { |
| 108 | + |
| 109 | + if ( isDirty( propertyTypes, currentState, previousState, includeColumns, session, i ) ) { |
| 110 | + if ( results == null ) { |
| 111 | + results = new int[span]; |
| 112 | + } |
| 113 | + results[count++] = i; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return count == 0 ? null : ArrayHelper.trim( results, count ); |
| 118 | + } |
| 119 | + |
| 120 | + private static boolean isDirty( |
| 121 | + @Nullable Type[] propertyTypes, |
| 122 | + Object[] currentState, |
| 123 | + Object[] previousState, |
| 124 | + boolean[][] includeColumns, |
| 125 | + SharedSessionContractImplementor session, int i) { |
| 126 | + final Type propertyType; |
| 127 | + if ( currentState[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY |
| 128 | + || ( propertyType = propertyTypes[i] ) == null ) { |
| 129 | + return false; |
| 130 | + } |
| 131 | + else if ( previousState[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY ) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + else { |
| 135 | + return propertyType.isDirty( previousState[i], currentState[i], includeColumns[i], session ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
75 | 139 | /**
|
76 | 140 | * Determine if any of the given field values are modified, returning an array containing
|
77 | 141 | * indices of the modified fields.
|
|
0 commit comments