My approach was to correct a mistake in the JHipster generate process where a Long usuarioId was generated in the DTO, but on the Angular side all Users were retrieved and their ids compared when saving the entity. Instead, I retrieved the User object using the UserService find by login method.
This is my solution, which I believe may be helpful:
Firstly, in the ManyToMany relationship:
@ManyToOne
@JsonIgnoreProperties("")
private User coordinador;
public User getCoordinador() {
return coordinador;
}
public ExtendedUser coordinador(User user) {
this.coordinador = user;
return this;
}
public void setCoordinador(User user) {
this.coordinador = user;
}
And in the DTO:
private Long coordinadorId;
private String coordinadorLogin;
public Long getCoordinadorId() {
return coordinadorId;
}
public void setCoordinadorId(Long userId) {
this.coordinadorId = userId;
}
public String getCoordinadorLogin() {
return coordinadorLogin;
}
public void setCoordinadorLogin(String userLogin) {
this.coordinadorLogin = userLogin;
}
In the Angular component's HTML file:
<div class="form-group">
<label class="form-control-label" jhiTranslate="sigemApp.extendedUser.coordinador" for="field_coordinador">Coordinador</label>
<select class="form-control" id="field_coordinador" name="coordinador" [(ngModel)]="extendedUser.coordinadorLogin" >
<option [ngValue]="null"></option>
<option [ngValue]="userOption.login" *ngFor="let userOption of users; trackBy: trackUserById">{{userOption.login}}</option>
</select></div>
Lastly, in the component.ts file:
users: IUser[];
this.userService.query().subscribe(
(res: HttpResponse<IUser[]>) => {
this.users = res.body;
},
(res: HttpErrorResponse) => this.onError(res.message)
);
trackUserById(index: number, item: IUser) {
return item.login;
}
getSelected(selectedVals: Array<any>, option: any) {
if (selectedVals) {
for (let i = 0; i < selectedVals.length; i++) {
if (option.id === selectedVals[i].id) {
return selectedVals[i];
}
}
}
return option;
}
To save it, I use the userService to retrieve the User object, as shown in the UserService:
Optional<User> coordinador = userRepository.findOneByLogin(extendedUserDTO.getCoordinadorLogin());
updateExtendedUser.setCoordinador(coordinador.get());
extendedUserService.save(updateExtendedUser);