Relationship mapping in Hibernate Java - save owning side entity
You configure the relation between User
and Url
correctly. It mainly complains when it saves an Url
, its User
is a new record.
The problem can be fixed if you save the User
first :
User user = getCurrentUserDetails();
user.addRssUrl(rssUrl);
userRepository.save(user);
rssUrlRepository.save(rssUrl);
But since you already configure cascade
is ALL for User
's Url
, all JPA operations applied on User
will automatically applied to its Url
too , which means you can simply do :
User user = getCurrentUserDetails();
user.addRssUrl(rssUrl);
userRepository.save(user);