32
32
import java .util .Optional ;
33
33
import java .util .Set ;
34
34
import java .util .function .BiFunction ;
35
+ import java .util .function .BiPredicate ;
35
36
import java .util .function .Consumer ;
36
37
import java .util .function .Function ;
37
38
import java .util .function .Supplier ;
@@ -343,6 +344,16 @@ public <T> T save(T instance) {
343
344
return saveImpl (instance , Collections .emptyMap (), null );
344
345
}
345
346
347
+ @ Override
348
+ public <T > T saveAs (T instance , BiPredicate <PropertyPath , Neo4jPersistentProperty > includeProperty ) {
349
+
350
+ if (instance == null ) {
351
+ return null ;
352
+ }
353
+
354
+ return saveImpl (instance , TemplateSupport .computeIncludedPropertiesFromPredicate (this .neo4jMappingContext , instance .getClass (), includeProperty ), null );
355
+ }
356
+
346
357
@ Override
347
358
public <T , R > R saveAs (T instance , Class <R > resultType ) {
348
359
@@ -451,10 +462,10 @@ private <T> DynamicLabels determineDynamicLabels(T entityToBeSaved, Neo4jPersist
451
462
452
463
@ Override
453
464
public <T > List <T > saveAll (Iterable <T > instances ) {
454
- return saveAllImpl (instances , Collections .emptyMap ());
465
+ return saveAllImpl (instances , Collections .emptyMap (), null );
455
466
}
456
467
457
- private <T > List <T > saveAllImpl (Iterable <T > instances , Map <PropertyPath , Boolean > includedProperties ) {
468
+ private <T > List <T > saveAllImpl (Iterable <T > instances , @ Nullable Map <PropertyPath , Boolean > includedProperties , @ Nullable BiPredicate < PropertyPath , Neo4jPersistentProperty > includeProperty ) {
458
469
459
470
Set <Class <?>> types = new HashSet <>();
460
471
List <T > entities = new ArrayList <>();
@@ -469,13 +480,19 @@ private <T> List<T> saveAllImpl(Iterable<T> instances, Map<PropertyPath, Boolean
469
480
470
481
boolean heterogeneousCollection = types .size () > 1 ;
471
482
Class <?> domainClass = types .iterator ().next ();
483
+
484
+ Map <PropertyPath , Boolean > pps = includeProperty == null ?
485
+ includedProperties :
486
+ TemplateSupport .computeIncludedPropertiesFromPredicate (this .neo4jMappingContext , domainClass ,
487
+ includeProperty );
488
+
472
489
Neo4jPersistentEntity <?> entityMetaData = neo4jMappingContext .getRequiredPersistentEntity (domainClass );
473
490
if (heterogeneousCollection || entityMetaData .isUsingInternalIds () || entityMetaData .hasVersionProperty ()
474
491
|| entityMetaData .getDynamicLabelsProperty ().isPresent ()) {
475
492
log .debug ("Saving entities using single statements." );
476
493
477
494
NestedRelationshipProcessingStateMachine stateMachine = new NestedRelationshipProcessingStateMachine (neo4jMappingContext );
478
- return entities .stream ().map (e -> saveImpl (e , includedProperties , stateMachine )).collect (Collectors .toList ());
495
+ return entities .stream ().map (e -> saveImpl (e , pps , stateMachine )).collect (Collectors .toList ());
479
496
}
480
497
481
498
class Tuple3 <T > {
@@ -497,7 +514,7 @@ class Tuple3<T> {
497
514
// Save roots
498
515
@ SuppressWarnings ("unchecked" ) // We can safely assume here that we have a humongous collection with only one single type being either T or extending it
499
516
Function <T , Map <String , Object >> binderFunction = neo4jMappingContext .getRequiredBinderFunctionFor ((Class <T >) domainClass );
500
- binderFunction = TemplateSupport .createAndApplyPropertyFilter (includedProperties , entityMetaData , binderFunction );
517
+ binderFunction = TemplateSupport .createAndApplyPropertyFilter (pps , entityMetaData , binderFunction );
501
518
List <Map <String , Object >> entityList = entitiesToBeSaved .stream ().map (h -> h .modifiedInstance ).map (binderFunction )
502
519
.collect (Collectors .toList ());
503
520
Map <Value , Long > idToInternalIdMapping = neo4jClient
@@ -515,10 +532,16 @@ class Tuple3<T> {
515
532
Neo4jPersistentProperty idProperty = entityMetaData .getRequiredIdProperty ();
516
533
Object id = convertIdValues (idProperty , propertyAccessor .getProperty (idProperty ));
517
534
Long internalId = idToInternalIdMapping .get (id );
518
- return this .<T >processRelations (entityMetaData , propertyAccessor , t .wasNew , new NestedRelationshipProcessingStateMachine (neo4jMappingContext , t .originalInstance , internalId ), TemplateSupport .computeIncludePropertyPredicate (includedProperties , entityMetaData ));
535
+ return this .<T >processRelations (entityMetaData , propertyAccessor , t .wasNew , new NestedRelationshipProcessingStateMachine (neo4jMappingContext , t .originalInstance , internalId ), TemplateSupport .computeIncludePropertyPredicate (pps , entityMetaData ));
519
536
}).collect (Collectors .toList ());
520
537
}
521
538
539
+ @ Override
540
+ public <T > List <T > saveAllAs (Iterable <T > instances , BiPredicate <PropertyPath , Neo4jPersistentProperty > includeProperty ) {
541
+
542
+ return saveAllImpl (instances , null , includeProperty );
543
+ }
544
+
522
545
@ Override
523
546
public <T , R > List <R > saveAllAs (Iterable <T > instances , Class <R > resultType ) {
524
547
@@ -537,7 +560,7 @@ public <T, R> List<R> saveAllAs(Iterable<T> instances, Class<R> resultType) {
537
560
Map <PropertyPath , Boolean > pps = PropertyFilterSupport .addPropertiesFrom (commonElementType , resultType ,
538
561
projectionFactory , neo4jMappingContext );
539
562
540
- List <T > savedInstances = saveAllImpl (instances , pps );
563
+ List <T > savedInstances = saveAllImpl (instances , pps , null );
541
564
542
565
if (projectionInformation .isClosed ()) {
543
566
return savedInstances .stream ().map (instance -> projectionFactory .createProjection (resultType , instance ))
0 commit comments