Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!