I have a very simple class, with a @ManyToOne association.
@Entity
public class Place {
...
@ManyToOne
@RestResource(exported = false)
private Category category;
...
}
Both Place and Category have their own respective Repositories that are exported. But for our case, we need to have the Category field not exported here. IDs are exposed for Category.
However, when I try to update an existing Place with an existing Category, Hibernate fails 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'm not sure how that's even happening, given I don't set any Cascade options - Categories are managed elsewhere.
I don't want to have to write a custom Controller for Place. I also tried using an EventHandler to manually set the field to see if that helped.
@Component
@RepositoryEventHandler(Place.class)
public class PlaceEventHandler {
@Autowired
private CategoryRepository categoryRepository;
...
@HandleBeforeSave
public void onUpdateExisting(Place place) {
if(place.getCategory() != null){
// or find by another field if you're not exposing IDs
place.setCategory(categoryRepository.findOne(place.getCategory().getId()));
}
}
...
}
But, no change in functionality. I know I'm going outside of what Spring Data REST suggests, but what can I do here? Is this even a SDR issue or am I mapping this incorrectly?