I have a problem with a table that has 2 types of IDs (a primary key and a foreing key). The problem originates, when I only want to retrieve those that do matchmaking with the ID of the primary key, not counting those of the foreign key passed by http. Inside the class, I have an ID embeded as a serializable object.
When I do a search missing one of the two id's it gives the error java.util.NoSuchElementException: No value present
My classes:
I have an order class (table) that has 2 keys. (one is a foreigner)
..
@IdClass(PedidoId.class)
public class Pedido {
@Id
//@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long numero_pedido;
@Id
private String codigo_cliente;
..
Controller
@GetMapping("/pedidos/{id}")
public Pedido getPedidoById(@PathVariable Long id) {
//return eRepo.findById(new PedidoId(id,"CT25")).get(); // OK
//return eRepo.findById(new PedidoId(id,"")).get(); // ERROR
Pedido pedido = eRepo.findById(new PedidoId(id,null)).get(); // ERROR
return pedido;
}
It works correctly when I have the two Id's with data.
The problem arises when one arrives empty. (Because we are only consulting the table itself, regardless of the foreign key)
When I use the pathUrl/pedidos/5 I get the following error
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Wed Sep 15 14:28:15 CEST 2021
There was an unexpected error (type=Internal Server Error, status=500).
No value present
java.util.NoSuchElementException: No value present
This is due to the parameter "customer_code" (FK another table) of the OrderId object reaches null.
I think it should search the same way even if it is only for one of the 2 IDs or with all 2 at the same time. Is there any way to fix this? Or is it simply necessary to create the service -> DAO and do it manually? I think I remember that it is possible without doing it manually.