I'm having an issue when using a find by on a domain class:
The error I'm getting is:
ERROR util.JDBCExceptionReporter - No value specified for parameter 2
The domain classes are:
class AppDetail {
String name
String url
Boolean active
static hasMany = [authString:AppIdentifier]
static constraints = {
name(unique:true,blank:false)
active(nullable:true)
}
class AppIdentifier {
Date added
String authString
static belongsTo = [authString:AppDetail]
static constraints = {
added()
authString(blank:false)
}
The find by is:
def identifier = AppIdentifer.findByAuthString('test')
def appDetails = AppDetail.findByAuthString(identifier)
Can anyone provide any insight into the meaning of this error?
Thanks in advance!
You have too many fields named "authString". Here's another way to redo the same classes:
class AppDetail {
String name
String url
Boolean active
static hasMany = [identifiers:AppIdentifier]
static constraints = {
name(unique:true,blank:false)
active(nullable:true)
}
class AppIdentifier {
Date added
String authString
static belongsTo = [appDetail:AppDetail]
static constraints = {
added()
authString(blank:false)
}
def identifier = AppIdentifer.findByAuthString('test')
def appDetails = identifier.appDetail
You defined authString both as String AND AppDetail:
String authString
static belongsTo = [authString:AppDetail]
Either remove String authString or change it to AppDetail authString
BTW From the point of view of GORM the property naming doesn't matter. But if you define a property with String in it's name, but of another class, you gonna get maintenance problems in the future.
Related
I have this class:
class Word {
String word
static hasMany = [group: WordGroup]
static belongsTo = [language: Language]
static constraints = {
word(unique: ['language'], maxSize:255)
}
}
Which is working fine, but I would need to associate the unique constraint to both language and group, so different groups can insert the same "word".
The groups are in an intermediate table (WordGroup) as this is a many to many relationship.
If I try to do
word(unique: ['language', 'group'], maxSize:255)
I get the error
Missing IN or OUT parameter in index:: 3
Is there a way to do this?
EDIT: I am adding the other classes referenced in the Word class if it helps
WordGroup
class WordGroup {
static belongsTo = [word: Word, group: Group]
static constraints = {}
}
Group
class Group {
String name
String url
static hasMany = [words: Word]
static constraints = {
name blank: false, unique: true
}
}
Language
class Language {
String name
String code
static constraints = {
name maxSize:255
code maxSize:255
}
}
Thanks.
I'm having a weird error caused by a many-to-many relationship between two domain classes. The classes are below. When I try to save the NoteParagraph object, I DON'T immediately get an error:
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText)
note.addToChildParagraphs(np).save()
However, on the next .save() action (on an unrelated object), I get the following error stacktrace:
Field 'note_paragraph_id' doesn't have a default value. Stacktrace follows:
java.sql.SQLException: Field 'note_paragraph_id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:102)
Here are my classes:
class NoteParagraph {
String number
String content
Note parentNote
Date dateCreated
Date lastUpdated
static belongsTo = Note
static hasMany = [
childNotes: Note
]
static constraints = {
}
static mapping = {
content type:'text'
}
}
and the second one:
class Note {
Integer noteType
Integer parentType
Integer parentId
String heading
Date dateCreated
Date lastUpdated
static hasMany = [
childParagraphs: NoteParagraph
]
static constraints = {
}
static mapping = {
heading type:'text'
}
}
I've tried deleting and recreating my database as some other threads have suggested, and that did not help.
Fix:
class NoteParagraph {
String number
String content
Date dateCreated
Date lastUpdated
static belongsTo = [parentNote: Note]
static hasMany = [
childNotes: Note
]
static constraints = {
}
static mapping = {
content type:'text'
}
}
When you try to add NoteParagraph to Note, NoteParagraph instance is not persistent (it is created but not saved in db yet).
Try to change:
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText)
note.addToChildParagraphs(np).save()
to:
note.save(flush: true) //add also this, if note is not persisted
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText, parentNote: note).save(flush: true)
note.addToChildParagraphs(np).save()
Good day to all. I am very new in using grails and I have followed several tutorials for beginners using grails until I come up in creating domain relationships. However, I got stuck with this problem right now. I have 3 domain classes namely, todo, category and user. And as I defined their relationships, it returns me an error saying unable to resolve class. Please see my codes below. Please help. Thank you so much.
Todo.groovy Class
package todoScaff
class Todo {
String name
String note
Date createDate
Date dueDate
Date completedDate
String priority
String status
User owner
Category category
static belongsTo = [User, Category]
static constraints = {
name(blank:false)
createDate()
priority()
status()
note(maxsize:1000, nullable:true)
completedDate(nullable:true)
dueDate(nullable:true)
}
String toString() {
name
}
}
Category.groovy Class
package categoryScaff
class Category {
String name
String description
User user
static belongsTo = User
static hasMany = [todos: Todo]
static constraints = {
name(blank:false)
}
String toString(){
name
}
}
User.groovy Class
package userScaff
class User {
String userName
String fname
String lname
static hasMany = [todos: Todo, categories: Category]
static constraints = {
userName(blank:false, unique:true)
fname(blank:false)
lname(blank:false)
}
String toString(){
"$lname, $fname"
}
}
Since you've placed your domain classes in different packages, you must import the classes at the head of the file.
package categoryScaff
import todoScaff.Todo
import userScaff.User
class Category {
The same needs to happen in your other domain classes that reference classes outside the current package.
I have a class in grails like:
class Autoresponder {
static String TYPE_NONE = "none"
static String TYPE_GETRESPONSE = "GetResponse"
static String TYPE_MAILCHIMP = "MailChimp"
static String TYPE_AWEBER = "AWeber"
static String TYPE_INFUSIONSOFT = "InfusionSoft"
static String TYPE_ICONTACT = "iContact"
static String TYPE_SENDY = "Sendy"
static String TYPE_ACTIVECAMPAIGN = "ActiveCampaign"
static String TYPE_API_ACTIVATE = "activate"
static String TYPE_ONTRAPORT = "ontraport"
//rest of code
}
I want to simply find that the above class has a static variable with value AWeber.
How do I do it? Is there a way to fetch all static user defined variables in a class(and thereby compare each variable's value with what I want)?
EDIT:
Due to some technical reasons, I can not change class definition.
Just iterate over all static fields looking for the one with the desired value. As in the following groovy script example
import static java.lang.reflect.Modifier.isStatic
class Autoresponder {
static String TYPE_NONE = "none"
static String TYPE_GETRESPONSE = "GetResponse"
static String TYPE_MAILCHIMP = "MailChimp"
static String TYPE_AWEBER = "AWeber"
static String TYPE_INFUSIONSOFT = "InfusionSoft"
static String TYPE_ICONTACT = "iContact"
static String TYPE_SENDY = "Sendy"
static String TYPE_ACTIVECAMPAIGN = "ActiveCampaign"
static String TYPE_API_ACTIVATE = "activate"
static String TYPE_ONTRAPORT = "ontraport"
}
def getStaticAttributeWithValue(Class clazz, Object searchedValue) {
clazz.declaredFields
.findAll{ isStatic(it.modifiers) }
.find { clazz[it.name] == searchedValue }
}
assert getStaticAttributeWithValue(Autoresponder, "AWeber") != null
assert getStaticAttributeWithValue(Autoresponder, "NonExist") == null
If the function returns null, there is no static field with that value, otherwise, it will be not null. (actually it will be a object of type java.lang.reflect.Field)
There is another way to fetch all static attributes in your class, that is using groovy MetaClass, but the idea is the same
def getStaticAttributeWithValue(Class clazz, Object searchedValue) {
clazz.metaClass.properties
.findAll{ it.getter.static }
.find { clazz[it.name] == searchedValue }
}
This way you will get a groovy.lang.MetaBeanProperty instead
The easiest way would be to use GrailsClassUtils.getStaticFieldValue to see if a Groovy class in Grails has a static property with a given value.
The above utility class has other methods that you may also find helpful.
Try to go with MetaClass. Something like (not tested... just an idea):
String val
if(Autoresponder.metaClass.static.AWeber){
val = Autoresponder.AWeber
}
With MetaClass you can also edit the methods and statics in there. It should also be faster than reflections
I'm new to Grails and study it via InfoQ's "Getting Started With Grails" book.
Running through it I've faced a problem with the BootStrap.groovy file:
I've edited it as the book says.
Run-app it - and the data I've entered into BootStrap.groovy has been shown.
I've updated the bootstrapping values but they are not changed.
No errors are shown in the console.
What I've tried:
exchanging def's places;
set Runner's class value to nullables;
changed .save(failOnError:true) to .save(flush:true).
Even after I've changed the Race class' bootstrapping values it looks like it hasn't been changed.
Looks like the value is taken from somewhere else than the BootStrap.groovy?
I use Grails 2.2.1 with a PostgreSQL DB.
The classes' code is below:
package racetrack
class Runner {
static constraints = {
firstName(blank:false)
lastName(blank:false)
dateOfBirth(nullable:true)
gender(inList:["M","F"])
address(nullable:true)
city(nullable:true)
state(nullable:true)
zipcode(nullable:true)
email(email:true)
}
static hasMany = [registrations:Registration]
/*static mapping = {
sort "email"
}*/
String firstName
String lastName
Date dateOfBirth
String gender
String address
String city
String state
String zipcode
String email
String toString(){
"${firstName} ${lastName} (${email})"
}
}
and
package racetrack
class BootStrap {
def init = { servletContext ->
def begun = new Runner(firstName:"Marathon",
lastName:"Runner",
dateOfBirth:"",
gender:"M",
address:"",
city:"",
state:"",
zipcode:"",
email:"me#me.ru"
)
begun.save(flush:true)
def zabeg = new Race(name:"Run SPB",
startDate:new Date() + 360*30,
city:"Moscow",
state:"Moscow",
cost:10.0,
distance:42.0,
maxRunners:10)
zabeg.save(flush:true)
}
def destroy = {
}
}
EDIT: could this be happening due to any generate-* scripts been run?
Race.groovy:
package racetrack
class Race {
static hasMany = [registrations:Registration]
String name
Date startDate
String city
String state
BigDecimal distance
BigDecimal cost
Integer maxRunners
static constraints = {
name(blank:false, maxSize:50)
startDate(validator: {
return (it >= new Date())
}
)
city()
state(inList:["Moscow", "Volgograd", "SPb", "NN"])
distance(min:0.0)
cost(min:0.0, max:100.0)
maxRunners(min:0, max:100000)
}
static mapping = {
sort "startDate"
}
BigDecimal inMiles(){
return distance*0.6214
}
String toString(){
return "${name}, ${startDate.format('MM/dd/yyyy')}"
}
}
BootStrap should be in grails-app/conf instead. Refer this.
Try to print the errors of the new instance you save by adding
print begun.errors
See if somethings comes up.
You need to remove
def index() { }
from the controllers.
I had the same issue. Now it is fixed.