Skip to content

Commit b2c1660

Browse files
GH-2123 - Select correct conversion for assigned ids of related nodes.
We must select the source entity and it's associcated id property for correctly picking up the right conversion as the source is only matched by the assigned id. It does not matter for new related target noode as that one is created or merged in the same step and we can work with its internal id. This fixes #2123.
1 parent c7991d0 commit b2c1660

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

src/main/java/org/springframework/data/neo4j/core/Neo4jTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ private void processNestedRelations(Neo4jPersistentEntity<?> sourceEntity, Objec
528528
sourceEntity, relationshipContext, relatedValueToStore);
529529

530530
Optional<Long> relationshipInternalId = neo4jClient.query(renderer.render(statementHolder.getStatement())).in(inDatabase)
531-
.bind(convertIdValues(targetEntity.getRequiredIdProperty(), fromId)) //
531+
.bind(convertIdValues(sourceEntity.getRequiredIdProperty(), fromId)) //
532532
.to(Constants.FROM_ID_PARAMETER_NAME)
533533
.bind(relatedInternalId) //
534534
.to(Constants.TO_ID_PARAMETER_NAME) //

src/main/java/org/springframework/data/neo4j/core/ReactiveNeo4jTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,7 @@ private Mono<Void> processNestedRelations(Neo4jPersistentEntity<?> sourceEntity,
537537
// in case of no properties the bind will just return an empty map
538538
Mono<Long> relationshipCreationMonoNested = neo4jClient
539539
.query(renderer.render(statementHolder.getStatement())).in(inDatabase)
540-
.bind(convertIdValues(targetEntity.getRequiredIdProperty(), fromId)) //
540+
.bind(convertIdValues(sourceEntity.getRequiredIdProperty(), fromId)) //
541541
.to(Constants.FROM_ID_PARAMETER_NAME) //
542542
.bind(relatedInternalId) //
543543
.to(Constants.TO_ID_PARAMETER_NAME) //

src/test/java/org/springframework/data/neo4j/integration/properties/DomainClasses.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
*/
1616
package org.springframework.data.neo4j.integration.properties;
1717

18+
import lombok.AllArgsConstructor;
1819
import lombok.Getter;
1920
import lombok.Setter;
2021

22+
import java.util.Date;
2123
import java.util.HashMap;
2224
import java.util.List;
2325
import java.util.Map;
@@ -26,9 +28,11 @@
2628
import org.springframework.data.neo4j.core.schema.GeneratedValue;
2729
import org.springframework.data.neo4j.core.schema.Id;
2830
import org.springframework.data.neo4j.core.schema.Node;
31+
import org.springframework.data.neo4j.core.schema.Property;
2932
import org.springframework.data.neo4j.core.schema.Relationship;
3033
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
3134
import org.springframework.data.neo4j.core.schema.TargetNode;
35+
import org.springframework.data.neo4j.core.support.DateLong;
3236

3337
/**
3438
* @author Michael J. Simons
@@ -127,4 +131,18 @@ static class SimplePropertyContainerWithVersion extends SimplePropertyContainer
127131
@Version
128132
private Long version;
129133
}
134+
135+
@Node
136+
@AllArgsConstructor
137+
@Getter @Setter
138+
static class WeirdSource {
139+
140+
@Id
141+
@Property("id")
142+
@DateLong
143+
private Date myFineId;
144+
145+
@Relationship(type = "ITS_COMPLICATED")
146+
IrrelevantTargetContainer irrelevantTargetContainer;
147+
}
130148
}

src/test/java/org/springframework/data/neo4j/integration/properties/PropertyIT.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.Arrays;
2121
import java.util.Collections;
22+
import java.util.Date;
2223
import java.util.Optional;
2324

2425
import org.junit.jupiter.api.BeforeAll;
@@ -220,6 +221,20 @@ void relationshipIdsShouldBeFilledDynamicRelationships() {
220221
assertThat(source2.getRels().get("DYN_REL").getId()).isNotNull();
221222
}
222223

224+
@Test // GH-2123
225+
void customConvertersForRelsMustBeTakenIntoAccount() {
226+
227+
Date now = new Date();
228+
DomainClasses.WeirdSource source = new DomainClasses.WeirdSource(now, new DomainClasses.IrrelevantTargetContainer());
229+
template.save(source).getMyFineId();
230+
try (Session session = driver.session()) {
231+
long cnt = session
232+
.run("MATCH (m) - [r:ITS_COMPLICATED] -> (n) WHERE m.id = $id RETURN count(m)",
233+
Collections.singletonMap("id", now.getTime())).single().get(0).asLong();
234+
assertThat(cnt).isEqualTo(1L);
235+
}
236+
}
237+
223238
@Configuration
224239
@EnableTransactionManagement
225240
static class Config extends AbstractNeo4jConfig {

src/test/java/org/springframework/data/neo4j/integration/properties/ReactivePropertyIT.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.ArrayList;
2323
import java.util.Arrays;
2424
import java.util.Collections;
25+
import java.util.Date;
2526
import java.util.List;
2627

2728
import org.junit.jupiter.api.BeforeAll;
@@ -235,6 +236,23 @@ void relationshipIdsShouldBeFilledDynamicRelationships() {
235236
.verifyComplete();
236237
}
237238

239+
@Test // GH-2123
240+
void customConvertersForRelsMustBeTakenIntoAccount() {
241+
242+
Date now = new Date();
243+
template.save(new DomainClasses.WeirdSource(now, new DomainClasses.IrrelevantTargetContainer()))
244+
.as(StepVerifier::create)
245+
.expectNextCount(1)
246+
.verifyComplete();
247+
248+
try (Session session = driver.session()) {
249+
long cnt = session
250+
.run("MATCH (m) - [r:ITS_COMPLICATED] -> (n) WHERE m.id = $id RETURN count(m)",
251+
Collections.singletonMap("id", now.getTime())).single().get(0).asLong();
252+
assertThat(cnt).isEqualTo(1L);
253+
}
254+
}
255+
238256
@Configuration
239257
@EnableTransactionManagement
240258
static class Config extends AbstractReactiveNeo4jConfig {

0 commit comments

Comments
 (0)