|
30 | 30 | import org.junit.jupiter.params.ParameterizedTest;
|
31 | 31 | import org.junit.jupiter.params.provider.ValueSource;
|
32 | 32 | import org.springframework.data.annotation.Transient;
|
| 33 | +import org.springframework.data.mapping.AssociationHandler; |
33 | 34 | import org.springframework.data.mapping.MappingException;
|
34 | 35 | import org.springframework.data.neo4j.core.schema.DynamicLabels;
|
35 | 36 | import org.springframework.data.neo4j.core.schema.GeneratedValue;
|
@@ -129,6 +130,71 @@ void doesFailOnRelationshipPropertiesWithMissingTargetNode() {
|
129 | 130 | .getPersistentEntity(EntityWithInCorrectRelationshipProperties.class))
|
130 | 131 | .withMessageContaining("Missing @TargetNode declaration in");
|
131 | 132 | }
|
| 133 | + |
| 134 | + @Test // GH-2289 |
| 135 | + void correctlyFindRelationshipObverseSameEntity() { |
| 136 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 137 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationship.class); |
| 138 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 139 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 140 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 141 | + }); |
| 142 | + } |
| 143 | + |
| 144 | + @Test // GH-2289 |
| 145 | + void correctlyFindRelationshipObverse() { |
| 146 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 147 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationshipToOtherEntity.class); |
| 148 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 149 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 150 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 151 | + }); |
| 152 | + persistentEntity = neo4jMappingContext.getPersistentEntity(OtherEntityWithBidirectionalRelationship.class); |
| 153 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 154 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 155 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 156 | + }); |
| 157 | + } |
| 158 | + |
| 159 | + @Test // GH-2289 |
| 160 | + void correctlyFindRelationshipObverseWithRelationshipProperties() { |
| 161 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 162 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationshipToOtherEntityWithRelationshipProperties.class); |
| 163 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 164 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 165 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 166 | + }); |
| 167 | + persistentEntity = neo4jMappingContext.getPersistentEntity(OtherEntityWithBidirectionalRelationship.class); |
| 168 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 169 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 170 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 171 | + }); |
| 172 | + } |
| 173 | + |
| 174 | + @Test // GH-2289 |
| 175 | + void correctlyFindSameEntityRelationshipObverseWithRelationshipProperties() { |
| 176 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 177 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityWithBidirectionalRelationshipProperties.class); |
| 178 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 179 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 180 | + assertThat(rd.getRelationshipObverse()).isNotNull(); |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + @Test // GH-2289 |
| 185 | + void correctlyDontFindRelationshipObverse() { |
| 186 | + Neo4jMappingContext neo4jMappingContext = new Neo4jMappingContext(); |
| 187 | + Neo4jPersistentEntity<?> persistentEntity = neo4jMappingContext.getPersistentEntity(EntityLooksLikeHasObserve.class); |
| 188 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 189 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 190 | + assertThat(rd.getRelationshipObverse()).isNull(); |
| 191 | + }); |
| 192 | + persistentEntity = neo4jMappingContext.getPersistentEntity(OtherEntityLooksLikeHasObserve.class); |
| 193 | + persistentEntity.doWithAssociations((AssociationHandler<Neo4jPersistentProperty>) a -> { |
| 194 | + RelationshipDescription rd = (RelationshipDescription) a; |
| 195 | + assertThat(rd.getRelationshipObverse()).isNull(); |
| 196 | + }); |
| 197 | + } |
132 | 198 | }
|
133 | 199 |
|
134 | 200 | @Nested
|
@@ -491,4 +557,123 @@ static class HasNoTargetNodeRelationshipProperties {
|
491 | 557 | @Id @GeneratedValue
|
492 | 558 | private Long id;
|
493 | 559 | }
|
| 560 | + |
| 561 | + @Node |
| 562 | + static class EntityWithBidirectionalRelationship { |
| 563 | + |
| 564 | + @Id @GeneratedValue |
| 565 | + private Long id; |
| 566 | + |
| 567 | + @Relationship("KNOWS") |
| 568 | + List<EntityWithBidirectionalRelationship> knows; |
| 569 | + |
| 570 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 571 | + List<EntityWithBidirectionalRelationship> knownBy; |
| 572 | + |
| 573 | + } |
| 574 | + |
| 575 | + @Node |
| 576 | + static class EntityWithBidirectionalRelationshipToOtherEntity { |
| 577 | + |
| 578 | + @Id @GeneratedValue |
| 579 | + private Long id; |
| 580 | + |
| 581 | + @Relationship("KNOWS") |
| 582 | + List<OtherEntityWithBidirectionalRelationship> knows; |
| 583 | + |
| 584 | + } |
| 585 | + |
| 586 | + @Node |
| 587 | + static class OtherEntityWithBidirectionalRelationship { |
| 588 | + |
| 589 | + @Id @GeneratedValue |
| 590 | + private Long id; |
| 591 | + |
| 592 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 593 | + List<EntityWithBidirectionalRelationshipToOtherEntity> knownBy; |
| 594 | + |
| 595 | + } |
| 596 | + |
| 597 | + @Node |
| 598 | + static class EntityWithBidirectionalRelationshipToOtherEntityWithRelationshipProperties { |
| 599 | + |
| 600 | + @Id @GeneratedValue |
| 601 | + private Long id; |
| 602 | + |
| 603 | + @Relationship("KNOWS") |
| 604 | + List<OtherEntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties> knows; |
| 605 | + |
| 606 | + } |
| 607 | + |
| 608 | + @Node |
| 609 | + static class OtherEntityWithBidirectionalRelationshipWithRelationshipProperties { |
| 610 | + |
| 611 | + @Id @GeneratedValue |
| 612 | + private Long id; |
| 613 | + |
| 614 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 615 | + List<EntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties> knownBy; |
| 616 | + |
| 617 | + } |
| 618 | + |
| 619 | + @RelationshipProperties |
| 620 | + static class OtherEntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties { |
| 621 | + @Id @GeneratedValue |
| 622 | + private Long id; |
| 623 | + |
| 624 | + @TargetNode |
| 625 | + OtherEntityWithBidirectionalRelationshipWithRelationshipProperties target; |
| 626 | + } |
| 627 | + |
| 628 | + @RelationshipProperties |
| 629 | + static class EntityWithBidirectionalRelationshipWithRelationshipPropertiesProperties { |
| 630 | + @Id @GeneratedValue |
| 631 | + private Long id; |
| 632 | + |
| 633 | + @TargetNode |
| 634 | + EntityWithBidirectionalRelationshipToOtherEntityWithRelationshipProperties target; |
| 635 | + } |
| 636 | + |
| 637 | + @Node |
| 638 | + static class EntityWithBidirectionalRelationshipProperties { |
| 639 | + |
| 640 | + @Id @GeneratedValue |
| 641 | + private Long id; |
| 642 | + |
| 643 | + @Relationship("KNOWS") |
| 644 | + List<BidirectionalRelationshipProperties> knows; |
| 645 | + |
| 646 | + @Relationship(type = "KNOWS", direction = Relationship.Direction.INCOMING) |
| 647 | + List<BidirectionalRelationshipProperties> knownBy; |
| 648 | + |
| 649 | + } |
| 650 | + |
| 651 | + @RelationshipProperties |
| 652 | + static class BidirectionalRelationshipProperties { |
| 653 | + |
| 654 | + @Id @GeneratedValue |
| 655 | + private Long id; |
| 656 | + |
| 657 | + @TargetNode |
| 658 | + EntityWithBidirectionalRelationshipProperties target; |
| 659 | + } |
| 660 | + |
| 661 | + @Node |
| 662 | + static class EntityLooksLikeHasObserve { |
| 663 | + @Id @GeneratedValue |
| 664 | + private Long id; |
| 665 | + |
| 666 | + @Relationship("KNOWS") |
| 667 | + private List<OtherEntityLooksLikeHasObserve> knows; |
| 668 | + } |
| 669 | + |
| 670 | + @Node |
| 671 | + static class OtherEntityLooksLikeHasObserve { |
| 672 | + @Id @GeneratedValue |
| 673 | + private Long id; |
| 674 | + |
| 675 | + @Relationship("KNOWS") |
| 676 | + private List<EntityLooksLikeHasObserve> knows; |
| 677 | + } |
| 678 | + |
494 | 679 | }
|
0 commit comments