简体   繁体   English

JHipster:使用JHipster域语言(JDL)生成MongoDB实体

[英]JHipster : Generate MongoDB entities with JHipster Domain Language (JDL)

I'm trying JHipster with a MongoDB database. 我正在尝试将MongoDB数据库用于JHipster。 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… 为此,我想使用JDL格式来生成实体,存储库,服务,dtos ...

This is my actual JDL file: And it works : 这是我实际的JDL文件:它可以正常工作:

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 . 现在,我想补充一下Author可以写一Book的概念。

I can add an entity Author : 我可以添加一个实体Author

entity Author {
        firstane String required
        lastname LocalDate required
    }

My specific question is: How can I associate an Author and a Book ? 我的具体问题是:如何关联AuthorBook

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. 但这不起作用,因为NoSQL数据库不支持关系。 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. 使用NoSQL数据库,这成为一个更重要的问题。 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. 在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. 创建一个服务 ,该服务使用“作者存储库”查找作者,并使用“书籍存储库”获取具有相同作者ID的书籍。
  • Have a single Author entity in JHipster. 在JHipster中只有一个Author实体。 Model books as an array of embdedded documents for each Author . 将书籍建模为每个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. 不幸的是,JHipster似乎不允许您将列表类型定义为实体字段,因此您必须将其自己添加到Java代码中。
  • You may be able to use the $lookup feature in mongodb 3.2 to fetch Books for a given Author. 您可能可以使用mongodb 3.2中的$lookup功能来获取给定作者的书。 The DBRef feature in spring-data-mongo might help in this case. 在这种情况下,spring-data-mongo中的DBRef功能可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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