Reputation: 57
I have simple domain:
package app
class Customers {
String CUSTOMER
String NOTE
static mapping = {
version false
id column: 'ID_CUSTOMER', type: 'long',
generator: 'sequence',
params: [sequence: 'CUSTOMER_SEQ']
}
static constraints = {
CUSTOMER(nullable: false, blank: false, unique: ['NOTE'])
NOTE(nullable: true, blank: true)
}
}
Columns and constraints are created properly in database (I want constraint on customer and note together), but instead of getting:
grails.validation.**ValidationException**
I am getting:
Message: Hibernate operation: could not execute statement; SQL [n/a]; ORA-00001: unique constraint (TEST2.UNIQUE_CUSTOMER_NOTE) violated
; nested exception is java.sql.SQLIntegrityConstraintViolationException: ORA-00001: unique constraint (TEST2.UNIQUE_CUSTOMER_NOTE) violated
Shouldn't I get ValidationException when constrains are violated?
Controller code is standard auto code generated by the generate-all
script.
Upvotes: 0
Views: 1247
Reputation: 75671
You don't show any of the controller code - are you checking the return value of validate()
and save()
calls (or using hasErrors()
) and redirecting back so the user can fix the mistakes?
What you're seeing will happen if you change one or both values and they're no longer unique, but you don't call validate()
or save()
. That's because at the end of the request, any modified instances will be updated in the database because the session gets flushed, and Hibernate uses its cached copy of the original values to detect modified/dirty instances that haven't been saved and flushed yet.
If you change a value but don't plan on saving the change (e.g. if you set values only for rendering the GSP) be sure to evict the instance from the session by calling discard()
- Hibernate won't try to push those changes if the instance is no longer attached to the session.
Upvotes: 1