Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

How to save Entity without affecting a related Entity

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?


Activity




Category:



An attempt to save Activity using crud repository.





 
Marshal
Posts: 22386
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


Note that we've had something similar recently in https://coderanch.com/t/745425/databases/ERROR-OneTOMany-ManyToOne-mapping-FK. There's one big difference though - there the linked record (in this case Category) was identified by its id, not a different field. I also don't see any methods on CrudRepository that lets you get a reference based on the id, only a full entity.
 
Izanami Caster
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Rob!!

Your suggestion has worked. I'll put this down to my lack of knowledge of what the API provides.

To arrive at my solution I firstly removed the wrongly placed CascadeType.ALL on the ManyToOne relationship. Later I used the categoryRepository.findById to reference the "detached" entity.



Thanks again for the guidance!!!
-Izanami Caster
 
Rob Spoor
Marshal
Posts: 22386
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome.
 
When it is used for evil, then watch out! When it is used for good, then things are much nicer. Like this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic