jhipster / jhipster-core

JHipster Domain Language, used by JHipster UML and JDL-Studio to generate entities
Apache License 2.0
346 stars 116 forks source link

ManyToMany not always in json with ownerSide=false #352

Closed yelhouti closed 4 years ago

yelhouti commented 5 years ago
Overview of the issue

The "non Owner" side of ManyToMany relationship is not always present in JDL, (making fluent methods in the generator break)

entity A
entity B

//works:
relationship ManyToMany {
    A{b) to B{a}
}

//works:
relationship ManyToMany {
    A to B
}

//DOES NOT work:
relationship ManyToMany {
    A{b) to B
}

If this is a "feature" not a bug, how can the generator know that Set<A> will not be present in B.java to avoid calling b.getAS().add(this); in A.java

If we want to keep this behavior (not having Set<A> in B) the logique in my opinion should be in the generator not in the .json.

Or maybe this case will juste disappear when the generators code starts using the .jdl as it's input, and if we want it back we will need to code the right way in the generator.

Motivation for or Use Case

This clearly can be avoided by changing the JDL, but it should be a known issue to avoid wasting other peoples time.

This is a bug because, the generated code does not compile

JHipster Version(s)

6.1.2 (don't think it's a regression) it's a side effect of adding fluent methods for add...

MathieuAA commented 5 years ago

I'll test this out and fix it. Thanks for reporting this issue!

MathieuAA commented 4 years ago

I wonder if it's not an issue with the generator, that should allow unidirectional many to many...

MathieuAA commented 4 years ago

The fix is quite easy:


the code to add in the entity parser, after l.350

if (!relatedRelationship.injectedFieldInTo) {
        relationship.otherEntityRelationshipName = lowerFirst(relatedRelationship.from);
        const otherSideRelationship = {
          relationshipName: camelCase(relatedRelationship.from),
          otherEntityName: camelCase(relatedRelationship.from),
          relationshipType: _.kebabCase(MANY_TO_MANY),
          otherEntityField: lowerFirst(otherSplitField.otherEntityField),
          otherEntityRelationshipName: lowerFirst(relatedRelationship.to),
          ownerSide: false
        };
        relatedRelationship.type = MANY_TO_MANY;
        entities[relatedRelationship.to].addRelationship(otherSideRelationship);
      }
yelhouti commented 4 years ago

@MathieuAA This is what I tried to explain in the issue, sadly didn't have time to work on it, I'm in a bit of pickle these days with some projects so sorry, I can't help any further for now.

MathieuAA commented 4 years ago

Don't worry! I'll try to get to the bottom of this :) again, thanks for reporting this one