• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Linwood Hayes
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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: 32
  • Mark post as helpful
  • send pies
  • 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
 
Rob Spoor
Sheriff
Posts: 20879
71
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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: 32
  • Mark post as helpful
  • send pies
  • 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: 20879
71
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!