I'm using to Spring Data with Hibernate and have run into an issue. .
There are 2 entities : Activity, Category
Activity ---- One to Many --- Category
Problem / Need:
Assuming that a Category record already exists in the database, I want to save a given activity pertaining to a pre-existing category.
Using the method provided in the ActivityRunner class below doesn't work. Please note that in this class, an attempt has been made to save off Activity alone, but not save Category, because I want to use the existing category through a foreign key reference. However the code doesn't allow me to use a "transient" instance of Category. I hope I've made my problem clear. Can anyone help ?
An attempt to save Activity using crud repository.
The Category you use is a fresh new object. It has no id set, so Spring Data / JPA treats it as a new entity. It will therefore try to insert it. Whether or not that succeeds depends on your unique constraints.
What you need to do is use a CategoryRepository to find the Category by its name, then use the result in the Activity.