micronaut-projects / micronaut-data

Ahead of Time Data Repositories
Apache License 2.0
464 stars 197 forks source link

Mongo nested collection mapping doesn't work for referenced entities #1954

Open frynet opened 1 year ago

frynet commented 1 year ago

Expected Behavior

That's what I want to see:

  1. mongodb document reference
  2. similar in spring
  3. raw DBRef (manually filled using mongo db compass)

Actual Behaviour

I've tried to declare data entities for my mongo db and all I got is a null in a nested (via reference) collection.

That's what I got (in all 3 cases)

Steps To Reproduce

go to my repository and follow the instructions

Environment Information

Example Application

https://github.com/frynet/MicronautFailedMappingMongoNestedEntities

Version

3.7.3

radovanradic commented 1 year ago

Try adding this annotation @Join("bananas") on FruitRepoV1

@MongoRepository
@Join("bananas")
interface FruitRepoV1 : CrudRepository<FruitV1, String>

(or findById method) so when FruitV1 object is loaded from the db it will fetch mapped collection bananas (if that is your actual issue here)

frynet commented 1 year ago

Try adding this annotation @Join("bananas") on FruitRepoV1

@MongoRepository
@Join("bananas")
interface FruitRepoV1 : CrudRepository<FruitV1, String>

(or findById method) so when FruitV1 object is loaded from the db it will fetch mapped collection bananas (if that is your actual issue here)

Still getting null :(

radovanradic commented 1 year ago

I have created simple test with your model here but not sure if this is your case or you are trying something else. The test is in this sample repo using Micronaut Data MongoDB and Kotlin.

dstepanov commented 1 year ago

I think the reporter wants to use DBRef mongo type to store the association internally. Right now, we have just a simple object with an id field. We would need to come up with a way to store it differently, maybe it can be stored with DBRef when the relationship is annotated with @BsonRepresentation(BsonType.DB_POINTER)

radovanradic commented 1 year ago

Right, I saw example using relation mapping and was not sure if they wanted to use collection mapping that can be done in Micronaut since DBRef is not supported in Micronaut Data mongoDB. Sorry for adding confusion.