hibernate - Grails validation over own domain -


i'm using grails 2.4.2 , have class contract has many invoicerecipient's. invoicerecipients class has attribute invoicetype has 2 possible values, 'o' invoice-original , 'c' invoice-copy. imagine, 1 record type 'o' allowed invoicerecipients 1 contract.

if try implement in following snipplet, vm runs stackoverflow.

another approach tried service method iterates through recipients array of contract count records invoicetype 'o' or tried select count through invoicerecipient.countbycontractandinvoicetype() determine number of 'o's in contract->invoicerecipients relation in controller.

in both last cases, hibernate generates update statement current invoicerecipient record, try validate. , if validation of current invoicerecipient fails , populate errors-object of instance, record updated (without problems, because constraint not coded class , throws no error in "save".) , have logical-wrong records in database.

class contract implements serializable {     ...     static hasmany = [recipients: invoicerecipient]     ... }  class invoicerecipient implements serializable {     static belongsto = [contract: contract]     ...     string invoicetype     ...      static constraints = {         invoicetype nullable: false, maxlength: 1, inlist: ['o', 'c'], validator: { val, obj ->         /* => generates stackoverflow         if (invoicerecipient.countbycontractandinvoicetype(obj.contract, 'o') > 1)             return "invoicerecipient.original.max.exceed"         */     } } 

i'd use this:

validator: { val, obj -> if (obj.recipients.findall{invoicetype == 'o'}.size() > 1)         return "invoicerecipient.original.max.exceed" 

this way should able prevent hibernate trying flush dirty objects , in process revalidate object.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -