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

Bug when using multiple relationships to same collection in one class #53

Closed
sverin opened this issue Jun 26, 2020 · 2 comments
Closed

Comments

@sverin
Copy link

sverin commented Jun 26, 2020

I found another thing that I think is a bug. In our model we would like to have a one-to-many relationship to another collection but in two different fields. But that seems to screw things up.

In the example below I have two fields on the Offer class, products and addons, referring with a OneToMany relationship to the same collection products. The products for both fields has been saved earlier and exists in the database.

@Document(collection = "offers")
@TypeAlias(value = "offer")
public class Offer {

  @Id
  private String id;

  private String name;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.NONE)
  @JoinProperty(name = "products")
  private List<Product> products;

  @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.NONE)
  @JoinProperty(name = "products")
  private List<Product> addons;

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

  private String name;

} 

Below is the data added. I have a ModelMapper converter that converts the string ids in products and addons to real objects before saving. And it works when I remove the relationship to addons.

{
  "name": "My Offer",
  "category": "CATEGORY_X",
  "subcategory": "SUB_CATEGORY_Y",
  "products": [
    "5ef5f73d9959571a685ade51"
  ],
  "addons": [
    "5ef5f75d9959571a685ade54"
  ],
  "rules": []
}

The result in MongoDB is a document that looks like this. It has saved the product reference from addon into the products field. And addon reference is completely missing.

{
    "_id" : ObjectId("5ef5f7ba9959571a685ade56"),
    "name" : "My Offer",
    "category" : "CATEGORY_X",
    "subcategory" : "SUB_CATEGORY_Y",
    "_class" : "offer",
    "products" : [ 
        {
            "_id" : ObjectId("5ef5f75d9959571a685ade54"),
            "_relmongo_target" : "products"
        }
    ]
}

Thanks for any help you could provide. It's a great library.

@kaiso
Copy link
Owner

kaiso commented Jun 26, 2020 via email

@sverin
Copy link
Author

sverin commented Jun 26, 2020

Thank you, I though that referred to the collection name to join with. Now it works fine.

@sverin sverin closed this as completed Jun 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants