I have a very simple association class @ManyToOne.
@Entity
public class Place {
...
@ManyToOne
@RestResource(exported = false)
private Category category;
...
}
Both places and categories have their own repositories that are exported. But for our case, we need the field Categorynot to be exported here. Identifiers are displayed for Category.
However, when I try to update an existing place with an existing category, Hibernate does not perform the update.
Example PUT to /places/foo
{
...
"category": {
"name": "Farm",
"id": 4
},
...
}
Caused by: org.hibernate.HibernateException: identifier of an instance of com.phrankly.places.model.Category was altered from 2 to 4
I am not sure how this happens, given that I am not setting any parameters Cascade- Categorieswhich are located elsewhere.
I do not want to write custom Controllerfor Place. I also tried using EventHandlerto manually set the field to see if this helped.
@Component
@RepositoryEventHandler(Place.class)
public class PlaceEventHandler {
@Autowired
private CategoryRepository categoryRepository;
...
@HandleBeforeSave
public void onUpdateExisting(Place place) {
if(place.getCategory() != null){
place.setCategory(categoryRepository.findOne(place.getCategory().getId()));
}
}
...
}
. , Spring Data REST, ? SDR, ?