Java 如何修改使用 jhipster 生成的现有实体?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28216307/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:48:39  来源:igfitidea点击:

How to modify existing entity generated with jhipster?

javahibernatejpajhipster

提问by Pracede

I use jhipster generator to create a projet. I've generated some entity manually following the information provided in jhipster documentation:

我使用 jhipster 生成器来创建一个项目。我按照jhipster 文档中提供的信息手动生成了一些实体:

If you prefer (or need) to do a database update manually, here is the development workflow: Modify your JPA entity (add a field, a relationship, etc.) Create a new "change log" in your src/main/resources/config/liquibase/changelog directory. The files in that directory are prefixed by their creation date (in yyyyMMddHHmmss format), and then have a title describing what they do. For example, 20141006152300_added_price_to_product.xml is a good name. Add this "change log" file in your src/main/resources/config/liquibase/master.xml file, so it is applied the next time you run your application If you want more information on using Liquibase, please go to http://www.liquibase.org.

如果您更喜欢(或需要)手动进行数据库更新,这里是开发工作流程:修改您的 JPA 实体(添加字段、关系等)在您的 src/main/resources/ 中创建一个新的“更改日志” config/liquibase/changelog 目录。该目录中的文件以其创建日期为前缀(yyyyMMddHHmmss 格式),然后有一个标题来描述它们的作用。例如,20141006152300_ added_price_to_product.xml 就是一个好名字。将此“更改日志”文件添加到您的 src/main/resources/config/liquibase/master.xml 文件中,以便下次运行您的应用程序时应用。如果您想了解有关使用 Liquibase 的更多信息,请访问http:/ /www.liquibase.org

Here the documentation just mention, how to add!!! What to do if i want to delete a field or a relation between entities (jpa entity) ?

这里文档刚刚提到,如何添加!!!如果我想删除一个字段或实体之间的关系(jpa 实体)怎么办?

Thanks

谢谢

回答by brevleq

In the same page you can see this sentence:

在同一个页面中你可以看到这句话:

If you have choosen to use MySQL or Postgresql in development, you can use the mvn liquibase:diff goal to automatically generate a changelog.

Liquibase Hibernate is a Maven plugin that is configured in your pom.xml, and is independant from your Spring application.yml file, so if you have changed the default settings (for example, changed the database password), you need to modify both files.

Here is the development workflow:

Modify your JPA entity (add a field, a relationship, etc.) Compile your application (this works on the compiled Java code, so don't forget to compile!) Run mvn liquibase:diff (or mvn compile liquibase:diff to compile before) A new "change log" is created in your src/main/resources/config/liquibase/changelog directory Review this change log and add it to your src/main/resources/config/liquibase/master.xml file, so it is applied the next time you run your application If you use Gradle instead of Maven, you can use the same workflow by running ./gradlew liquibaseDiffChangelog, and change the database configuration in liquibase.gradle if required.

如果您在开发中选择使用 MySQL 或 Postgresql,则可以使用 mvn liquibase:diff 目标自动生成变更日志。

Liquibase Hibernate 是一个 Maven 插件,配置在你的 pom.xml 中,独立于你的 Spring application.yml 文件,所以如果你更改了默认设置(例如更改了数据库密码),则需要同时修改这两个文件.

这是开发工作流程:

修改您的 JPA 实体(添加字段、关系等) 编译您的应用程序(这适用于已编译的 Java 代码,所以不要忘记编译!)运行 mvn liquibase:diff (或 mvn compile liquibase:diff 进行编译之前)在您的 src/main/resources/config/liquibase/changelog 目录中创建了一个新的“更改日志” 查看此更改日志并将其添加到您的 src/main/resources/config/liquibase/master.xml 文件中,因此它下次运行应用程序时应用如果您使用 Gradle 而不是 Maven,您可以通过运行 ./gradlew liquibaseDiffChangelog 来使用相同的工作流程,并在需要时更改 liquibase.gradle 中的数据库配置。

So you just need change your JPA entities (remove, add, etc), run mvn compile liquibase:diff, and put the changes in master.xml.

因此,您只需要更改 JPA 实体(删除、添加等),运行mvn compile liquibase:diff,并将更改放入 master.xml 中。

回答by Roberto

1) Edit the json file representing your entity (add/remove field, the syntax is pretty easy, check in the end of the file if is required any change to the general entity properties like 'fieldsContainOneToMany'...), you'll find it in:

