apache / apache/grails-core

Grails 3.3.0 findAllBy* in bidirectional hasMany relation is producing error.

Open
#14,600 6 comments 0 reactions 0 assignees View on GitHub
relates-to: gorm
Dominant language
Groovy
Stars
2.9k
Forks
975
Avg merge
1d 22h
Merged PRs (30d)
92

Description

I was trying to use addTo* method, then findAllBy*, it is producing unexpected error.

GORM dynamic finder should not suggest such methods -
Book.findAllByAuthors() if it does not work.

I posted the details in [Stackoverflow](https://stackoverflow.com/questions/47138194/grails-3-3-0-bidirectiona-hasmany-is-not-working),

```
class Author {

String name

static hasMany = [fiction: Book, nonFiction: Book]
}
```

```
class Book {
String title

static belongsTo = Author
static hasMany = [authors:Author]
}
```

```
class BookController {
def testBook(){
def fictBook = new Book(title: "IT")
def fictBook2 = new Book(title: "MBA")
def fictBook3 = new Book(title: "DBA")
def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
def nonFictBook2 = new Book(title: "Cleaning Codex writer")
def a = new Author(name: "Stephen King")
.addToFiction(fictBook)
.addToNonFiction(nonFictBook)
.save(flush:true)

println a.fiction
// println Book.findAllByAuthors([a]) // doesn't work, same error
println Book.withCriteria() {
'in'('authors', [a])
}

// it works
// println Book.withCriteria() {
// authors {
// 'in'('id', [a.id])
// }
// }

render "helllo"
}
}
```

### Expected Behaviour
Book.findAllByAuthors() should give the list of the books from given Authors

### Actual Behaviour
```
URI /book/testBook
Class org.h2.jdbc.JdbcSQLException
Message null
Caused by Parameter "#1" is not set; SQL statement: select this_.id as id1_4_0_, this_.version as version2_4_0_, this_.title as title3_4_0_ from book this_ where this_.id in (?) [90012-195]
```

### Environment Information
- **Operating System**: Ubuntu 17.04
- **Grails Version:** 3.3.0
- **JDK Version:** 1.8.0_131
- **gormVersion:** 6.1.6.RELEASE
- **gradleWrapperVersion:** 3.5

### Example Application

- TODO: link to github repository with example that reproduces the issue

Contributor guide

Open the contributing guide

Research direction

Start with the Book.findAllByAuthors([a]) call in the testBook controller action and reproduce the H2 error using the supplied Author and Book mappings. Compare the failing dynamic finder with the working withCriteria examples. Done means the expected list of books is returned without the parameter-binding error, or the finder is no longer suggested if unsupported.

Written by the indexing model from the issue text.

Assessment

Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.