jhipster / generator-jhipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.
https://www.jhipster.tech
Apache License 2.0
21.55k stars 4.02k forks source link

[FEATURE] Possibility of add "required" in a one to many relationship #3417

Closed marcosmoreno closed 8 years ago

marcosmoreno commented 8 years ago

Currently if I create a one to many relationship (Owner-Car), I can create records in the many part with the relationship field empty (i.e. Car without an owner).

I was told in the jhipster domain language issue tracker that if Jhipster implements this, JHipster UML and the JDL would incorporate that. Any chance it is considered?

JHipster Version(s) 3.0

deepu105 commented 8 years ago

This is a bit trickier to do so unless someone is willing to volunteer I don't think any of @jhipster/developers will have time to do this. Any way ill leave it open if any body wants to do this

adornala commented 8 years ago

@jhipster/developers : I can volunteer to work on this. Shall I go ahead?

deepu105 commented 8 years ago

Yes please On 20 Apr 2016 00:12, "Amarnath Reddy Dornala" notifications@github.com wrote:

@jhipster/developers : I can volunteer to work on this. Shall I go ahead?

— You are receiving this because you are on a team that was mentioned. Reply to this email directly or view it on GitHub https://github.com/jhipster/generator-jhipster/issues/3417#issuecomment-211997121

adornala commented 8 years ago

Front end validation solves this right or want to edit Entity as well? Just making sure. If no owner is selected we can't create a Car!

marcosmoreno commented 8 years ago

@dornala actually if I edit the entity manually (adding the notnull directive in the ManyToOne field) the Front end does the validation already: If no owner is selected we can't create a car. Please see the my last comment in the linked issue in the description. I guess it would need adjustments in the entity generator and in the JDL parser, as not all OneToMany might need this validation.

adornala commented 8 years ago

Ya @marcosmoreno by adding notnull to one to many field in entity, we can make sure JPA transaction fails. If there is no Owner selected error(Internal Server Error) will be thrown , So, we add front end validation to make sure - save button shows up only when we have a owner for car(Avoiding internal server error).

Now, Requirement: If we are using JDL or Entity Generator to create entity. we want entity to be created with not null @ entity level and with front end validation. Is it Right?

marcosmoreno commented 8 years ago

I did not add the NotNull to the OneToMany, as I want to be able of saving owners without a car. What I observed is that in the entity Car, there was a ManyToOne relationship (note I did not have a ManyToOne in my JDL, it was added by JDL), and there I put the @NotNull. I wanted to be able of having Owners without Car, but no Cars without Owner. After building, Jhipster already tells me "The field is required" when I try to save the Car Entity (I also would have expected an "internal server error", but this is not what seems to happen - I get a 404 bad request in the console, so probably it is taking the message from that error).

In JDL:

relationship OneToMany {
  Owner{car} to Car{owner(ownerName)}
}

My Owner Entity has the following: (I did not touch this Entity)

@OneToMany(mappedBy = "owner")
    @JsonIgnore
    private Set<Car> cars = new HashSet<>();

My entity in the .domain package has this in the Car Entity:

    @ManyToOne
    @NotNull  //This is the one that I added
    private Owner owner;

So the requirement would be: being able of writing "required" in the JDL, in order that the NotNull is added to the @ManyToOne relationship.

I would expect something like:

relationship OneToMany {
  Owner{car} to Car{owner(ownerName),required} //just an idea on where to put the required
}

Jhipster already tells me "Field owner cannot be empty" when I try to save the Car Entity after my change, so (I do not see the need of adjusting the front-end (but maybe I am missing something). in the front-end, we could add the "required" to the field, to avoid the call to the server that is happening now.

I could help gladly to code if I would now where to start (I did not have a look at the sources yet).

adornala commented 8 years ago

Got it. Typo I meant ManytoOne.

adornala commented 8 years ago

Hi @deepu105 I need small help testing. I made all necessary changes. It working fine when I change name to something other than generator-jhipster(In package.json and linking it to yeoman as npm link) Any idea why it's happening deepu? Sorry if it's an easy fix, please guide me. This is my first contribution.

deepu105 commented 8 years ago

I dont understand your issue.

This is the way to run the generator from a local instance

  1. Install npm i -g generator-jhipster
  2. Fork the generator to a local folder named generator-jhipster
  3. Go inside the folder and run npm link
  4. Make a small change to any log statement and run yo jhipster in someother new folder to check if its running your change
  5. If not try running npm link generator-jhipster from the folder where you are trying to run yo jhipster
deepu105 commented 8 years ago

closed via https://github.com/jhipster/generator-jhipster/pull/3503