Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda