cant get grails console and domain class create to work, nor where queries help...

woodmawa

New member
ok trying another forum - not getting any reply off nabble and i need some help

running grails 2.01


trying to do some real basic dummy code to get used to working with Gorm and keep failing at first hurdle

my integration tests fail, and i cant get domain class created in console either ! - advicxe required

i create three dummy domain classes

Author 1:m to Book, and Book m:n with Reader. I make Book the 'owner' for both relationshiops like this

Author Domain class:
package com.softwood.book

class Author {

String name
Date dateCreated
//List books //change set to list

static hasMany = [books:Book]

static constraints = {
name (size: 3..20)
}
}

Book domaion class:
package com.softwood.book

class Book {

String title
Date dateCreated

static hasMany = [readBy:Reader]

static belongsTo = [author:Author]

static constraints = {
title (size: 3..20)
}
}

Reader domain class
package com.softwood.book

class Reader {

String name
Date dateCreated

static hasMany = [hasRead:Book]

static belongsTo = [Book]

static constraints = {
}
}

- when i try to run from console the following i cant cant create a domain class - it returns null - script like this

import com.softwood.book.Book
import com.softwood.book.Author
import com.softwood.book.Reader

class Dummy
{
String name
}

def d = new Dummy (name : "will")
assert d.name == "will"

res = new Book (title:"my book ")
assert res.title == "my book"
def sav = res.save()
assert sav?.hasErrors () == null

def books = Book.list ()
books.each {-> println it.title}

output says
Assertion failed:

assert res.title == "my book"
| | |
| | false
| my book
com.softwood.book.Book : null

when i try to build integration test and run it both these tests return [] when running a query across an association (its ok if i only query an attribute in the class) - what am i doing wrong ??

...
@Test
void testReadersQuery()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")

def will = new Reader (name:"william")
def maz = new Reader (name:"marian")

book1.addToReadBy (will)
book2.addToReadBy (maz)
book3.addToReadBy (will)
book3.addToReadBy (maz)

book1.save()
book2.save()
book3.save ()

assert book1.readBy.size() == 1
assert book2.readBy.size() == 1
assert book3.readBy.size() == 2
assert will.hasRead.size() ==2
assert maz.hasRead.size() == 2

//query m:n relationship
def bQuery = Book.where {
readBy{name == "william"}
}

def resBook = bQuery.findAll()
assert resBook != [] //fails here
assert resBook.size() == 2


}

@Test
void testAuthorsQuery ()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")

def unk = new Author (name:"uknown")
unk.addToBooks (book1)
unk.addToBooks (book2)
unk.save()
assertFalse unk.hasErrors()

def jk = new Author (name: "JK Rowling")
jk.addToBooks (book3).save()
assertFalse jk.hasErrors()

//query 1:m relationship
def resUnkAuthorQuery = Author.where
{
books.title == "unknown"
}
def resUnkAuth = resUnkAuthorQuery.findAll()
assert resUnkAuth != [] //fails here
assert resUnkAuth.size() == 2

}
 

woodmawa

New member
by golly hard work thats been, improved but not fixed .

i eventually did getErrors on the book.save() and it fails - it was demanding that author could not be null. so changed the Book contraints as follows

class Book {

String title
Date dateCreated

static hasMany = [readBy:Reader]

static belongsTo = [author:Author]

static constraints = {
title (size: 3..20)
readBy (nullable : true)
author (nullable : true ) //constraint to ensure its nullable
}

String toString ()
{
"$title"
}
}

so all of a sudden a book.save () with readers or authors can be saved - problem # 1 fixed .

however i still have two test fails i cant figure

@Test
void testFindReaderByBookQuery ()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")

def will = new Reader (name:"william")
def maz = new Reader (name:"marian")

book1.addToReadBy (will)
book2.addToReadBy (maz)
book3.addToReadBy (will)
book3.addToReadBy (maz)

book1.save()
book2.save()
book3.save ()

...
def fRes = Reader.findAllByHasRead (Book.findByTitle ("war of the worlds"))
assert fRes != null
assert fRes.size() == 1
}

which errors seriously with - not sure it thinks #1 hasnt been set .

Error 2012-02-21 23:02:21,815 [main] ERROR util.JDBCExceptionReporter - Parameter "#1" is not set; SQL statement:
select this_.id as id3_0_, this_.version as version3_0_, this_.date_created as date3_3_0_, this_.name as name3_0_ from reader this_ where this_.id=? [90012-147]
| Failure: testFindReaderByBookQuery(com.softwood.book.AuthorIntegTests)
| org.springframework.jdbc.UncategorizedSQLException: Hibernate operation: could not execute query; uncategorized SQLException for SQL [select this_.id as id3_0_, this_.version as version3_0_, this_.date_created as date3_3_0_, this_.name as name3_0_ from reader this_ where this_.id=?]; SQL state [90012]; error code [90012]; Parameter "#1" is not set; SQL statement:
select this_.id as id3_0_, this_.version as version3_0_, this_.date_created as date3_3_0_, this_.name as name3_0_ from reader this_ where this_.id=? [90012-147]; nested exception is org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
select this_.id as id3_0_, this_.version as version3_0_, this_.date_created as date3_3_0_, this_.name as name3_0_ from reader this_ where this_.id=? [90012-147]
at org.grails.datastore.gorm.GormStaticApi.methodMissing(GormStaticApi.groovy:108)
at com.softwood.book.AuthorIntegTests.testFindReaderByBookQuery(AuthorIntegTests.groovy:211)
Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
select this_.id as id3_0_, this_.version as version3_0_, this_.date_created as date3_3_0_, this_.name as name3_0_ from reader this_ where this_.id=? [90012-147]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.expression.Parameter.checkSet(Parameter.java:73)
at org.h2.command.Prepared.checkParameters(Prepared.java:161)
at org.h2.command.CommandContainer.query(CommandContainer.java:77)
at org.h2.command.Command.executeQuery(Command.java:132)
at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
... 2 more


the other test

@Test
void testAuthorsQuery ()
{
def book1 = new Book (title:"war of the worlds")
def book2 = new Book (title: "famous five")
def book3 = new Book (title: "goblet of fire")

def unk = new Author (name:"uknown")
unk.addToBooks (book1)
unk.addToBooks (book2)
unk.save()
...
//query 1:m relationship
def resUnkAuthorQuery = Author.where
{
books.title == "unknown"
}
/*
def resUnkAuthorQuery = Book.where
{
author{name == "unknown"}
} */
def resUnkAuth = resUnkAuthorQuery.findAll()
assert resUnkAuth != []
assert resUnkAuth.size() == 2

}

returns emtpty [] and not records i was expecting - dont understand that

any additional thoughts still well received
 
Top