Skip to content
Advertisement

How can I show Multiple Rows in JSON

I need to show sql result

in a JSON like this

{
slpcode: 700,
businesspartnerparamid:{181,
                        195,
                        197,
                        362,
                        376
                        }
}

in a spring boot app.

Class

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import javax.persistence.*;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="businesspartnerparamsalesperson")
public class Vendedor {

    @Id
    @Column(name="businesspartnerparamid", nullable = false)
    private Long businessPartnerParamID;

    @Column(name="Slpcode",nullable = false)
    private Long slpCode;

    public Long getBusinessPartnerParamID() {
        return businessPartnerParamID;
    }

    public void setBusinessPartnerParamID(Long businessPartnerParamID) {
        this.businessPartnerParamID = businessPartnerParamID;
    }

    public Long getSlpCode() {
        return slpCode;
    }

    public void setSlpCode(Long slpCode) {
        slpCode = slpCode;
    }
}

Repo

Repository
public interface VendedorRepository extends JpaRepository<Vendedor, Long> {



    Optional<Vendedor> findVendedorBySlpCode (Long slpCode);



}

Service

@Service
@AllArgsConstructor
public class ApiService {

    @Autowired
    private final VendedorRepository vendedorRepository;

    @Autowired
    private final ClienteRepository clienteRepository;

    public Optional<Vendedor> findVendedorByCode (Long slpCode) {
        Optional<Vendedor> vendedor = vendedorRepository.findVendedorBySlpCode(slpCode);

        return vendedor;
    }

    public Optional<Cliente> findClienteByCode (String code){
        Optional<Cliente> cliente = clienteRepository.findClienteByCardCode(code);

        return cliente;
    }


}

Controller

@CrossOrigin
@RestController
@EnableAutoConfiguration
@RequestMapping("/vendedor")
@AllArgsConstructor
public class ApiController {

    @Autowired
    private ApiService apiService;


    @GetMapping(value="/{slpCode}", produces = MediaType.APPLICATION_JSON_VALUE)
    public Optional<Vendedor> findVendedorBySlpCode (@PathVariable Long slpCode){
        return apiService.findVendedorByCode(slpCode);
    }
}

I’m getting this message on log:

query did not return a unique result: 94; nested exception is >javax.persistence.NonUniqueResultException: query did not return a unique result: 94

After that, I wanna delete/update all of salesperson associated id’s (transfer clients (id’s) from one to another .

Advertisement

Answer

The javax.persistence.NonUniqueResultException indicates that there is more than one result from the query. Looking at your data, indeed, you have more than one row where the slpCode is 700.

You have to change the return type to List of Vendedor:

List<Vendedor> findVendedorBySlpCode (Long slpCode);

If no vendors are found an empty list will be returned.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement