Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Merge pull request #107 from eugenypozharsky/GRAILS-9936
Browse files Browse the repository at this point in the history
(GRAILS-9936) Fixed unique constraint
  • Loading branch information
graemerocher committed Aug 21, 2013
2 parents 000151c + 6e4d8e9 commit feb6ba3
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 7 deletions.
Expand Up @@ -202,13 +202,8 @@ public List<?> doInHibernate(Session session) throws HibernateException {
Object uniquenessGroupPropertyValue = GrailsClassUtils.getPropertyOrStaticPropertyOrFieldValue(target, uniquenessGroupPropertyName);

if (uniquenessGroupPropertyValue != null && DomainClassArtefactHandler.isDomainClass(uniquenessGroupPropertyValue.getClass())) {
try {
// We are merely verifying that the object is not transient here
session.lock(uniquenessGroupPropertyValue, LockMode.NONE);
}
catch (TransientObjectException e) {
shouldValidate = false;
}
// We are merely verifying that the object is not transient here
shouldValidate = session.contains(uniquenessGroupPropertyValue);
}
if (shouldValidate) {
criteria.add(Restrictions.eq(uniquenessGroupPropertyName, uniquenessGroupPropertyValue));
Expand Down
@@ -0,0 +1,53 @@
package grails.gorm.tests

import grails.persistence.Entity
import spock.lang.Issue

class UniqueConstraintGroupWithoutVersioningSpec extends GormDatastoreSpec{

@Issue('GRAILS-9936')
void "Test transient uniqueness handling"() {
given:"Some linked objects"
def a2 = new UniqueConstraintTestParentEntity(prop: "prop1")
a2.id = 1
def b3 = new UniqueConstraintTestChildEntity(prop: "b1", a: a2)
a2.bs = [b3]
when:"validate child item"
def processed = true
try {
b3.validate()
} catch (Exception ex) {
processed = false
}
then:"no exception is thrown"
processed
}

@Override
List getDomainClasses() {
[UniqueConstraintTestParentEntity, UniqueConstraintTestChildEntity]
}

def setup() {}
}

@Entity
class UniqueConstraintTestParentEntity {
Long id
String prop
static hasMany = [bs: UniqueConstraintTestChildEntity]
static mapping = {
version false
id generator: "assigned"
}
}

@Entity
class UniqueConstraintTestChildEntity {
String prop
static belongsTo = [a: UniqueConstraintTestParentEntity]

static constraints = {
prop unique:["a"]
}
}

0 comments on commit feb6ba3

Please sign in to comment.