简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM