Announcement Announcement Module
Collapse
No announcement yet.
Effects of Save on an existing object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    Put a constraint on the actual database table. For example, your primary key, which is the object ID, could have a uniqueness constraint.

    Comment


    • #3
      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

      Comment


      • #4
        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.

        Comment


        • #5
          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

          Comment


          • #6
            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

            Comment

            Working...
            X