Relationship JPA, Spring, Angular,

Asked

Viewed 254 times

0

I have a client record and I want to associate with this client a relationship of equipment but is not saving the relationship.

on the model layer of the customer class I did so

 @OneToMany(cascade = {CascadeType.ALL } )
    @JoinColumn(name = "CLIENTE_ID")
    private Collection<Equipamento> equipamentos; 

In my front end JS I have so

app.controller("clienteController", function($scope, $http, $filter){

    $scope.clientes = [];
    $scope.cliente={}; 
    $scope.equipamento={};



    $scope.salvarClientes=function(){
        if($scope.frmCliente.$valid){
        $http({
            method : 'POST',
            url : 'http://localhost:8080/user/clientes', data:$scope.cliente
        }).then(function(response) {

            Lobibox.notify('success',  
                    {
                        title: false,
                        msg: 'Registro salvo com sucesso!', 
                        delay: 4000,
                        position: "top right"
                    }
                );    
            carregarClientes();         
            $scope.cliente={};
            $scope.equipamento={};


        }, function(response) {
            console.log(response.data);
            console.log(response.status);
        });


        }else{
            Lobibox.alert("error",
                    {
                        title: "Atenção",
                        msg: "Campos com * são de preenchimento obrigatório!"
                    });

        }

    }; 
       ......

In my HTML

<div ng-controller="clienteController">

    <div class="container">
        <div class="row">
            <div class="col-md-6">
                <div class="panel panel-primary">
                    <div class="panel-heading">Cadastro de Cliente</div>

                    <div class="panel-body">
                        <form novalidate name="frmCliente" id="formCadastro" role="form">

                            <div class="row">


                                <div class="form-group col-md-12">
                                    <label>* Empresa:</label> <input name="inpEmpresa"
                                        maxlength="60" required="required" type="text"
                                        class="form-control" ng-model="cliente.empresa" />
                                </div>
                                <div class="form-group col-md-12">
                                    <label>* CNPJ:</label> <input name="inpCnpj" maxlength="16"
                                        required="required" type="text" class="form-control"
                                        ng-model="cliente.cnpj" />
                                </div>
                                <div class="form-group col-md-6">
                                    <label>* Cidade:</label> <input name="inpCidade" maxlength="60"
                                        required="required" type="text" class="form-control"
                                        ng-model="cliente.cidade" />
                                </div>


                                <div class="form-group col-md-12">
                                    <label>* Endereço:</label> <input name="inpEndereco"
                                        maxlength="60" required="required" type="text"
                                        class="form-control" ng-model="cliente.endereco" />
                                </div>

                                <div class="form-group col-md-12">
                                    <label>* E-mail:</label> <input name="inpEmail" maxlength="50"
                                        required="required" type="email" class="form-control"
                                        ng-model="cliente.email" />
                                </div>

                                <div class="form-group col-md-6">
                                    <label>* Telefone:</label> <input name="inpTelefone"
                                        maxlength="15" required="required" type="text"
                                        class="form-control" ng-model="cliente.telefone" />
                                </div>


                                <div class="form-group col-md-6">
                                    <label>* Contato:</label> <input name="inpContato"
                                        maxlength="30" required="required" type="text"
                                        class="form-control" ng-model="cliente.contato" />
                                </div>

                                <div class="form-group col-md-12">
                                    <label>Observação:</label>
                                    <textarea name="inpObservacao" class="form-control" rows="3"
                                        ng-model="cliente.observacao"></textarea>
                                </div>

                                <div></div>
                            </div>
                        </form>
                        <div class="pull-right">
                            <button type="button" id="btnSalvar" class="btn btn-default"
                                ng-click="salvarClientes()">Salvar</button>
                            <!-- ou ng-disabled="frmCliente.inpNome.$invalid"  -->

                            <button type="button" class="btn btn-default"
                                ng-click="cancelarAlteracaoClientes(cli)">Cancelar
                                Edição</button>
                                    <button type="button" id="btnAdCadEquipamento"
                                class="btn btn-default" ng-click="addEquipamento()">
                                <span class="glyphicon glyphicon-plus"></span>
                            </button>
                        </div>

                    </div>
                </div>
            </div>

             <div ng-controller="equipamentoController">
                <div  id="cadEquipInsp" class="col-md-6 ">
                     <div class="panel panel-primary">
                        <div class="panel-heading">Cadastro de
                            Equipamentos/Instrumentos</div>

                        <div class="panel-body">
                            <form novalidate name="frmEquipamento" id="formCadastro" role="form">
                                <div class="row">
                                    <div class="form-group col-md-12">
                                        <label>Descrição:</label> <input name="inpDescricao"
                                            maxlength="60" required="required" type="text"
                                            class="form-control" ng-model="equipamento.descricao" />
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label>Data da Inspeção:</label> <input
                                            name="inpDataEquipamento" maxlength="16" required="required"
                                            type="date" class="form-control" ng-model="equipamento.dataEquipamento" />
                                    </div>
                                    <div class="form-group col-md-6">
                                        <label>Próxima Inspeção:</label> <input
                                            name="inpProximaInspecao" maxlength="60" required="required"
                                            type="date" class="form-control" ng-model="equipamento.proximaInspecao" />
                                    </div>
                                <!--    <div class="pull-right">
                                    <button type="button" id="btnSalvar" class="btn btn-default"
                                ng-click="salvarEquipamentos()">Salvar</button>
                                 </div> -->
                                </div>
                            </form>                         
                        </div>
                    </div> 
                </div>
            </div>
  • Inside your $Scope.customer has a list of equipment?

  • @Yes that’s the idea.

1 answer

0


I found the following solution: In the Equipment class of the model folder I changed the relation I did so:

    @ManyToOne
    @JoinColumn(name = "CLIENTE_ID")
    private Cliente cliente; 

In the Equipment registration screen I made a component select that brings all registered customers.

                               <div class="form-group col-md-12">
                                        <label>* Empresa:</label> <select 
                                      ng-model="equipamento.cliente.id"
                                            class="form-control">
                                            <option value="{{cli.id}}" 
                                          ng-repeat="cli in clientes">{{cli.empresa}}</option>

                                        </select>
                                    </div>

After that, I put a method saving equipment and saved the interface in the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.