1 /* Copyright 2004-2005 Graeme Rocher
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 package org
.codehaus
.groovy
.grails
.validation
;
17 import org
.springframework
.validation
.Errors
;
18 import org
.springframework
.beans
.BeanWrapper
;
19 import org
.codehaus
.groovy
.grails
.commons
.GrailsDomainClassProperty
;
20 import org
.hibernate
.collection
.PersistentCollection
;
23 * A validator that first checks if the Hibernate PersistentCollection instance has been initialised before bothering
26 * @author Graeme Rocher
30 * Created: Apr 13, 2007
33 public class HibernateDomainClassValidator
extends GrailsDomainClassValidator
{
36 * Overrides the default behaviour and first checks if a PersistentCollection instance has been initialised using the
37 * wasInitialised() method before cascading
39 * @param errors The Spring Errors instance
40 * @param bean The BeanWrapper for the bean
41 * @param persistentProperty The GrailsDomainClassProperty instance
42 * @param propertyName The name of the property
44 * @see org.hibernate.collection.PersistentCollection#wasInitialized()
46 protected void cascadeValidationToMany(Errors errors
, BeanWrapper bean
, GrailsDomainClassProperty persistentProperty
, String propertyName
) {
47 Object collection
= bean
.getPropertyValue(propertyName
);
48 if(collection
!= null) {
49 if(collection
instanceof PersistentCollection
) {
50 PersistentCollection persistentCollection
= (PersistentCollection
)collection
;
51 if(persistentCollection
.wasInitialized()) {
52 super.cascadeValidationToMany(errors
, bean
, persistentProperty
, propertyName
);
56 super.cascadeValidationToMany(errors
, bean
, persistentProperty
, propertyName
);