Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
LazyInitializationException using patch add with Spring HATEOAS

I have an entity ElementType that has a set of EquipmentCodes:

@Entity 
@Table(name = "ELEMENT_TYPES")
public class ElementType extends AbstractEntity<Long> {

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name= "ELEM_TYPE_ID")
private Set<EquipmentCode> equipmentCodes;

}

I have both classes exposed as Rest Resources with repositories:

@RepositoryRestResource(path = "elementTypes", collectionResourceRel = "elementTypes")
@Transactional
public interface ElementTypeRepository extends CrudRepository<ElementType, Long> {}

Now if I try some operations, like

GET https://localhost:8080/api/elementTypes/40529090

or PATCH sending

{
   "equipmentCodes" : [
   "https://localhost:8080/api/equipmentCodes/40529100"
    ]
}

It seems to work fine, however, if I try to add a new equipmentCode to existing ones:

 [
    {
     "op": "add",
     "path": "/equipmentCodes",
     "value": [
       "https://localhost:8080/api/equipmentCodes/40529099"
    ]
    }
]

I get

org.hibernate.LazyInitializationException: failed to lazily initialize a collection, could not initialize proxy - no Session, at at org.springframework.data.rest.webmvc.json.patch.JsonLateObjectEvaluator.evaluate(JsonLateObjectEvaluator.java:45)

Any hint about what the problem is? Is there something wrong with the way I use "add" or is it something about the Spring configuration?

Edit: it may be a sintactic mistake, like this I don't get the exception:

 [
    {
     "op": "add",
     "path": "/equipmentCodes",
     "value": "https://localhost:8080/api/equipmentCodes/40529099"
    }
]

However what is strange is that instead of adding the existing EquipmentCode with id 40529099 to the ElementType, it is just inserting a new empty EquipmentCode (Of course I'm sending the patch to https://localhost:8080/api/elementTypes/40529090), removing the other ones in the set, and then asigning the new one the the ElementType

To clarify, what I want is to add one EquipmentCode that is already in the DB to the set in ElementType, without removing the current EquipmentCodes in that set.

about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda