5

I'm trying JHipster with a MongoDB database. For my example I would like to store Books. To do that I would like to use the JDL format to be able to generate entities, repositories, services, dtos…

This is my actual JDL file: And it works :

entity Book {
    name String required
    date LocalDate required
}

dto all with mapstruct
paginate all with pager
service all with serviceImpl

Now, I would like to add the notion that a Book can be written by an Author.

I can add an entity Author:

entity Author {
        firstane String required
        lastname LocalDate required
    }

My specific question is: How can I associate an Author and a Book?

The documentation has this example :

relationship OneToMany {
  Author{book} to Book{writer(name) required}
}

But that's not working because NoSQL databases don't support relationships. So, how can I achieve that ?

Thanks.

Mohsen
  • 4,536
  • 2
  • 27
  • 49
  • You can generate collection in the database of Mongodb when importing from jdl ? – csu May 07 '18 at 18:54

1 Answers1

4

You haven't said exactly what you want to do with your entities. With NoSQL databases this becomes a more important question. Let's assume you want to return an author and all their books as a single document.

Here are some options:

  • Have two separate entities with no formal relationship in JHipster. Create a service that looks up an author using the Author Repository and also fetches the books with the same author id using the Book Repository.
  • Have a single Author entity in JHipster. Model books as an array of embdedded documents for each Author. Unfortunately JHipster does not seem to allow you to define list types as entity fields so you will have to add this to the Java code yourself.
  • You may be able to use the $lookup feature in mongodb 3.2 to fetch Books for a given Author. The DBRef feature in spring-data-mongo might help in this case.
Frank Wilson
  • 3,192
  • 1
  • 20
  • 29