Skip to content

Commit 479b873

Browse files
committed
HHH-8276 - Fix AnyType handling during Nullability checking
1 parent f111600 commit 479b873

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

hibernate-core/src/main/java/org/hibernate/engine/internal/Nullability.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
import java.util.Iterator;
2727

28-
import org.hibernate.EntityMode;
2928
import org.hibernate.HibernateException;
3029
import org.hibernate.PropertyValueException;
3130
import org.hibernate.bytecode.instrumentation.spi.LazyPropertyInitializer;
@@ -139,20 +138,32 @@ else if ( value != null ) {
139138
* @return property path
140139
* @throws HibernateException error while getting subcomponent values
141140
*/
142-
private String checkSubElementsNullability(final Type propertyType, final Object value)
143-
throws HibernateException {
144-
//for non null args, check for components and elements containing components
141+
private String checkSubElementsNullability(Type propertyType, Object value) throws HibernateException {
142+
// IMPL NOTE : we currently skip checking "any" and "many to any" mappings. This is not the best solution.
143+
//
144+
// The problem I ran into with performing the checks on "any" and "many to any" mappings had to do with
145+
// cascaded saves of transient associated entities not yet having assigned the identifier (this was
146+
// specifically in the "many to any" case).
147+
148+
if ( propertyType.isAnyType() ) {
149+
return null;
150+
}
151+
145152
if ( propertyType.isComponentType() ) {
146153
return checkComponentNullability( value, (CompositeType) propertyType );
147154
}
148-
else if ( propertyType.isCollectionType() ) {
149155

156+
if ( propertyType.isCollectionType() ) {
150157
//persistent collections may have components
151158
final CollectionType collectionType = (CollectionType) propertyType;
152159
final Type collectionElementType = collectionType.getElementType( session.getFactory() );
160+
161+
if ( collectionElementType.isAnyType() ) {
162+
return null;
163+
}
164+
153165
if ( collectionElementType.isComponentType() ) {
154166
//check for all components values in the collection
155-
156167
final CompositeType componentType = (CompositeType) collectionElementType;
157168
final Iterator itr = CascadingActions.getLoadedElementsIterator( session, collectionType, value );
158169
while ( itr.hasNext() ) {
@@ -163,6 +174,7 @@ else if ( propertyType.isCollectionType() ) {
163174
}
164175
}
165176
}
177+
166178
return null;
167179
}
168180

@@ -176,23 +188,22 @@ else if ( propertyType.isCollectionType() ) {
176188
* @return property path
177189
* @throws HibernateException error while getting subcomponent values
178190
*/
179-
private String checkComponentNullability(final Object value, final CompositeType compType)
180-
throws HibernateException {
191+
private String checkComponentNullability(Object value, CompositeType compType) throws HibernateException {
181192
/* will check current level if some of them are not null
182193
* or sublevels if they exist
183194
*/
184195
final boolean[] nullability = compType.getPropertyNullability();
185-
if ( nullability!=null ) {
196+
if ( nullability != null ) {
186197
//do the test
187-
final Object[] values = compType.getPropertyValues( value, EntityMode.POJO );
198+
final Object[] subValues = compType.getPropertyValues( value, session );
188199
final Type[] propertyTypes = compType.getSubtypes();
189-
for ( int i=0; i<values.length; i++ ) {
190-
final Object subvalue = values[i];
191-
if ( !nullability[i] && subvalue==null ) {
200+
for ( int i = 0; i < subValues.length; i++ ) {
201+
final Object subValue = subValues[i];
202+
if ( !nullability[i] && subValue==null ) {
192203
return compType.getPropertyNames()[i];
193204
}
194-
else if ( subvalue != null ) {
195-
final String breakProperties = checkSubElementsNullability( propertyTypes[i], subvalue );
205+
else if ( subValue != null ) {
206+
final String breakProperties = checkSubElementsNullability( propertyTypes[i], subValue );
196207
if ( breakProperties != null ) {
197208
return buildPropertyPath( compType.getPropertyNames()[i], breakProperties );
198209
}

0 commit comments

Comments
 (0)