This week's book giveaways are in the Cloud and AI/ML forums.
We're giving away four copies each of Cloud Native Patterns and Natural Language Processing and have the authors on-line!
See this thread and this one for details.
Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Hibernate Spring repository returns new object and therefore breaks references between objects

 
Greenhorn
Posts: 11
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I am having issues with hibernate within spring and would really appreciate any help.

The scenario is that I have a many-to-many relationship between objects "student" and "module"where a module contains a list of students, and a student contains a list of modules,

The idea being that I can modify all student objects belonging to a module when something changes, and equally allow the student objects to immediately pick up changes, should something change in the module.

The issue is that every time I pull from the db, the repository returns a new object, with a new hashcode meaning The Module objects contained in Student no longer point to the Module Objects in the DB, and vice-versa.

The repository is @Autowired

The student object is added to the principal so I don't need to call the DB every time a page loads.

As an exmaple of what I mean:
The original object is created, added to Student and persisted to the db. It looks like this:


The user then navigates to a new page where the module object is pulled from the db and compared to the one in the student object:


As you can see it is an object with the same attributes, a clone of the original but a completely different object with a different reference. So If I update this object, the Module object residing in student will be unaffected As the reference equality check shows.



Th student object holds a reference to the originally persisted object, while the object grabbed from the db is a new object.



Here is my hibernate setup:
Student:


Module:






I know this is a lot to read, so any help would be greatly appreciated!
 
Sheriff
Posts: 9643
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see the images you posted (the domain is blocked in my office), but you can try to override the equals and hashCode methods in your Entity classes. Check this documentation which recommends overriding these methods in situations similar to yours...
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!