0

I have a Angular App with a formgroup for something like a signup screen. There, I have all my inputs working like this:

<div id="form-group" class="left">
      <input
      [ngClass]="{ 'erros': groupForm.get('name').errors?.required && form.submitted }"   
      id="fullname"
      formControlName="name" 
      class="form-control" 
      placeholder="Name">
  </div>

It works flawlessly, but there is a specific field that never sends any data (the field is just like that one, but it is a select:

 <div id="form-group" class="left">
      <select formControlName="entity">
          <option id = "zero" value="" disabled selected>Entity</option>
          <option id = "_1" value=0 >CT</option>
          <option id = "_2" value=1 >HP</option>
          <option id = "_3" value=2 >CM</option>
      </select>
  </div>

). The funny thing is, when I console.log() this fromGroup, all my info is there (Even entity!). But is does not reach the Database (postgres btw). The API is Spring + Hibernate, and it has a model, resource and repository for every table in the db, their structure is like this:

Model:

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
private Integer id;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "entity")
private Entity entity;

public GerProspect() {
    super();
}

Repository:

public interface ProspectRepository extends JpaRepository<Prospect, Integer>{}

Resource:

@CrossOrigin("${origem-permitida}")
@RestController
public class ProspectResource {

    @Autowired
    private ProspectRepository ProspectRepository;

    @GetMapping("/Prospect")
    public List<Prospect> ListProspect(){   
    return ProspectRepository.findAll();
    }

    @GetMapping("/Prospect/{ProspectId}")
    public Prospect editProspect(@PathVariable Integer ProspectId) {
    return ProspectRepository.findById(ProspectId).get();
    }   

    @PostMapping("/Prospect")
    public Prospect addProspect(@RequestBody @Valid Prospect T) {
    return ProspectRepository.save(T);
    }

    @DeleteMapping("/Prospect")
    public Prospect remProspect(@RequestBody @Valid Prospect T) {

        ProspectRepository.delete(T);
        return ProspectRepository.findAll().stream().findAny().get();
    }

}

I'm not the author of this API, but I need to use it anyway. All my info. in the formGroup is correct and not-null, but only the field Entity is never filled. I should also say that this specific field is a foreign Key with the same structure and this list<>:

@OneToMany(fetch = FetchType.LAZY, mappedBy = "ConfigEntity", targetEntity= Prospect.class)
List<Prospect> Prospects = new ArrayList<Prospect>();

I really dont know why, but this field is always null in the db. What am I doing wrong here?

CH4B
  • 734
  • 1
  • 9
  • 27

1 Answers1

2

First of all, you should have unique CSS inside a file (Does ID have to be unique in the whole page?). Secondly, even if in the DB table corresponding to the Prospect table, the "entity" field is just an Integer, inside your Java code, "entity" is a full instance of Entity class, not just an Integer. So you either send through the request a full Entity object, or you send just the id, like you tried to, but you manually fetch the corresponding Entity inside the controller and attach it to the Prospect instance.

Ieremias Viorel
  • 418
  • 5
  • 9
  • 2
    Send a DTO (https://martinfowler.com/eaaCatalog/dataTransferObject.html) object which you use to build your model object. One of the fields of the DTO will be the id of the Entity. – Ieremias Viorel Dec 06 '18 at 19:07
  • I don't really know how to do that, but thanks for the light! – CH4B Dec 06 '18 at 19:14