|
29 | 29 | import org.junit.jupiter.api.Test;
|
30 | 30 | import org.junit.jupiter.params.ParameterizedTest;
|
31 | 31 | import org.junit.jupiter.params.provider.ValueSource;
|
| 32 | +import org.springframework.data.mapping.AssociationHandler; |
32 | 33 | import org.springframework.data.mapping.MappingException;
|
33 | 34 | import org.springframework.data.neo4j.core.schema.DynamicLabels;
|
34 | 35 | import org.springframework.data.neo4j.core.schema.GeneratedValue;
|
@@ -110,6 +111,71 @@ void doesFailOnRelationshipPropertiesWithMissingTargetNode() {
|
110 | 111 | .getPersistentEntity(EntityWithInCorrectRelationshipProperties.class))
|
111 | 112 | .withMessageContaining("Missing @TargetNode declaration in");
|
112 | 113 | }
|
| 114 | + |
| 115 | + @Test // GH-2289 |
| 116 | + void correctlyFindRelationshipObverseSameEntity() { |
| 117 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 118 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationship.class); |
| 119 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 120 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 121 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + @Test // GH-2289 |
| 126 | + void correctlyFindRelationshipObverse() { |
| 127 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 128 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationshipToOtherEntity.class); |
| 129 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 130 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 131 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 132 | + }); |
| 133 | + persistentEntity = neo4jMappingContext.getPersistentEntity(OtherEntityWithBidirectionalRelationship.class); |
| 134 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 135 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 136 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 137 | + }); |
| 138 | + } |
| 139 | + |
| 140 | + @Test // GH-2289 |
| 141 | + void correctlyFindRelationshipObverseWithRelationshipProperties() { |
| 142 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 143 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationshipToOtherEntityWithRelationshipProperties.class); |
| 144 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 145 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 146 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 147 | + }); |
| 148 | + persistentEntity = neo4jMappingContext.getPersistentEntity(OtherEntityWithBidirectionalRelationship.class); |
| 149 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 150 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 151 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 152 | + }); |
| 153 | + } |
| 154 | + |
| 155 | + @Test // GH-2289 |
| 156 | + void correctlyFindSameEntityRelationshipObverseWithRelationshipProperties() { |
| 157 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 158 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationshipProperties.class); |
| 159 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 160 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 161 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 162 | + }); |
| 163 | + } |
| 164 | + |
| 165 | + @Test // GH-2289 |
| 166 | + void correctlyDontFindRelationshipObverse() { |
| 167 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 168 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityLooksLikeHasObserve.class); |
| 169 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 170 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 171 | + assertThat(rd.getRelationshipObverse()).isNull(); |
| 172 | + }); |
| 173 | + persistentEntity = neo4jMappingContext.getPersistentEntity(OtherEntityLooksLikeHasObserve.class); |
| 174 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 175 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 176 | + assertThat(rd.getRelationshipObverse()).isNull(); |
| 177 | + }); |
| 178 | + } |
113 | 179 | }
|
114 | 180 |
|
115 | 181 | @Nested
|
@@ -444,4 +510,123 @@ static class HasNoTargetNodeRelationshipProperties {
|
444 | 510 | @Id @GeneratedValue
|
445 | 511 | private Long id;
|
446 | 512 | }
|
| 513 | + |
| 514 | + @Node |
| 515 | + static class EntityWithBidirectionalRelationship { |
| 516 | + |
| 517 | + @Id @GeneratedValue |
| 518 | + private Long id; |
| 519 | + |
| 520 | + @Relationship("KNOWS") |
| 521 | + List<EntityWithBidirectionalRelationship> knows; |
| 522 | + |
| 523 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 524 | + List<EntityWithBidirectionalRelationship> knownBy; |
| 525 | + |
| 526 | + } |
| 527 | + |
| 528 | + @Node |
| 529 | + static class EntityWithBidirectionalRelationshipToOtherEntity { |
| 530 | + |
| 531 | + @Id @GeneratedValue |
| 532 | + private Long id; |
| 533 | + |
| 534 | + @Relationship("KNOWS") |
| 535 | + List<OtherEntityWithBidirectionalRelationship> knows; |
| 536 | + |
| 537 | + } |
| 538 | + |
| 539 | + @Node |
| 540 | + static class OtherEntityWithBidirectionalRelationship { |
| 541 | + |
| 542 | + @Id @GeneratedValue |
| 543 | + private Long id; |
| 544 | + |
| 545 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 546 | + List<EntityWithBidirectionalRelationshipToOtherEntity> knownBy; |
| 547 | + |
| 548 | + } |
| 549 | + |
| 550 | + @Node |
| 551 | + static class EntityWithBidirectionalRelationshipToOtherEntityWithRelationshipProperties { |
| 552 | + |
| 553 | + @Id @GeneratedValue |
| 554 | + private Long id; |
| 555 | + |
| 556 | + @Relationship("KNOWS") |
| 557 | + List<OtherEntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties> knows; |
| 558 | + |
| 559 | + } |
| 560 | + |
| 561 | + @Node |
| 562 | + static class OtherEntityWithBidirectionalRelationshipWithRelationshipProperties { |
| 563 | + |
| 564 | + @Id @GeneratedValue |
| 565 | + private Long id; |
| 566 | + |
| 567 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 568 | + List<EntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties> knownBy; |
| 569 | + |
| 570 | + } |
| 571 | + |
| 572 | + @RelationshipProperties |
| 573 | + static class OtherEntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties { |
| 574 | + @Id @GeneratedValue |
| 575 | + private Long id; |
| 576 | + |
| 577 | + @TargetNode |
| 578 | + OtherEntityWithBidirectionalRelationshipWithRelationshipProperties target; |
| 579 | + } |
| 580 | + |
| 581 | + @RelationshipProperties |
| 582 | + static class EntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties { |
| 583 | + @Id @GeneratedValue |
| 584 | + private Long id; |
| 585 | + |
| 586 | + @TargetNode |
| 587 | + EntityWithBidirectionalRelationshipToOtherEntityWithRelationshipProperties target; |
| 588 | + } |
| 589 | + |
| 590 | + @Node |
| 591 | + static class EntityWithBidirectionalRelationshipProperties { |
| 592 | + |
| 593 | + @Id @GeneratedValue |
| 594 | + private Long id; |
| 595 | + |
| 596 | + @Relationship("KNOWS") |
| 597 | + List<BidirectionalRelationshipProperties> knows; |
| 598 | + |
| 599 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 600 | + List<BidirectionalRelationshipProperties> knownBy; |
| 601 | + |
| 602 | + } |
| 603 | + |
| 604 | + @RelationshipProperties |
| 605 | + static class BidirectionalRelationshipProperties { |
| 606 | + |
| 607 | + @Id @GeneratedValue |
| 608 | + private Long id; |
| 609 | + |
| 610 | + @TargetNode |
| 611 | + EntityWithBidirectionalRelationshipProperties target; |
| 612 | + } |
| 613 | + |
| 614 | + @Node |
| 615 | + static class EntityLooksLikeHasObserve { |
| 616 | + @Id @GeneratedValue |
| 617 | + private Long id; |
| 618 | + |
| 619 | + @Relationship("KNOWS") |
| 620 | + private List<OtherEntityLooksLikeHasObserve> knows; |
| 621 | + } |
| 622 | + |
| 623 | + @Node |
| 624 | + static class OtherEntityLooksLikeHasObserve { |
| 625 | + @Id @GeneratedValue |
| 626 | + private Long id; |
| 627 | + |
| 628 | + @Relationship("KNOWS") |
| 629 | + private List<EntityLooksLikeHasObserve> knows; |
| 630 | + } |
| 631 | + |
447 | 632 | }
|
0 commit comments