How to delete an entity after creating it using jhipster?

若如初见. 提交于 2019-12-02 19:04:08

I use git scm for this. It's not just the generated files that need to be deleted. jHipster also modifies files so with those you need to keep the file but back out the change.

Each time I create an entity I do a separate commit so I can track what jHipster did for each entity.

To clear all changes since the last commit I do

git reset --hard
git clean -fd

If I've done that but I also need to back out the previous commit then I do

git reset --hard HEAD~1

Commits are local with git so it's not until you push these commits that they will be shared.

shacharsol

You should delete the following:

rm -rf   src/main/resources/config/liquibase/changelog/XXX_added_entity_YourEntity.xml
rm -rf src/main/java/com/radsphere/jeces/domain/YourEntity.java
rm -rf src/main/java/com/radsphere/jeces/repository/YourEntityRepository.java
rm -rf src/main/java/com/radsphere/jeces/web/rest/YourEntityResource.java 
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntitys.html
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity-detail.html
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity.js
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity.controller.js
rm -rf src/main/webapp/scripts/app/entities/YourEntity/YourEntity-detail.controller.js
rm -rf  src/main/webapp/scripts/components/entities/YourEntity/YourEntity.service.js
rm -rf src/test/java/com/radsphere/jeces/web/rest/YourEntityResourceTest.java
rm -rf src/main/webapp/i18n/en/YourEntity.json
rm -rf src/main/webapp/i18n/fr/YourEntity.json       

And remove the reference from config/liquibase/master.xml:

<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">

<include file="classpath:config/liquibase/changelog/00000000000000_initial_schema.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307175923_added_entity_Company.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180142_added_entity_UserTypeRecruiter.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180338_added_entity_UserTypeCandidate.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180448_added_entity_QuestionType.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180612_added_entity_Question.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307180849_added_entity_Answer.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307181033_added_entity_CandidateAnswer.xml" relativeToChangelogFile="false"/>
<!--<include file="classpath:config/liquibase/changelog/20150307181249_added_entity_your_removed_entity.xml" relativeToChangelogFile="false"/>-->
<include file="classpath:config/liquibase/changelog/20150307182736_added_entity_ExamTemplate.xml" relativeToChangelogFile="false"/>
<include file="classpath:config/liquibase/changelog/20150307182950_added_entity_Exam.xml" relativeToChangelogFile="false"/>
<!-- JHipster will add liquibase changelogs here -->

I do a pull request to add this feature: https://github.com/jhipster/generator-jhipster/pull/4369

To use it, it's quite simple:

yo jhipster:entity Foo --remove

It's remove the liquibase script but after you need to deal with the table/columns family already created and not yet dropped.

Please vote or comment the issue if you are interested by this : https://github.com/jhipster/generator-jhipster/issues/4372

You're right a delete command would be useful. But this command does not exist today. Perhaps it will be added. If you want to delete entity, you have to do it manually. So delete all generated files for your entity: domain, repository, rest, test, Angularjs controller & services, HTML view, link in menu, HTML view

it's simple you just need to delete the entity on the directory .jhipster/entityName.json and thats it.

You can run the "yo:jhipster entity" again and override everything else.

Meme.-

EDITED: added script for newer version of jhipster 3.9.0

My delete-entity.sh script for jhipster Release 2.26.2, based on @shacharsol 's answer.

Usage: ./delete-entity GROUP_ID ENTITY_NAME

GROUP_ID: com/subpackage/and/so/on/ (end with '/')

ENTITY_NAME: firstLowercaseNameOfEntity

Example: ./delete-entity uz/javlon/ transInfo

#!/usr/bin/env bash

echo "################";

if [ -z "$1" ];
then
    printf "Required argument GROUP_ID is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
    exit 1;
else
    GROUP_ID=$1;
    echo "GROUP_ID is set to '$1'.";
fi

if [ -z "$2" ];
then
    printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
    exit 1;
else
    ENTITY_NAME=$2;
    echo "ENTITY_NAME is set to '$2'.";
fi

echo;

rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${ENTITY_NAME^}.xml
rm -rf src/main/java/${GROUP_ID}domain/${ENTITY_NAME^}.java
rm -rf src/main/java/${GROUP_ID}repository/${ENTITY_NAME^}Repository.java
rm -rf src/main/java/${GROUP_ID}repository/search/${ENTITY_NAME^}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${ENTITY_NAME^}Resource.java

rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}s.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}

rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.search.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}

rm -rf src/test/java/${GROUP_ID}web/rest/${ENTITY_NAME^}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${ENTITY_NAME^}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.spec.js
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}

rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json

rm -rf ./.jhipster/${ENTITY_NAME^}.json

echo;
echo "Deleting ${ENTITY_NAME^} is completed.";
echo "################";

My another delete-entity.sh script for jhipster Release 3.9.0, based on @Tevfik Kiziloren's answer.

#!/usr/bin/env bash

echo;
if [ -z "$1" ];
then
    printf "Required argument GROUP_ID is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
    exit 1;
else
    GROUP_ID=$1;
    echo "GROUP_ID is set to '$1'.";
fi

if [ -z "$2" ];
then
    printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
    exit 1;
else
    ENTITY_NAME=$2;
    JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr  '[a-z]' '[A-Z]'`${ENTITY_NAME:1}
    echo "ENTITY_NAME is set to '$2'."
    echo "Java entity name inferred as: '${JAVA_ENTITY_NAME}'.";
fi

JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr  '[a-z]' '[A-Z]'`${ENTITY_NAME:1}
UNDERSCORED_FOLDER_NAME=`echo ${ENTITY_NAME} | sed -r 's/([a-z0-9])([A-Z])/\1-\L\2/g'`
echo ${UNDERSCORED_FOLDER_NAME};

QUESTION=$'You may want to keep definition file(.jhipster/'${JAVA_ENTITY_NAME}'.json) in case you want to regenerate entity in the future.\nDo you want to delete entity definition file also?'

while true; do
    read -p "${QUESTION}" yn
    case $yn in
        [Yy]* ) rm -rf ./.jhipster/${ENTITY_NAME}.json; break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo;
echo "Starting to delete files...";

rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${JAVA_ENTITY_NAME}.xml
rm -rf src/main/java/${GROUP_ID}domain/${JAVA_ENTITY_NAME}.java
rm -rf src/main/java/${GROUP_ID}repository/${JAVA_ENTITY_NAME}Repository.java
rm -rf src/main/java/${GROUP_ID}service/${JAVA_ENTITY_NAME}Service.java
rm -rf src/main/java/${GROUP_ID}service/impl/${JAVA_ENTITY_NAME}ServiceImpl.java
rm -rf src/main/java/${GROUP_ID}repository/search/${JAVA_ENTITY_NAME}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}Resource.java
rm -rf src/main/java/${GROUP_ID}web/rest/dto/${JAVA_ENTITY_NAME}DTO.java
rm -rf src/main/java/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}Mapper.java
rm -rf target/generated-sources/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}MapperImpl.java

rm -rf src/main/webapp/app/entities/${UNDERSCORED_FOLDER_NAME}/*

rm -rf src/test/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${JAVA_ENTITY_NAME}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${UNDERSCORED_FOLDER_NAME}/*
rm -rf src/test/javascript/spec/app/entities/${UNDERSCORED_FOLDER_NAME}

rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/fr/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json

echo "Deleting entity '${ENTITY_NAME}' is completed.";
echo;
echo "-----------------------------------------------------";
echo "Do not forget to manually correct these files also:  ";
echo "-----------------------------------------------------";
echo " --> src/main/webapp/index.html"
echo " --> src/main/webapp/scripts/compopnents/navbar.html"
echo " --> src/main/webapp/i18n/**/global.json"
echo " --> src/main/resources/config/liquibase/master.xml (if you use liquibase)"
echo " --> src/main/resources/config/mongeez/master.xml   (if you use mongodb)"

I've made some additions to SparX's and Meme's answers.

  • You need to delete DTO file generated for the entity, if you had chosen to "use DTO's" when generating your entity,
  • JHipster uses Mapstruct to map Entity and DTO. If you use DTO, then you need to delete mapper classes that JHipster generated for you
  • You need to remove javascript file references of deleted entities from "webapp/index.html"
  • You need to remove i18n definitions of deleted entity from "webapp/i18n/*/global.json"
  • You need to remove menu links from "webapp/scripts/components/navbar.html"
  • If you want to update update entity definition and regenerate entity in the future, you may keep ./.jhipster/${ENTITY_NAME}.json

I updated the SparX's bash script(delete-entity.sh) to ask for whether to delete entity definition file or not. Also the "${ENTITY_NAME^}" syntax (which is used for converting the first letter of the entity name to uppercase) is not run in older bash versions(such as 3.2).

The updated bash script is below. Just put this script in the folder which your pom.xml is located.

Example use-case: If your project's groupId "com.example" and if you want to delete entity named as "city", an example command is given below:

./delete-entity.sh com/example/ city

#!/usr/bin/env bash
echo;
if [ -z "$1" ];
then
    printf "Required argument GROUP_ID is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME.\n";
    exit 1;
else
    GROUP_ID=$1;
    echo "GROUP_ID is set to '$1'.";
fi

if [ -z "$2" ];
then
    printf "Required argument ENTITY_NAME is not set. \nUsage: ./delete-entity.sh GROUP_ID ENTITY_NAME .\n";
    exit 1;
else
    ENTITY_NAME=$2;
    JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr  '[a-z]' '[A-Z]'`${ENTITY_NAME:1} 
    echo "ENTITY_NAME is set to '$2'." 
    echo "Java entity name inferred as: '${JAVA_ENTITY_NAME}'.";
fi

JAVA_ENTITY_NAME=`echo ${ENTITY_NAME:0:1} | tr  '[a-z]' '[A-Z]'`${ENTITY_NAME:1} 


QUESTION=$'You may want to keep definition file(.jhipster/${JAVA_ENTITY_NAME}.json) in case you want to regenerate entity in the future.\nDo you want to delete entity definition file also?'

while true; do
    read -p "${QUESTION}" yn
    case $yn in
        [Yy]* ) rm -rf ./.jhipster/${ENTITY_NAME}.json; break;;
        [Nn]* ) break;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo;
echo "Starting to delete files...";

rm -rf src/main/resources/config/liquibase/changelog/*_added_entity_${JAVA_ENTITY_NAME}.xml
rm -rf src/main/java/${GROUP_ID}domain/${JAVA_ENTITY_NAME}.java
rm -rf src/main/java/${GROUP_ID}repository/${JAVA_ENTITY_NAME}Repository.java
rm -rf src/main/java/${GROUP_ID}service/${JAVA_ENTITY_NAME}Service.java
rm -rf src/main/java/${GROUP_ID}service/impl/${JAVA_ENTITY_NAME}ServiceImpl.java
rm -rf src/main/java/${GROUP_ID}repository/search/${JAVA_ENTITY_NAME}SearchRepository.java
rm -rf src/main/java/${GROUP_ID}web/rest/${JAVA_ENTITY_NAME}Resource.java
rm -rf src/main/java/${GROUP_ID}web/rest/dto/${JAVA_ENTITY_NAME}DTO.java
rm -rf src/main/java/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}Mapper.java
rm -rf target/generated-sources/${GROUP_ID}web/rest/mapper/${JAVA_ENTITY_NAME}MapperImpl.java

rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}s.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.html
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-delete-dialog.controller.js
rm -rf src/main/webapp/scripts/app/entities/${ENTITY_NAME}

rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}/${ENTITY_NAME}.search.service.js
rm -rf src/main/webapp/scripts/components/entities/${ENTITY_NAME}

rm -rf src/test/java/${GROUP_ID}web/rest/${ENTITY_NAME}ResourceIntTest.java
rm -rf src/test/gatling/simulations/${ENTITY_NAME}GatlingTest.scala
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}/${ENTITY_NAME}-detail.controller.spec.js
rm -rf src/test/javascript/spec/app/entities/${ENTITY_NAME}

rm -rf src/main/webapp/i18n/en/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/fr/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/ru/${ENTITY_NAME}.json
rm -rf src/main/webapp/i18n/uz/${ENTITY_NAME}.json

echo "Deleting entity '${ENTITY_NAME}' is completed.";
echo;
echo "-----------------------------------------------------";
echo "Do not forget to manually correct these files also:  ";
echo "-----------------------------------------------------";
echo " --> src/main/webapp/index.html"
echo " --> src/main/webapp/scripts/compopnents/navbar.html"
echo " --> src/main/webapp/i18n/**/global.json"
echo " --> src/main/resources/config/liquibase/master.xml (if you use liquibase)"
echo " --> src/main/resources/config/mongeez/master.xml   (if you use mongodb)"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!