| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Grails
Revision: 7240
Author: graeme
Date: 21 Jul 2008 11:19:52
Changes:fix for GRAILS-3084
Files:| ... | ...@@ -15,6 +15,7 @@ | |
| 15 | 15 | package org.codehaus.groovy.grails.orm.hibernate.validation; |
| 16 | 16 | |
| 17 | 17 | import groovy.lang.GString; |
| 18 | import org.codehaus.groovy.grails.commons.DomainClassArtefactHandler; | |
| 18 | 19 | import org.codehaus.groovy.grails.commons.GrailsClassUtils; |
| 19 | 20 | import org.codehaus.groovy.runtime.InvokerHelper; |
| 20 | 21 | import org.hibernate.Criteria; |
| ... | ...@@ -27,6 +28,7 @@ | |
| 27 | 28 | import org.springframework.validation.Errors; |
| 28 | 29 | |
| 29 | 30 | import java.util.ArrayList; |
| 31 | import java.util.Collections; | |
| 30 | 32 | import java.util.Iterator; |
| 31 | 33 | import java.util.List; |
| 32 | 34 | |
| ... | ...@@ -121,16 +123,25 @@ | |
| 121 | 123 | session.setFlushMode(FlushMode.MANUAL); |
| 122 | 124 | |
| 123 | 125 | try { |
| 124 | Criteria criteria = session.createCriteria( constraintOwningClass ) | |
| 125 | .add( Restrictions.eq( constraintPropertyName, propertyValue ) ); | |
| 126 | if( uniquenessGroup != null ) { | |
| 127 | for( Iterator it = uniquenessGroup.iterator(); it.hasNext(); ) { | |
| 128 | String propertyName = (String) it.next(); | |
| 129 | criteria.add(Restrictions.eq( propertyName, | |
| 130 | GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(target, propertyName))); | |
| 131 | } | |
| 132 | } | |
| 133 | return criteria.list(); | |
| 126 | boolean shouldValidate = true; | |
| 127 | if(propertyValue != null && DomainClassArtefactHandler.isDomainClass(propertyValue.getClass())) { | |
| 128 | shouldValidate = session.contains(propertyValue); | |
| 129 | } | |
| 130 | if(shouldValidate) { | |
| 131 | Criteria criteria = session.createCriteria( constraintOwningClass ) | |
| 132 | .add( Restrictions.eq( constraintPropertyName, propertyValue ) ); | |
| 133 | if( uniquenessGroup != null ) { | |
| 134 | for( Iterator it = uniquenessGroup.iterator(); it.hasNext(); ) { | |
| 135 | String propertyName = (String) it.next(); | |
| 136 | criteria.add(Restrictions.eq( propertyName, | |
| 137 | GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(target, propertyName))); | |
| 138 | } | |
| 139 | } | |
| 140 | return criteria.list(); | |
| 141 | } | |
| 142 | else { | |
| 143 | return Collections.EMPTY_LIST; | |
| 144 | } | |
| 134 | 145 | } finally { |
| 135 | 146 | session.setFlushMode(FlushMode.AUTO); |
| 136 | 147 | } |
| ... | ...@@ -0,0 +1,37 @@ | |
| 1 | package org.codehaus.groovy.grails.orm.hibernate | |
| 2 | /** | |
| 3 | * @author Graeme Rocher | |
| 4 | * @since 1.0 | |
| 5 | * | |
| 6 | * Created: Jul 21, 2008 | |
| 7 | */ | |
| 8 | class CascadingSaveAndUniqueConstraintTests extends AbstractGrailsHibernateTests{ | |
| 9 | ||
| 10 | protected void onSetUp() { | |
| 11 | gcl.parseClass(''' | |
| 12 | class Face { | |
| 13 | Long id | |
| 14 | Long version | |
| 15 | Nose nose | |
| 16 | ||
| 17 | static constraints = { | |
| 18 | nose(unique: true) | |
| 19 | } | |
| 20 | } | |
| 21 | class Nose { | |
| 22 | Long id | |
| 23 | Long version | |
| 24 | static belongsTo = [face:Face] | |
| 25 | } | |
| 26 | ''') | |
| 27 | } | |
| 28 | ||
| 29 | ||
| 30 | void testCascadingSaveAndUniqueConstraint() { | |
| 31 | def faceClass = ga.getDomainClass("Face").clazz | |
| 32 | def noseClass = ga.getDomainClass("Nose").clazz | |
| 33 | ||
| 34 | def face = faceClass.newInstance(nose:noseClass.newInstance()).save() | |
| 35 | } | |
| 36 | ||
| 37 | } | |
| 0 | 38 | \ No newline at end of file |