1) 编辑代表您的实体的 json 文件(添加/删除字段,语法非常简单,如果需要对一般实体属性(如“fieldsContainOneToMany”...)进行任何更改,请检查文件末尾),您将在:

<jhipster_root_folder>/.jhipster/entityName.json

2) Build the code.

2)构建代码。

3) In the root of your project run the command:

3)在项目的根目录中运行命令:

yo jhipster:entity entityName

NOTE: this command by default overwrite all your manual changes. I recommend to do a commit on a VCS repository before to run this command to eventually revert any not required change.

注意:默认情况下,此命令会覆盖您的所有手动更改。我建议在运行此命令之前在 VCS 存储库上进行提交,以最终恢复任何不需要的更改。

4) run

4)运行

mvn liquibase:diff

回答by J?rg

You can also modify existing entitiesinteractively, e.g. using yo jhipster:entity Foofor entity Foo.

您还可以交互修改现有实体,例如使用yo jhipster:entity Foofor entity Foo

This way, you can regenerate all entities and dialogs.

这样,您可以重新生成所有实体和对话框。

If you are using a RDBMS and liquibase, you have to write the changelog file(s) and add it to the master.xml. What helps here is using mvn liquibase:diffor gradlew liquibaseDiff, which you can run against your existing DB. Though, I would recommend to write the changelog files manually.

如果您使用 RDBMS 和 liquibase,则必须编写更改日志文件并将其添加到 master.xml。这里有帮助的是使用mvn liquibase:diffor gradlew liquibaseDiff,您可以对现有数据库运行它。不过,我建议手动编写更改日志文件。

回答by Daniel Alves

What I've done to modify jhipster entity like a Relationship to add new tables was:

我为修改 jhipster 实体(如关系以添加新表)所做的工作是:

  1. (save project before anything happens :p) yo jhipster:entity TableToEdit and Edit whatever you want.Who to editTableOrAdd
  2. Then add the new tables.(optional)
  3. I had then I just had to do change or remove all the tables of the DataBase, to generate the new fields and relations on the SQL part automatically.
  1. (在任何事情发生之前保存项目:p)哟 jhipster:entity TableToEdit 和编辑任何你想要的。谁来编辑表或添加
  2. 然后添加新表。(可选)
  3. 然后我只需要更改或删除数据库的所有表,以自动生成 SQL 部分的新字段和关系。

Note: If after this steps, it has any error, serverError or something I regenerate de entityregenerate Entity. Of course if you want to keep all your data this is not a good alternative.

注意:如果在这一步之后,它有任何错误, serverError 或者我重新生成实体重新生成实体的东西。当然,如果您想保留所有数据,这不是一个好的选择。

回答by ThanhLD

I have solution worked for jhipster 3.4.2, it is the result of me in 1 day, hope it helpful to you :)

我有适用于 jhipster 3.4.2 的解决方案,这是我在 1 天内的结果,希望对您有所帮助:)

  1. Run yo jhipster:entity entityName
  2. Modify entities (add, remove, ...), at the end, jhipster will give question to override file, *IMPORTANT NOTE: select NOfor changelog .xmlfile and navbar.htmlfile.
  3. Back to project and run mvn compile liquibase:diff
  4. Run your application
  5. DONE
  1. yo jhipster:entity entityName
  2. 修改实体(添加,删除,...),最后,jhipster 会给出覆盖文件的问题, *重要提示:changelog .xml文件和navbar.html文件选择否
  3. 返回项目并运行 mvn compile liquibase:diff
  4. 运行您的应用程序
  5. 完毕

