Announcement Announcement Module
Collapse
No announcement yet.
Detecting unique constraint violation in db independent way Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Detecting unique constraint violation in db independent way

    How can i solve this problem in a clean and DB independent way? Imagine i have a Vehicle Object which has several properties that should be unique e.g. chasis no, engine no and registration number. How can i detect if any of this constraints is being violated and then being able to present the user with a clear error message that clearly indicates which property(s) violated the constraint?

    I've tried selecting before saving and it worked for first time saves but fell apart when i tried to do updates. Or is it not possible to do this in DB independent way?

  • #2
    Creating unique indexes on your tables and then catching org.springframework.dao.DataIntegrityViolationExce ption should work.

    Comment


    • #3
      Originally posted by dejanp
      Creating unique indexes on your tables and then catching org.springframework.dao.DataIntegrityViolationExce ption should work.
      Ok i'll try that. Thanks

      Comment


      • #4
        I also the same problem but unlike you I have only one field that is unique. I am wandering what is the common solution for this problem? I see many unanswer topics with this theme.

        Also, sometimes you getting this exception org.hibernate.exception.ConstraintViolationExcepti on instead of the one you mentioned above.

        Also, if you new record, this is the exception org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs) you would see.

        Anyway, please let me know if you have it working with your problem.

        Comment


        • #5
          same problem

          Originally posted by hhoang
          I also the same problem but unlike you I have only one field that is unique. I am wandering what is the common solution for this problem? I see many unanswer topics with this theme.

          Also, sometimes you getting this exception org.hibernate.exception.ConstraintViolationExcepti on instead of the one you mentioned above.

          Also, if you new record, this is the exception org.hibernate.AssertionFailure: null id in entry (don't flush the Session after an exception occurs) you would see.

          Anyway, please let me know if you have it working with your problem.
          I find that for single unique fields it's not that much of problem. I usually catch org.springframework.dao.DataIntegrityViolationExce ption as that only unique field is the only one that can throw it. So in that case i just call showForm() in my catch block to notify the user of the error. I yet have to test the solution suggested above and if it works i'll probably post some feedback

          Comment

          Working...
          X