Error com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError)
See original GitHub issueIn my project, when I use getById, I have an “Infinite recursion” error. My application is structured as follows:
TABLES:
object AddressTable : UUIDTable("main.address") {
val internalId = long("internal_id").autoIncrement().uniqueIndex("address_internal_id_index")
val active = bool("active").default(true)
val zipCode = varchar("zip_code", 10)
val country = varchar("country", 120)
val state = varchar("state", 120)
val city = varchar("city", 120)
val district = varchar("district", 120)
val publicPlace = varchar("public_place", 120)
val number = varchar("number", 10)
val complement = varchar("complement", 50)
val idPatientFk = reference(
"id_patient_fk", PatientTable, ReferenceOption.CASCADE, ReferenceOption.NO_ACTION
)
val idProfessionalFk = reference(
"id_professional_fk", ProfessionalTable, ReferenceOption.CASCADE, ReferenceOption.NO_ACTION
)
}
class Address(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<Address>(AddressTable)
var internalId by AddressTable.internalId
var active by AddressTable.active
var zipCode by AddressTable.zipCode
var country by AddressTable.country
var state by AddressTable.state
var city by AddressTable.city
var district by AddressTable.district
var publicPlace by AddressTable.publicPlace
var number by AddressTable.number
var complement by AddressTable.complement
val idPatientFk by Patient optionalBackReferencedOn AddressTable.idPatientFk
val idProfessionalFk by Professional optionalBackReferencedOn AddressTable.idProfessionalFk
}
object PatientTable : UUIDTable("main.patient") {
val internalId = long("internal_id").uniqueIndex("patient_internal_id_index").autoIncrement()
val active = bool("active").default(true)
val name = varchar("name", 120)
val cpf = varchar("cpf", 14).uniqueIndex("patient_cpf_index")
val birthDate = integer("birth_date")
}
class Patient(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<Patient>(PatientTable)
var internalId by PatientTable.internalId
var active by PatientTable.active
var name by PatientTable.name
var cpf by PatientTable.cpf
var birthDate by PatientTable.birthDate
}
object ResponsibleTable : UUIDTable("main.responsible", "id") {
val internalId = long("internal_id").uniqueIndex("responsible_internal_id_index")
val active = bool("active").default(true)
val name = varchar("name", 120)
val cpf = varchar("cpf", 15).uniqueIndex("responsible_cpf_index")
val idPatientFk = reference(
"id_patient_fk", PatientTable, ReferenceOption.CASCADE, ReferenceOption.NO_ACTION
)
}
class Responsible(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<Responsible>(ResponsibleTable)
var internalId by ResponsibleTable.internalId
var active by ResponsibleTable.active
var name by ResponsibleTable.name
var cpf by ResponsibleTable.cpf
val idPatientFK by Patient optionalBackReferencedOn ResponsibleTable.idPatientFk
}
object PhoneTable : UUIDTable("main.phone", "id") {
val internalId = long("internal_id").uniqueIndex("phone_internal_id_index")
val active = bool("active")
val number = varchar("number", 24).index("phone_number_index")
val threadType = customEnumeration("thread_type", "EThreadType", { value ->
EThreadType.valueOf(value as String)
}, { EnumSetting("EThreadType", it) })
val idPatientFk = reference(
"id_patient_fk", PatientTable.id, ReferenceOption.CASCADE, ReferenceOption.NO_ACTION
)
val idResponsibleFk = reference(
"id_responsible_fk", ResponsibleTable.id, ReferenceOption.CASCADE, ReferenceOption.NO_ACTION
)
val idProfessionalFk = reference(
"id_professional_fk", ProfessionalTable.id, ReferenceOption.CASCADE, ReferenceOption.NO_ACTION
)
}
class Phone(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<Phone>(PhoneTable)
var internalId by PhoneTable.internalId
var active by PhoneTable.active
var number by PhoneTable.number
var threadType by PhoneTable.threadType
val idPatientFk by Patient optionalBackReferencedOn PhoneTable.idPatientFk
val idResponsibleFk by Responsible optionalBackReferencedOn PhoneTable.idResponsibleFk
val idProfessionalFk by Professional optionalBackReferencedOn PhoneTable.idProfessionalFk
}
object ProfessionalTable : UUIDTable("main.professional", "id") {
val internalId = long("internal_id").autoIncrement().index("professional_internal_id_index")
val active = bool("active")
val name = varchar("name", 120)
val cpf = varchar("cpf", 14).uniqueIndex("professional_cpf_index")
val birthDate = integer("birth_date")
val profession = customEnumeration("profession", "EProfessional", { value ->
EProfession.valueOf(value as String)
}, { EnumSetting("EProfessional", it) })
val council = customEnumeration("council", "EProfessionalDocument", { value ->
EProfessionalDocument.valueOf(value as String)
}, { EnumSetting("EProfessionalDocument", it) })
val documentNumber = varchar("document_number", 15).uniqueIndex("professional_document_number_index")
}
class Professional(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<Professional>(ProfessionalTable)
var internalId by ProfessionalTable.internalId
var active by ProfessionalTable.active
var name by ProfessionalTable.name
var cpf by ProfessionalTable.cpf
var birthDate by ProfessionalTable.birthDate
var profession by ProfessionalTable.profession
var council by ProfessionalTable.council
var documentNumber by ProfessionalTable.documentNumber
}
DATABASE FACTORY
suspend fun <T> dbQuery(block: () -> T): T =
withContext(Dispatchers.IO) {
transaction { block() }
SERVICE
override suspend fun getById(id: UUID): Patient? = dbQuery {
Patient.findById(id)
}
CONTROLLER
fun Route.patient(service: PatientService) =
route("/api/patient") {
get("/{id}") {
call.respond(service.getById(UUID.fromString(call.parameters["id"])) ?: HttpStatusCode.NotFound)
}
My database: https://imgur.com/a/1Xn2qlN
But when I search for a patient, it always returns the error:
com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: org.jetbrains.exposed.sql.Column["table"]->com.rjdesenvolvimento.module.patients.PatientTable["columns"]->java.util.ArrayList[0]->org.jetbrains.exposed.sql.Column["table"]->com.rjdesenvolvimento.module.patients.PatientTable["columns"]->java.util.ArrayList[0]...
What am I doing wrong?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5
Top Results From Across the Web
Infinite Recursion with Jackson JSON and Hibernate JPA issue
Keenformatics - How To Solve JSON infinite recursion Stackoverflow (my blog); Jackson References ... ObjectMapper; import com.fasterxml.jackson.databind.
Read more >Jackson - Bidirectional Relationships - Baeldung
First, we'll discuss the Jackson JSON infinite recursion problem. Then we'll see how to serialize entities with bidirectional relationships.
Read more >com.fasterxml.jackson.databind.JsonMappingException
on executing my api with postman /mymodule/patient?mobile=0708443737 , i get an Infinite recursion (StackOverflowError) but if i add this ...
Read more >Jackson JSON infinite recursion problem - My Two Cents
As UserDetail contains a reference to User, it will try to get User and this will go on in loop causing StackOverflowError exception....
Read more >Infinite recursion (StackOverflowError) - Google Groups
Infinite recursion (StackOverflowError) (through reference chain: org.springframework.data.rest.webmvc.EntityResource["[anySetter]"]->java.util.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Instead jsonproperties you need use jsonignoreproperties, the former doesnt exist
El lun., 25 nov. 2019 10:18, Joaquín Sánchez userquin@gmail.com escribió:
One tecnique is to create a pojo instead returning the entity.
Another using Jackson annotations to only include your fields. See JsonPropeties annotation for type.
To avoid circular dependencies is hard to solve, in this case I suggest you use pojos.
El lun., 25 nov. 2019 2:23, Rodrigo Batista notifications@github.com escribió: