Results 1 to 5 of 5

Thread: call save on an already persistent entity

  1. #1
    Join Date
    Jan 2008
    Posts
    3

    Default call save on an already persistent entity

    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 int id;
    private B b;
    private C c;
    }

    classes B and C are simple classes also mapped with ids mapped to sequences

    i'm trying to create an object "A" and save it to the database.

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

    saveOrUpdate(a)

    so now i 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's 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 "

    (can't post the actual code because it's too long)

    Any help is appreciated


    Thanks

  2. #2

    Default

    Then i do some calculations and create an instance of "C" and set it to "a"
    How do you do this? Are you doing A c = new A(); and setting the properties, or are you doing A c = a;?

    If the former, then Hibernate will not consider c to be transient, as it is not the same instance as a. If the latter, then c is the same object as a, and Hibernate will treat it as such.

  3. #3
    Join Date
    Jan 2008
    Posts
    3

    Default

    Thanks for you reply.

    Actually, "a" "b" and "c" are of different classes.

    I do something like:
    A a = new A(),
    B b = new B();
    a.setB(b);

    call saveOrUpdate on "a" here, so i can get a.id and b.id

    C c = new C();
    a.setC(c);

    Now, i need to get c.id so I can do some things with it. (**)

    Then flush and commit.

    (**) Here, if i call saveOrUpdate on "a" again it does nothing because "a" has already been made persistent in this session...
    And i cant flush earlier because of some DB constraints.

  4. #4

    Default

    What happens if you use update() instead of saveOrUpdate()?

  5. #5
    Join Date
    Jan 2008
    Posts
    3

    Default

    It's just the same, org.hibernate.impl.SessionImpl internally calls a saveOrUpdate() when you call update().

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •