I have a project in Java and i am converting to Kotlin, but the entities with relations and mapping @ManytoOne they are with problems. The entities they are using mapping fecht type LAZY, aren't they respecting the LAZY and executing the query.
Exemple:
Entity father:
@Entity
@Table(name = "TIPOSITUACAO")
data class TipoSituacao (
@Id
@Column(name = "ID")
val id: Long? = null,
@Column(name = "DESCRICAO")
val descricao: String? = null
)
Entity son:
@Entity
@Table(name = "SITUACAO")
data class Situacao (
@Id
@Column(name = "ID")
val id: Long,
@Column(name = "DESCRICAO")
val descricao: String = "",
@Column(name = "TIPOSITUACAO_ID")
val tipoSituacaoId: Long? = null,
@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumn(name = "TIPOSITUACAO_ID", referencedColumnName = "ID", insertable = false, updatable = false)
val tipoSituacao: TipoSituacao? = null
)
My endpoint:
@RequestMapping("/api")
@RestController
class EndPoints {
@Autowired
private val situacaoRepository: SituacaoRepository? = null
val situacao: List<Situacao>
@GetMapping(value = ["/situacao"])
get() = situacaoRepository!!.findAll()
}
return:
[
{
"id": 1,
"descricao": "SITUACAO 1.1",
"tipoSituacaoId": 1,
"tipoSituacao": {
"id": 1,
"descricao": "TIPO SITUACAO 1"
}
},
{
"id": 2,
"descricao": "SITUACAO 1.2",
"tipoSituacaoId": 1,
"tipoSituacao": {
"id": 1,
"descricao": "TIPO SITUACAO 1"
}
},
{
"id": 3,
"descricao": "SITUACAO 2.1",
"tipoSituacaoId": 2,
"tipoSituacao": {
"id": 2,
"descricao": "TIPO SITUACAO 2"
}
}
]
Queries:
Hibernate: select situacao0_.id as id1_0_, situacao0_.descricao as descrica2_0_, situacao0_.tiposituacao_id as tipositu3_0_ from situacao situacao0_
Hibernate: select tiposituac0_.id as id1_1_0_, tiposituac0_.descricao as descrica2_1_0_ from tiposituacao tiposituac0_ where tiposituac0_.id=?
Hibernate: select tiposituac0_.id as id1_1_0_, tiposituac0_.descricao as descrica2_1_0_ from tiposituacao tiposituac0_ where tiposituac0_.id=?
If i use @JsonIgnore, @JsonManageReference or @JsonBackReference o son tipoSituacao until not return , but the query continue executing.
I tried using the DTO, but the same problem.
Every time i use situacaoRepository.find ou findAll the query is executed.
Project link with problem: https://github.com/maxbrizolla/spring-kotlin-jpa-problem
Can someone can help me?