Skip to content

Commit

Permalink
Fix bug: using both property walking and a mapping that referenced th…
Browse files Browse the repository at this point in the history
…e same association property caused duplicate aliases to be created.
  • Loading branch information
jcoleman committed May 13, 2011
1 parent d0565d5 commit fa7b0a0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 7 deletions.
60 changes: 60 additions & 0 deletions features/subquery.feature
Expand Up @@ -164,4 +164,64 @@ Feature: Sub-queries
)
}.all()
"""

Scenario: Using both property('../propName') walking and mapping for the same association property at the same time (bug: query exception due to duplicate alias being created had been created by new property walking code)
Given I have created the following "Book" instances:
| title | authorName |
| Lord of the Rings | Tolkien |
| 20000 Leagues Under the Sea | Verne |
And I have created the following "Author" instances:
| name |
| Tolkien |
| Verne |
| Bernstein |
When I execute the following code:
"""
Book.where {
id mapTo: 'book_id'
author is: notNull
author where: {
name mapTo: 'author_name'
}
exists(
Book.where {
id equals: mapping('book_id')
property('../author.name') ne: 'Bernstein'
author where: {
name equals: mapping('author_name')
}
}
)
}.all()
"""
Then I should get the following results:
| name |
When I execute the following code:
"""
def book = Book.findByTitle('Lord of the Rings')
book.author = Author.findByName('Tolkien')
book.save()
"""
And I execute the following code:
"""
Book.where {
id mapTo: 'book_id'
author is: notNull
author where: {
name mapTo: 'author_name'
}
exists(
Book.where {
id equals: mapping('book_id')
property('../author.name') ne: 'Bernstein'
author where: {
name equals: mapping('author_name')
}
}
)
}.all()
"""
Then I should get the following results:
| title |
| Lord of the Rings |

24 changes: 17 additions & 7 deletions src/groovy/com/radiadesign/relationalscope/RelationalScope.groovy
Expand Up @@ -355,16 +355,25 @@ class RelationalScope {

static createAssociationAliasIfNecessary(options, associationPath, associationDescriptor=null) {
def discriminator = RelationalScope.aliasDiscriminatorFor(options, associationDescriptor)
def aliasMap = options.associationAliases[discriminator]
def optionsOrAssociationDescriptor = (associationDescriptor ?: options)
def aliasMap = optionsOrAssociationDescriptor.associationAliases[discriminator]
if (!aliasMap) {
aliasMap = options.associationAliases[discriminator] = [:]
aliasMap = optionsOrAssociationDescriptor.associationAliases[discriminator] = [:]
}
if (!aliasMap[associationPath]) {
def alias = "${discriminator}_${associationPath.replace('.', '_')}"
(associationDescriptor ?: options).criteria
.createAlias( associationPath,
alias,
CriteriaSpecification.LEFT_JOIN )
println "\n\n\ncreating alias: '${alias}' with discriminator '${discriminator}'\n"
println "already had aliases:\n${options.associationAliases.inspect()}\n\n"
/*try {
throw new Exception()
} catch (e) {
e.printStackTrace()
}*/

optionsOrAssociationDescriptor.criteria
.createAlias( associationPath,
alias,
CriteriaSpecification.LEFT_JOIN )
aliasMap[associationPath] = alias
}
}
Expand Down Expand Up @@ -427,7 +436,7 @@ class RelationalScope {
}

def discriminator = RelationalScope.aliasDiscriminatorFor(options, associationDescriptor)
def discriminatedAliases = options.associationAliases[discriminator]
def discriminatedAliases = (associationDescriptor ?: options).associationAliases[discriminator]
assert discriminatedAliases : "An association was used for which no alias has been created"
alias = discriminatedAliases[associationPath]
assert alias : "An association was used for which no alias has been created"
Expand All @@ -443,6 +452,7 @@ class RelationalScope {
associationDomainProperty: associationDomainProperty,
isDetachedCriteria: options.isDetachedCriteria,
detachedCriteriaCount: options.getDetachedCriteriaCount(),
associationAliases: options.associationAliases,
criteria: options.criteria
)
}
Expand Down

0 comments on commit fa7b0a0

Please sign in to comment.