回答by Martin Naughton

For jhipster 5.7.0 you can run the entity command again and it will ask if you want to modify it.

对于 jhipster 5.7.0,您可以再次运行实体命令,它会询问您是否要修改它。

 % jhipster entity device                                 
INFO! Using JHipster version installed locally in current project's node_modules
INFO! Executing jhipster:entity device
INFO! Options: from-cli: true

Found the .jhipster/Device.json configuration file, entity can be automatically generated!


The entity device is being updated.

? Do you want to update the entity? This will replace the existing files for this entity, all your custom code will be overwritten 
  Yes, re generate the entity 
? Yes, add more fields and relationships 
  Yes, remove fields and relationships 
  No, exit

回答by Demobilizer

You can modify entity by tow ways.

您可以通过两种方式修改实体。

First(using jdl-studio):

首先(使用 jdl-studio):

update .jh or .jdl file by following steps:

通过以下步骤更新 .jh 或 .jdl 文件:

1) export .jh file using jhipster export-jdl myApp-jdl.jhcommand to root folder of your project. Also you can export the .jh file to particular path using jhipster export-jdl myPath/myApp-jdl.jh

1) 使用jhipster export-jdl myApp-jdl.jh命令将 .jh 文件导出到项目的根文件夹。您也可以使用将 .jh 文件导出到特定路径jhipster export-jdl myPath/myApp-jdl.jh

2) here, you can edit your existing entities. You can also add new entities.

2) 在这里,您可以编辑您现有的实体。您还可以添加新实体。

3) Then save this myApp-jdl.jh file to root folder of your project and run jhipster import-jdl myApp-jdl.jhor you can store it anywhere on your system and just give path to import from! like, jhipster import-jdl myPath/myApp-jdl.jh

3)然后将此 myApp-jdl.jh 文件保存到项目的根文件夹并运行,jhipster import-jdl myApp-jdl.jh或者您可以将其存储在系统上的任何位置,只需提供导入路径即可!喜欢,jhipster import-jdl myPath/myApp-jdl.jh

Second(using JHipster CLI):

第二(使用 JHipster CLI):

as suggested on official documentation jhipster.tech/creating-an-entity/you can update your entity with following steps:

按照官方文档jhipster.tech/creating-an-entity/ 的建议,您可以通过以下步骤更新您的实体:

1) type same command jhipster entity <my_entity_name>that we use to create entity using CLI. Here <my_entity_name>would be the entity name that we want to modify.

1) 键入与jhipster entity <my_entity_name>我们使用 CLI 创建实体相同的命令。这<my_entity_name>将是我们要修改的实体名称。

2) it'll give you different options like, re generate the entity, add more fields and relationships, remove fields and relationships etc..

2)它会给你不同的选项,比如重新生成实体,添加更多字段和关系,删除字段和关系等。

3) make modifications as per our requirement.

3) 根据我们的要求进行修改。

NOTE:it will automatically remove the manual changes that you did on pre-built JHipster project!

注意:它会自动删除您在预先构建的 JHipster 项目上所做的手动更改!

SOLUTION:

解决方案:

1) commit the code before modifying or adding new entity.

1) 在修改或添加新实体之前提交代码。

2) when it'll ask you to overwrite the changes to files, you can skip overwrite option as per your requirement. Just a suggestion is not to chage navbar html file.

2)当它要求您覆盖对文件的更改时,您可以根据需要跳过覆盖选项。只是一个建议是不要更改导航栏 html 文件。

3) after that, you can open it to Intellij or Eclipse. And then you can modify other changed files by local history.In Intellij, local historywill be in VCSmenu.

3) 之后,您可以将其打开到 Intellij 或 Eclipse。然后您可以通过本地历史记录修改其他更改的文件在 Intellij 中,本地历史记录将在VCS菜单中。