29

I have created 3 entities (Author, Book, Library) using "yo jhipster:entity" command but in one entity (Library) I had a ManyToMany relationship (to Book) but that caused "mappedBy reference an unknown target entity property: com.tst.testdomain.domain.Book.librarys in com.tst.testdomain.docmain.Library.books" so what is the clean way of deleted the Library entity. Would a command like "yo jhipster:entitydelete" be useful?

vijay bhatt
  • 299
  • 1
  • 3
  • 3

8 Answers8

17

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.

Ben Thurley
  • 6,943
  • 4
  • 31
  • 54
  • 2
    Wow, I think that's my first downvote. I'd love to know why!? This answer is correct to the best of my knowledge and the git process works for me. Remeber this was answered in Feb and jHipster has changed a lot since then, maybe they added an actual delete? Either way using git is still a valid way to handle this. – Ben Thurley Nov 23 '15 at 10:17
  • The only problem I had with `git clean -fd` is that it removed my IDE meta files and any untracked files, which a huge inconvenience, so I stopped using it. (I didn't downvote you BTW) – DhafirNz Jan 24 '16 at 19:37
  • @DhafirNz you need to create a text file called .gitignore and list any untracked items like IDE meta files in there. https://git-scm.com/docs/gitignore – Ben Thurley Jan 24 '16 at 22:07
14

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

Duff
  • 852
  • 12
  • 16
11

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 -->

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
shacharsol
  • 2,326
  • 20
  • 14
6

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

Pracede
  • 4,226
  • 16
  • 65
  • 110
  • 2
    It is extremely manual (error prone) to modify relationships in an existing entity after adding a new entity using jhipster. What is the correct approach to changing relationships (or for adding new attributes) in an existing entity (say Book) to a new entity (say Library) be done automatically? Why can't the command "yo jhipster:entity book" when run a second (or many times) allow editing of book entity for adding new attributes and relationships be possible? The current approach requires the entity model to be fully understood upfront. – vijay bhatt Jan 30 '15 at 16:28
  • You can clean up the references to the now deleted entity by editing the appropriate json files found under .jhipster and removing the relationship from them. Then rerun the sub-generator (yo jhipster:entity ) to regenerate the files. It would be wise to do this under version control so you can watch the changes that are made. – JayL Mar 26 '15 at 03:56
6

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.-

Meme
  • 69
  • 1
  • 1
  • 1
    Quite ok; if you wish to remove the entity altogether there are a few files to be deleted manually: ./src/test/java/.../web/rest/BookResourceIntTest.java ./src/test/gatling/simulations/BookGatlingTest.scala ./src/main/java/.../web/rest/BookResource.java ./src/main/java/.../repository/BookRepository.java ./src/main/java/.../repository/search/BookSearchRepository.java ./src/main/resources/config/liquibase/changelog/20160502221559_added_entity_Book.xml – lrkwz May 04 '16 at 13:36
4

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)"
SparX
  • 271
  • 2
  • 6
  • 15
3

In jHipster 7.0.1 you should delete the following files:

jhipster/[EntityName].json
src/main/java/[base package]/domain/[EntityName].java
src/main/java/[base package]/repository/[EntityName]Repository.java
src/main/java/[base package]/repository/search/[EntityName]SearchRepository.java
src/main/java/[base package]/service/[EntityName]Service.java
src/main/java/[base package]/service/dto/[EntityName]DTO.java
src/main/java/[base package]/service/impl/[EntityName]ServiceImpl.java
src/main/java/[base package]/service/mapper/[EntityName]Mapper.java
src/main/java/[base package]/web/rest/[EntityName]Resource.java
src/main/resources/config/liquibase/changelog/*_added_entity_[EntityName].xml
src/main/resources/config/liquibase/changelog/*_added_entity_constraints_[EntityName].xml
src/main/resources/config/liquibase/fake-data/[entity_name].csv
src/test/java/[base package]/domain/[EntityName]Test.java
src/test/java/[base package]/repository/search/[EntityName]SearchRepositoryMockConfiguration.java
src/test/java/[base package]/service/dto/[EntityName]DTOTest.java
src/test/java/[base package]/service/mapper/[EntityName]MapperTest.java
src/test/java/[base package]/web/rest/[EntityName]ResourceIT.java

Delete the whole directory:

src/main/webapp/app/entities/[entity-name]

Remove the references from the following files:

.yo-rc.json
src/main/java/[base package]/config/CacheConfiguration.java
src/main/resources/config/liquibase/master.xml
src/main/webapp/app/entities/entity-routing.module.ts
src/main/webapp/app/layouts/navbar/navbar.component.html
src/main/webapp/i18n/en/global.json
src/main/webapp/i18n/pt-br/global.json

Drop the database and let the app generate it again when starting up. If you can't delete the database, instead of deleting the liquibase files you will need to create a changelog to delete the obsolete table.

Obviusly depending on how you generated your project you might have less files (or maybe more files).

0

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)"
Tevfik Kiziloren
  • 147
  • 3
  • 10