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
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.