Generator-jhipster: Feature Request : MongoDB DbRef Relationship

Created on 6 Sep 2016  路  9Comments  路  Source: jhipster/generator-jhipster

Dear all,

After a discussion on #901 , and as requested, i open this ticket.

I'm sorry i don't have time to play with the generator for now, but my suggestion will be very clear.

On mongoDB, the generator don't provide any possibile feature to include a link between two entities.

However, since version 3 of Mongodb (i guess), there is a possibility to add an anotation on a field, which allows to link 2 entities. But WITHOUT cascading save, which can be done manuallly.

I already do this manually, exemple here :

@Document(collection = "collection_data")
public class CollectionData implements Serializable {

    @Id
    private String id;

    @Field("name")
    private String name;

    @DBRef
    @Field("fields")
    private List<FieldData> fields;

}   

@Document(collection = "field_data")
public class  FieldData implements Serializable {

    @Id
    private String id;

    @Field("name")
    private String name;

    @DBRef
    @JsonBackReference
    private FieldData fieldParent;

    @DBRef
  @JsonBackReference
    @Field("collection")
    private CollectionData collectionData;

}

In this exemple, you can see all the possibilities you can have :

  • @DBref is the annotation allowing Mongodb to know that there is a relationship (Many-to-one, one-to-one bi/mono, Many-to-many) to another entity. Depending on which relation you need, you can declare a List / Set of objects or a single Object
  • @JsonBackReference : Allows to fix the "Infinite Loop" issue (tried a lot of solutions, but this one is the one working). This annotation needs to be put at ONE place only, to stop the infinite loop. (i can explain more if needed) ==> Note that we use this anotation only on bi-directionnal relationship

Once it's done, we can have the same generation for the front part. it works perfectly fine.

One remark : If you need to do that on a lot of tables ... it means that a relationship BD is better for you

Second Remark : This can also be done by placing an id in the first & second entity & just write additionnal requests to retreive it.

YeoMan Generator :

As i said, for now, i don't have a lot of time to play with the generator, but what can be added on generator is :

  • same questions for MySql
  • Replace the many-to-many , many-to-one & one-to-many by declaring a SET or List (The difference with MYSQL is that you can do a Bi-Directionnal or Mono-directionnal on all those possibilities, by adding the relashion in one entity & not the other)
  • put @Dbref on the field
  • put the @JsonBackReference when there is a Bi-Directionnal relashionship !!

This is my first contribution here, and i'd like to thank you for this project. It saved mine ... So the least i can do is to contribute also !! I'll do more when i'll have time (main reason i use Jhipster for)

I hope it'll help

Ps : Bravo Julien !!

area

Most helpful comment

This will be a great feature. Thanks!

All 9 comments

That's indeed a good feature, and we can also do the same kind of thing with Cassandra, so the code must be generic enough.

I don't think anybody is working on this? it's indeed a very good feature, and I've always wanted to have this from the very start, but if nobody is working on it let's close it.
We have a lot of opened bugs, and the AngularJS 2 release is the priority at the moment.
I might have some new trainees at Ippon Tech: if it happens, I will give them this task.

This will be a great feature. Thanks!

Yes, but if nobody works on it, it just won't be done.
I'm sorry I don't know MongoDB well enough to do it myself, and my current priority is AngularJS 2

I did thinking about this feature in the last days. @jdubois, It will be a great moment for taken to discuss subject again?

This is very interesting indeed 馃榿馃憤

Please contribute code, if you think, it's so interesting and worth doing.

Yes, there are also lots of people who contact me privately on this.

  • If people want to contribute: we have everything explained on GitHub, and nearly 350 people already contributed to the project. So you just need to do like everyone else.
  • Contacting me privately doesn't help: I have a day job, and tons of other stuff to do on JHipster. This is also against our guidelines, so don't be surprised if I don't answer you.

Sorry. You are right!

Thanks for your explanation.

Was this page helpful?
0 / 5 - 0 ratings