• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

can someone help me understand why one transient object can be saved, but not two together ?

 
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you can


you can save Student object without problem.  But if it is



Then when you do


"
It would give error like "object references an unsaved transient instance - save the transient instance before flushing".  basically it comlains object "address" is transient.  But, it works when "Student" doesn't have reference to "Address".  In that case, isn't "student" object also transient ? why can "student" object be saved when it is transient ? but just can't be saved when it refers to another transient object ?
 
Linwood Hayes
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry, in my POJO classes, I forgot to add the one-to-many relation annotation in the two classes..  But that's not the main point.  My question was about that hibernate exception and why it happens.  Thanks
 
Sheriff
Posts: 22783
131
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
If you don't specify any cascade actions explicitly, no action is cascaded. That means that your Address will not be stored in the database, and that in turn would mean the Student would lose its reference. Instead, an exception is thrown, so you're made aware that you made a mistake.

Possible solutions:
- Persist the Address object manually before you set it on the Student object.
- Add at least PERSIST as cascade action on the Student side of the relationship. That way, whenever a Student's Address is not persisted, this will be done automagically.
 
Linwood Hayes
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:If you don't specify any cascade actions explicitly, no action is cascaded. That means that your Address will not be stored in the database, and that in turn would mean the Student would lose its reference. Instead, an exception is thrown, so you're made aware that you made a mistake.

Possible solutions:
- Persist the Address object manually before you set it on the Student object.
- Add at least PERSIST as cascade action on the Student side of the relationship. That way, whenever a Student's Address is not persisted, this will be done automagically.



Got it.  But when Address is not saved and Student loses reference,  does it affect saving *other* attributes (like student name, etc) of "Student" ? i.e. would the entire saving of "Student" object fail, or just miss the part of saving Address and reference to address ?
 
Rob Spoor
Sheriff
Posts: 22783
131
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
That's the thing - to prevent your database to end up in a state that you don't intend (i.e. with all fields except the address set), nothing will be set but you get this exception instead. To set everything but the Address, simply don't set an Address on the Student. Then, everything that is set will be stored in the database.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic