generator-jhipster: ManyToMany relationship generates wrong relationship table name

Overview of the issue

I use JDL file

entity Material {
    name String
}
entity MaterialOrigin {
    name String
}
relationship ManyToMany {
    Material{origin} to MaterialOrigin{material}
}

This wrongly generates relationship table name as “material_origin” instead of “material_origin_material” in files:

  • .../domain/Material.java
  • .../changelog/..._added_entity_constraints_Material.xml
  • .../changelog/..._added_entity_Material.xml

The issue reveals itself on ./mvnw with the error

WARN 26590 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'auditEventsEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/audit/AuditEventsEndpointAutoConfiguration.class]: Unsatisfied dependency expressed through method 'auditEventsEndpoint' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAuditEventRepository' defined in file [.../target/classes/com/company/app/repository/CustomAuditEventRepository.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'persistenceAuditEventRepository': Cannot create inner bean '(inner bean)#1c95bc62' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1c95bc62': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.MappingException: Foreign key (FKipo7e0wsvsnrag9hoy1hnmeb6:material_origin [origin_id])) must have same number of columns as the referenced primary key (material_origin [material_id,origin_id])
Motivation for or Use Case

The app fails to run without a proper relation table. The material_origin_material is not even described anywhere despite being referenced from .../changelog/..._added_entity_MaterialOrigin.xml.

Reproduce the error
  1. Initialize fresh app with jhipster and all the defaults (just hit enter like 10 times)
  2. Save the JDL above to the jhipster-many-to-many-bug.jdl
  3. Bootstrap models with jhipster import-jdl ./jhipster-many-to-many-bug.jdl --force
  4. ./mvnw
Related issues

I didn’t find any.

Suggest a Fix
JHipster Version(s)

6.5.1

JHipster configuration

INFO! Using JHipster version installed locally in current project’s node_modules INFO! Executing jhipster:info INFO! Options: from-cli: true

JHipster Version(s)
jhipster@0.0.1-SNAPSHOT /home/nikolay.seliverstov/Projects/jh-manytomany-bug
└── generator-jhipster@6.5.1 

JHipster configuration, a .yo-rc.json file generated in the root folder
.yo-rc.json file
{
  "generator-jhipster": {
    "promptValues": {
      "packageName": "com.janssen.fsra",
      "nativeLanguage": "en"
    },
    "jhipsterVersion": "6.5.1",
    "applicationType": "monolith",
    "baseName": "jhipster",
    "packageName": "com.janssen.fsra",
    "packageFolder": "com/janssen/fsra",
    "serverPort": "8080",
    "authenticationType": "jwt",
    "cacheProvider": "ehcache",
    "enableHibernateCache": true,
    "websocket": false,
    "databaseType": "sql",
    "devDatabaseType": "h2Disk",
    "prodDatabaseType": "mysql",
    "searchEngine": false,
    "messageBroker": false,
    "serviceDiscoveryType": false,
    "buildTool": "maven",
    "enableSwaggerCodegen": false,
    "jwtSecretKey": "bXktc2VjcmV0LXRva2VuLXRvLWNoYW5nZS1pbi1wcm9kdWN0aW9uLWFuZC10by1rZWVwLWluLWEtc2VjdXJlLXBsYWNl",
    "embeddableLaunchScript": false,
    "useSass": true,
    "clientPackageManager": "npm",
    "clientFramework": "angularX",
    "clientTheme": "none",
    "clientThemeVariant": "",
    "testFrameworks": [],
    "jhiPrefix": "jhi",
    "entitySuffix": "",
    "dtoSuffix": "DTO",
    "otherModules": [],
    "enableTranslation": true,
    "nativeLanguage": "en",
    "languages": ["en"],
    "blueprints": []
  }
}
JDL for the Entity configuration(s) entityName.json files generated in the .jhipster directory
JDL entity definitions
entity Material {
  name String
}
entity MaterialOrigin {
  name String
}
relationship ManyToMany {
  Material{origin} to MaterialOrigin{material}
}
Environment and Tools

openjdk version “11.0.5” 2019-10-15 OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.5+10) OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.5+10, mixed mode)

git version 2.17.1

node: v8.15.1

npm: 6.4.1

yeoman: 3.1.1

Docker version 19.03.5, build 633a0ea838

docker-compose version 1.23.2, build 1110ad01

Entity configuration(s) entityName.json files generated in the .jhipster directory
Material.json
{
    "name": "Material",
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String"
        }
    ],
    "relationships": [
        {
            "relationshipType": "many-to-many",
            "otherEntityName": "materialOrigin",
            "otherEntityRelationshipName": "material",
            "relationshipName": "origin",
            "otherEntityField": "id",
            "ownerSide": true
        }
    ],
    "changelogDate": "20191220115447",
    "entityTableName": "material",
    "dto": "no",
    "pagination": "no",
    "service": "no",
    "jpaMetamodelFiltering": false,
    "fluentMethods": true,
    "readOnly": false,
    "clientRootFolder": "",
    "applications": "*"
}
MaterialOrigin.json
{
    "name": "MaterialOrigin",
    "fields": [
        {
            "fieldName": "name",
            "fieldType": "String"
        }
    ],
    "relationships": [
        {
            "relationshipType": "many-to-many",
            "otherEntityName": "material",
            "otherEntityRelationshipName": "origin",
            "relationshipName": "material",
            "otherEntityField": "id",
            "ownerSide": false
        }
    ],
    "changelogDate": "20191220115448",
    "entityTableName": "material_origin",
    "dto": "no",
    "pagination": "no",
    "service": "no",
    "jpaMetamodelFiltering": false,
    "fluentMethods": true,
    "readOnly": false,
    "clientRootFolder": "",
    "applications": "*"
}
Browsers and Operating System

Ubuntu 18.04 Firefox 71

  • Checking this box is mandatory (this is just to show you read everything)

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 33 (22 by maintainers)

Commits related to this issue

Most upvoted comments

keeping entity name is important for db maintenance, you’re right, and 3 char hash suffix would be enough to avoid conflict

@mshima I’m sorry, but why do we need a separate generator in order to fix the bug? No, it’s just a workaround.

The bug is triaged. The fix is simple. The patch is ready. But it’s a breaking change when regenerating.

JHipster Developers don’t agree that we should create a config to workaround this. https://github.com/jhipster/generator-jhipster/pull/11030#discussion_r362032865

Is it possible to detect name clashing beforehand and generate an alternative relationship table name in case of clashing like <rel_name1>_<rel_name2> -> <rel_name1>_<rel_name2>_rel ?

Adding prefix of suffix, will not fix relations like relation_name -> other = relation_name_other rel -> name_other = relation_name_other

I think we should add prefix rel_ to tables. It will be easier to differentiate between other tables, but is not needed to fix this.

But I guess the collision finding is out of the scope that’s why the current solution is like that.

Yes, too hard.

Please see the list of the affected files in the issue description. If you are asking about list of jhipster source files then I cannot help here. I didn’t dive into the issue that deep.

Should we rename the issue to express that?