Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi level relations loading support #52

Closed
sverin opened this issue Jun 25, 2020 · 6 comments
Closed

Add multi level relations loading support #52

sverin opened this issue Jun 25, 2020 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@sverin
Copy link

sverin commented Jun 25, 2020

I having an issue when I fetching objects that have relationship in more than one level. I have 3 different classes that reference to each other by a one-to-one relationship, as shown below.

@Document(collection = "products")
@TypeAlias(value = "product")
public class Product {
  
  @Id
  private String id;

  private String name;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinProperty(name = "variations")
  private List<ProductVariation> variations;

}
@Document(collection = "variations")
@TypeAlias(value = "variation")
public class ProductVariation {

  @Id
  private String variationId;

  private String name;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
  @JoinProperty(name = "options")
  private List<Option> options;

}
@Document(collection = "options")
@TypeAlias(value = "option")
public class Option {

  @Id
  private String optionId;

  private String text;

}

Through a spring-controller and spring repository I can add some data to MongoDB database. But then my issue starts. As you can see in the repsonse below the option does not have any text value when I use a ProductRepository to fetch the data. I have checked the MongoDB database and the data has been saved correctly.

[
  {
    "id": "5ef4998e0a994e434d6cce86",
    "name": "My Product Name",
    "variations": [
      {
        "id": "5ef4998e0a994e434d6cce84",
        "name": "My Product Variation Name",
        "options": [
          {
            "id": "5ef4998e0a994e434d6cce88",
            "text": null
          }
        ]
      }
    ]
  }
]

However, if I use a ProductVariationRepository and do the fetch one level down, it works perfectly.

[
  {
    "id": "5ef4998e0a994e434d6cce84",
    "name": "My Product Variation Name",
    "options": [
      {
        "id": "5ef4998e0a994e434d6cce88",
        "text": "My options value"
      }
    ]
  }
]

Is this a bug? Can't you have multi level relationship? Or have I missed something in my configuration ?

@kaiso kaiso self-assigned this Jun 25, 2020
@kaiso kaiso added the enhancement New feature or request label Jun 25, 2020
@kaiso kaiso changed the title Fetching issue in nested objects Add multi level relations loading support Jun 25, 2020
@sverin
Copy link
Author

sverin commented Jun 25, 2020

Thank you for your quick response. Its a great library, and this would make it even better.

@kaiso
Copy link
Owner

kaiso commented Jun 29, 2020

Hello @sverin I've published a release candidate version 3.4.0-RC1 to address the multi level loading feature.
can your test and give me your feedback please?
Thank you

@sverin
Copy link
Author

sverin commented Jun 30, 2020

I have tried it today and it works perfectly. It all works as I expected now. I will try it out some more until friday and if I have some issues I'll let you know. Thanks for your quick response. This framework just become even better.

@sverin
Copy link
Author

sverin commented Jul 6, 2020

I have tried it now for a couple of days and it works fine. If you feel comfortable with the code you can close the issue and make a final release.

@kaiso
Copy link
Owner

kaiso commented Jul 7, 2020

Thank you @sverin I will release the version then.

@kaiso kaiso closed this as completed Jul 7, 2020
@loehnertz
Copy link

loehnertz commented Dec 15, 2020

@sverin
I have almost exactly the same setup as you posted in your first comment above.
The only differences are that I have @OneToOne relations and that I am using Kotlin instead of Java (I don't think this causes a problem in this particular case, but it could potentially).
Apart from that, it's the same. I am running on v3.4.1. I also have three collections with the same nesting structure.
However, I cannot get it to work. For me, all fields are null when loaded out of MongoDB again. To me, it looks like the relation is never resolved after some debugging deep in Spring Data (spring-data-commons-2.3.5) (I receive a MappingInstantiationException). The persisting works just fine. With only one level of relation, it also works perfectly.

When you wrote:

I have tried it today and it works perfectly. It all works as I expected now.

Did you refer to the same setup as you posted initially or did you change some details afterward in your running solution?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants