May be I don't understand what you are trying to do, but, how can you know if a unique constraint is violated before you try to save to the database? I don't think is possible to do with the mapping file, as hibernate does not have a list of the already stored values, unless you have a second level cache (and the data is in the cache)
Hibernate will throw a ConstraintViolationException, but it will be done after trying to save in the database (In fact, the exception, is the result of a database exception)
The only solution, would be to check the database before saving, to see if the key is already present in the database. I do this with foreign keys
By the way, the unique attribute of the property tag, is only useful when automatically generating the database from the mapping files.
|