如何通过jhipster:import-jdl添加搜索框并配置关联实体选择?
jhipster:import-jdl for Your Two Requirements Hey there, let's walk through how to tackle both of your needs using JHipster's JDL and the import-jdl command.
1. Adding Search Boxes to Your Entities
To get search boxes on your Book and Author list pages, you just need to enable search functionality directly in your JDL. JHipster will handle generating all the backend logic and UI components automatically once you import the updated JDL.
First, update your JDL file to include the search directive for both entities. You have two options depending on your project's setup:
Option A: JPA Search (Simple Database Filtering)
This is great if you don't need advanced search features—it uses basic SQL queries to filter results:
entity Book { name String required minlength(2) } entity Author { name String required minlength(2) } relationship ManyToMany { Book{author(name)} to Author{book} } // Enable search for Book and Author using JPA search Book with JPA search Author with JPA
Option B: Elasticsearch (Advanced Full-Text Search)
If your project was set up with Elasticsearch (you chose it during project creation), use this instead for powerful full-text search:
// Same entity and relationship definitions as above // Enable Elasticsearch search search Book with Elasticsearch search Author with Elasticsearch
Once your JDL is updated, run the import command in your project root:
jhipster:import-jdl path/to/your/jdl-file.jdl
After the command finishes, your Book and Author list pages will have a search box at the top where users can type to filter results automatically.
2. Configuring Author Selection for Book Creation
Your existing JDL already has the correct setup for the many-to-many relationship, which will generate the exact behavior you want: a dropdown of existing Authors when creating a Book. Let me break it down:
- The line
relationship ManyToMany { Book{author(name)} to Author{book} }tells JHipster to use thenamefield of Author for display in the Book's UI (so the dropdown shows Author names instead of raw IDs). - When you run
jhipster:import-jdl, JHipster generates a multi-select dropdown component in the Book creation form. This component fetches all existing Author records from the backend API, letting users pick one or more Authors to associate with the new Book.
What to Expect After Import
- Frontend: In your frontend code (e.g., Angular's
book-update.component.html), you'll see a multi-select element populated with Author names. Users can select multiple Authors, which will be sent to the backend when saving the Book. - Backend: The backend will automatically handle saving the many-to-many association in the join table (generated by JHipster) between Book and Author.
If you ever need to adjust this (like limiting the number of selectable Authors), you can modify the generated frontend component code after running the import command—just be aware that re-running import-jdl will overwrite those changes unless you use JHipster's custom code hooks.
内容的提问来源于stack exchange,提问作者Mike

