@@ -727,7 +727,7 @@ public void internalApplyToDb(Cursor sourceCursor, Cursor<TARGET> targetCursor)
727
727
TARGET [] toRemoveFromDb ;
728
728
TARGET [] toPut ;
729
729
TARGET [] addedStandalone = null ;
730
- TARGET [] removedStandalone = null ;
730
+ List < TARGET > removedStandalone = null ;
731
731
732
732
boolean isStandaloneRelation = relationInfo .relationId != 0 ;
733
733
IdGetter <TARGET > targetIdGetter = relationInfo .targetInfo .getIdGetter ();
@@ -746,7 +746,7 @@ public void internalApplyToDb(Cursor sourceCursor, Cursor<TARGET> targetCursor)
746
746
entitiesAdded .clear ();
747
747
}
748
748
if (!entitiesRemoved .isEmpty ()) {
749
- removedStandalone = ( TARGET []) entitiesRemoved .keySet (). toArray ( );
749
+ removedStandalone = new ArrayList <>( entitiesRemoved .keySet ());
750
750
entitiesRemoved .clear ();
751
751
}
752
752
}
@@ -777,26 +777,51 @@ public void internalApplyToDb(Cursor sourceCursor, Cursor<TARGET> targetCursor)
777
777
throw new IllegalStateException ("Source entity has no ID (should have been put before)" );
778
778
}
779
779
780
- checkModifyStandaloneRelation (sourceCursor , entityId , removedStandalone , targetIdGetter , true );
781
- checkModifyStandaloneRelation (sourceCursor , entityId , addedStandalone , targetIdGetter , false );
780
+ if (removedStandalone != null ) {
781
+ removeStandaloneRelations (sourceCursor , entityId , removedStandalone , targetIdGetter );
782
+ }
783
+ if (addedStandalone != null ) {
784
+ addStandaloneRelations (sourceCursor , entityId , addedStandalone , targetIdGetter , false );
785
+ }
782
786
}
783
787
}
784
788
785
- private void checkModifyStandaloneRelation (Cursor cursor , long sourceEntityId , @ Nullable TARGET [] targets ,
786
- IdGetter <TARGET > targetIdGetter , boolean remove ) {
787
- if (targets != null ) {
788
- int length = targets .length ;
789
- long [] targetIds = new long [length ];
790
- for (int i = 0 ; i < length ; i ++) {
791
- long targetId = targetIdGetter .getId (targets [i ]);
792
- if (targetId == 0 ) {
793
- // Paranoia
794
- throw new IllegalStateException ("Target entity has no ID (should have been put before)" );
795
- }
796
- targetIds [i ] = targetId ;
789
+ /**
790
+ * The list of removed entities may contain non-persisted entities, which will be ignored (removed from the list).
791
+ */
792
+ private void removeStandaloneRelations (Cursor cursor , long sourceEntityId , List <TARGET > removed ,
793
+ IdGetter <TARGET > targetIdGetter ) {
794
+ Iterator <TARGET > iterator = removed .iterator ();
795
+ while (iterator .hasNext ()) {
796
+ if (targetIdGetter .getId (iterator .next ()) == 0 ) {
797
+ iterator .remove ();
798
+ }
799
+ }
800
+
801
+ int size = removed .size ();
802
+ if (size > 0 ) {
803
+ long [] targetIds = new long [size ];
804
+ for (int i = 0 ; i < size ; i ++) {
805
+ targetIds [i ] = targetIdGetter .getId (removed .get (i ));
806
+ }
807
+ cursor .modifyRelations (relationInfo .relationId , sourceEntityId , targetIds , true );
808
+ }
809
+ }
810
+
811
+ /** The target array may not contain non-persisted entities. */
812
+ private void addStandaloneRelations (Cursor cursor , long sourceEntityId , @ Nullable TARGET [] added ,
813
+ IdGetter <TARGET > targetIdGetter , boolean remove ) {
814
+ int length = added .length ;
815
+ long [] targetIds = new long [length ];
816
+ for (int i = 0 ; i < length ; i ++) {
817
+ long targetId = targetIdGetter .getId (added [i ]);
818
+ if (targetId == 0 ) {
819
+ // Paranoia
820
+ throw new IllegalStateException ("Target entity has no ID (should have been put before)" );
797
821
}
798
- cursor . modifyRelations ( relationInfo . relationId , sourceEntityId , targetIds , remove ) ;
822
+ targetIds [ i ] = targetId ;
799
823
}
824
+ cursor .modifyRelations (relationInfo .relationId , sourceEntityId , targetIds , remove );
800
825
}
801
826
802
827
/** For tests */
0 commit comments