Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JHipster : Generate MongoDB entities with JHipster Domain Language (JDL)

Tags:

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.

like image 370
jérémie GUIDOUX Avatar asked Jul 09 '17 16:07

jérémie GUIDOUX


People also ask

How do you regenerate entities in JHipster?

You can generate entities from a JDL file using the jdl sub-generator, by running jhipster jdl your-jdl-file. jh . If you do not want to regenerate your entities, while importing a JDL, you can use the --json-only flag to skip entity creation part and create only the json files in . jhipster folder.

What is JDL JHipster?

The JDL is a JHipster-specific domain language where you can describe all your applications, deployments, entities and their relationships in a single file (or more than one) with a user-friendly syntax.

How do you create a many to many relationship in JHipster?

If you want to create many entities and relationships, you might prefer to use a graphical tool. In that case, two options are available: JHipster UML, which allows you to use an UML editor. JDL Studio, our online tool to create entities and relationships using our domain-specific language.


1 Answers

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.
like image 157
Frank Wilson Avatar answered Oct 11 '22 14:10

Frank Wilson