Results 1 to 6 of 6

Thread: Effects of Save on an existing object

  1. #1
    Join Date
    Oct 2004
    Posts
    5

    Default Effects of Save on an existing object

    Using Hibernate, DAO support, and templating...

    I have an object that is already persisted to the database. I try to save it again and it does an update. Is there any way I can have it throw an exception if the object already exists in the database?

    Thanks.

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Put a constraint on the actual database table. For example, your primary key, which is the object ID, could have a uniqueness constraint.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    I assume you're using "saveOrUpdate", you could try "save" alone.
    Putting a constraint shouldn't help as if an id is present in the persisted object, hibernate detects it and choose "update" instead of "insert".
    A primary key is always unique.
    If "save" doen't help, you should manually "load(Class, Serializable)" and if there's a result, raise an exception yourself.

    Hope it helps

    Olivier

  4. #4
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Quote Originally Posted by ojolly
    A primary key is always unique.
    Of course, what I mean is that there may not even be a primaty key declaration in the db, or the key could be compound, etc. The db constraint needs to be there.

    Re-reading the original question though, he's saying it's doing an update, so that fits with saveOrUdate being used instead of save. Using save, with a proper pk definition in the db, should catch dupes. Note that Hibenate will not necessarilly write out the object at the time of the save (depending on the id generation strategy), but potentially as late as when the transaction commits, which will force a flush.
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  5. #5
    Join Date
    Oct 2004
    Posts
    5

    Default

    Quote Originally Posted by ojolly
    I assume you're using "saveOrUpdate", you could try "save" alone.
    Putting a constraint shouldn't help as if an id is present in the persisted object, hibernate detects it and choose "update" instead of "insert".
    A primary key is always unique.
    If "save" doen't help, you should manually "load(Class, Serializable)" and if there's a result, raise an exception yourself.

    Hope it helps

    Olivier
    I did a check as Colin pointed out and the primary key is set. I get a duplicate entry error if I try and do a duplicate add. I am just doing a save and I thought in some of my straight Hibernate code this threw an exception. I will have to look again.

    Guess I have to decide if the business case wants me to make sure it does not exist or not.

    Thanks.

    Bryan

  6. #6
    Join Date
    Aug 2004
    Location
    Toulouse, France
    Posts
    148

    Default

    I get a duplicate entry error if I try and do a duplicate add.
    Ok, isn't what you expected ? Isn't it a catchable exception which lets you handle this with business behaviour ?

    Olivier

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  5. Replies: 38
    Last Post: May 11th, 2005, 02:49 PM

Posting Permissions

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