tags:

views:

71

answers:

1

I have a class like this (with getters and setters, all mapped to hibernate, each with a sequence to oracle DB)

 public class A{
       private B b;
       private C c;
 }

i'm creating this object and saving it to the database.

So i create an A object and populate it with a B object and call

saveOrUpdate(a)

so i can have "a" and "b" sequences generated. then i do some calculations and create an instance of "C" and set it to "a"

Up to here, everything works great. I now need to get "c"'s id so i do some checks before i flush and commit.

But saveOrUpdate won't work here because the object is no longer transient. According to hibernate docs on saveOrUpdate: "if the object is already persistent in this session, do nothing "

Any ideas?

(can't post the code because it's too long and messy, but it's basically that)

A: 

if C is an hibernate mapped class and the propery is correctly mapped, than hibernate will detect that the property C has changed in will save it for you when the session flushes or the active transaction will commit. Then it is possible to get the id of C after the commit.

An other approach is to call saveOrUpdate(c). In that case c get an id assigned, so you can retrieve it.

What checks are you doing that you need c's id?

Salandur
right. but i need C's id before i flush and commit. and calling saveOrUpdate on "a" a second time does nothing because "a" has already been made persistent in this session so it doesn't even check if it's been changed (not until i flush, which i can't do because DB constraints). I need c's Id to make an url with that id in the querystring and to put it in a text that goes in another field.
wll, than you have to make an saveOrUpdate(c) call, so hibernate will assign an id, which u can use for your text field. if the field is an propery of c, it will be correctly persisted by hibernate.
Salandur