@@ -66,6 +66,11 @@ class SqlGenerator {
66
66
static final SqlIdentifier IDS_SQL_PARAMETER = SqlIdentifier .unquoted ("ids" );
67
67
static final SqlIdentifier ROOT_ID_PARAMETER = SqlIdentifier .unquoted ("rootId" );
68
68
69
+ /**
70
+ * Length of an aggregate path that is one longer then the root path.
71
+ */
72
+ private static final int FIRST_NON_ROOT_LENTH = 2 ;
73
+
69
74
private final RelationalPersistentEntity <?> entity ;
70
75
private final RelationalMappingContext mappingContext ;
71
76
private final RenderContext renderContext ;
@@ -112,6 +117,40 @@ class SqlGenerator {
112
117
this .dialect = dialect ;
113
118
}
114
119
120
+ /**
121
+ * When deleting entities there is a fundamental difference between deleting
122
+ * <ol>
123
+ * <li>the aggregate root.</li>
124
+ * <li>a first level entity which still references the root id directly</li>
125
+ * <li>and all other entities which have to use a subselect to navigate from the id of the aggregate root to something
126
+ * referenced by the table in question.</li>
127
+ * </ol>
128
+ * For paths of the second kind this method returns {@literal true}.
129
+ *
130
+ * @param path the path to analyze.
131
+ * @return If the given path is considered deeply nested.
132
+ */
133
+ private static boolean isFirstNonRoot (AggregatePath path ) {
134
+ return path .getLength () == FIRST_NON_ROOT_LENTH ;
135
+ }
136
+
137
+ /**
138
+ * When deleting entities there is a fundamental difference between deleting
139
+ * <ol>
140
+ * <li>the aggregate root.</li>
141
+ * <li>a first level entity which still references the root id directly</li>
142
+ * <li>and all other entities which have to use a subselect to navigate from the id of the aggregate root to something
143
+ * referenced by the table in question.</li>
144
+ * </ol>
145
+ * For paths of the third kind this method returns {@literal true}.
146
+ *
147
+ * @param path the path to analyze.
148
+ * @return If the given path is considered deeply nested.
149
+ */
150
+ private static boolean isDeeplyNested (AggregatePath path ) {
151
+ return path .getLength () > FIRST_NON_ROOT_LENTH ;
152
+ }
153
+
115
154
/**
116
155
* Construct an IN-condition based on a {@link Select Sub-Select} which selects the ids (or stand-ins for ids) of the
117
156
* given {@literal path} to those that reference the root entities specified by the {@literal rootCondition}.
@@ -126,9 +165,8 @@ private Condition getSubselectCondition(AggregatePath path, Function<Column, Con
126
165
127
166
AggregatePath parentPath = path .getParentPath ();
128
167
129
- // TODO: Introduce methods to express what >2 and == 2 means
130
168
if (!parentPath .hasIdProperty ()) {
131
- if (parentPath . getLength () > 2 ) {
169
+ if (isDeeplyNested ( parentPath ) ) {
132
170
return getSubselectCondition (parentPath , rootCondition , filterColumn );
133
171
}
134
172
return rootCondition .apply (filterColumn );
@@ -140,7 +178,7 @@ private Condition getSubselectCondition(AggregatePath path, Function<Column, Con
140
178
141
179
Condition innerCondition ;
142
180
143
- if (parentPath . getLength () == 2 ) { // if the parent is the root of the path
181
+ if (isFirstNonRoot ( parentPath ) ) { // if the parent is the root of the path
144
182
145
183
// apply the rootCondition
146
184
innerCondition = rootCondition .apply (selectFilterColumn );
@@ -719,7 +757,7 @@ private String createDeleteByPathAndCriteria(AggregatePath path, Function<Column
719
757
720
758
Column filterColumn = table .column (path .getTableInfo ().reverseColumnInfo ().name ());
721
759
722
- if (path . getLength () == 2 ) {
760
+ if (isFirstNonRoot ( path ) ) {
723
761
724
762
delete = builder //
725
763
.where (rootCondition .apply (filterColumn )) //
0 commit comments