I have an entity A linked to B with a ManyToOne relation, of type Lazy
public class A extends BaseEntity {
@Enumerated(EnumType.STRING)
private TipoTurnoEnum tipo;
private String number;
private String state;
private Date date;
@JoinColumn(name = "id_b", referencedColumnName = "ID")
@ManyToOne(fetch = FetchType.LAZY)
private B b;
/* Se excluye el resto de la entidad */
When I make a query about entity A, and in the result attribute B is not null, Hibernate makes a second query, this time on B.
i.e.:
2018-10-09 14:51:32.176 DEBUG 15484 --- [nio-8080-exec-2] org.hibernate.SQL :
select
A.numero as numero1_1_,
A.estado as estado2_1_,
A.fecha as fecha3_1_,
A.b as b7_1_
from
A
where
A.numero=?
If A.b is not null, then Hibernate executes:
select
B.id as id1_15_0_,
B.descripcion as descripcion_15_0_
from
B
where
B.id=?
Being? in B.id, replaced by the value of A.b obtained in the first query. The request object that passed as a filter of the frontend (typescript) was defined as:
let request = {
A: {number: 1}
};
How do I prevent Hibernate from doing this? Why does this happen even when the relationship is explicitly defined as Lazy? I'm only interested in the values of A, except when I send an attribute to filter B, that is:
A: {
number: 1,
b: {
id: 2
}
}
I am using the findOne function of crudRepository (invoking from the genericRepository interface)
@PostMapping(value = "/findOne", produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public BaseEntity findone(@RequestBody FiltroRequest request) throws GenericException {
return genericService.findOne(request);
}
Thank you very much, in advance.