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()
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 have 2 tables named 'employees' and 'dataentry'. I need to set the employee_id column in employees table as primary key, and also employee_id column in dataentry as a foreign key of employees.employee_id. How can I do this in groovy domain class.
here is my employees.groovy model class
package com.standout.utilityapplication
import java.util.Date;
import com.standout.utilityapplication.Dataentry
class Employees {
static mapping = {
table 'employees'
version false
}
static hasMany = [employee_id:Dataentry]
String employee_id
String employee_name
String team
Long contact_no
String designation
static constraints = {
employee_id(nullable: false,maxSize:10)
employee_name(nullable: false,maxSize:100)
team(nullable: true,maxSize:40)
contact_no(nullable: true,maxSize:10)
designation(nullable: true,maxSize:40)
}
}
and here is my dataentry.groovy model class
package com.standout.utilityapplication
import com.standout.utilityapplication.Employees
class Dataentry {
static mapping = {
table 'dataentry'
version false
}
static belongsTo = [employee_id:Employees]
String employee_id
String team
Date receipt_dt
String restaurant_name
int number_of_persons
float amount
Date bill_submitted_dt
String reimbursed
char presented_bank_fl
Date presented_bank_dt
String create_id
Date create_dt
String mod_id
Date mod_dt
static constraints = {
reimbursed(nullable: true)
presented_bank_fl(nullable: true)
presented_bank_dt(nullable: true)
mod_id(nullable: true)
mod_dt(nullable: true)
}
}
Please tell me how to make a mapping with cascaded
Something akin to this should get the entities joined. Convention is to name an entity in the singular, not a plural (Employee, not Employees):
class Employee {
static hasMany = [dataentries:Dataentry]
static mapping = {
id name:'employee_id'
}
}
class Dataentry {
static belongsTo = [employee:Employee]
}
Let the system take care of the details.
when I use the bellow code see the exception Found two representations of same collection.
how can fix this problem?
class ProductModel implements Serializable{
static auditable = true
String name
Product product
Guarantee guarantee
String status
Integer width
Integer height
Integer weight
static hasMany = [variationValues: VariationValue, prices: Price]
static composites = ["prices"]
static mapping = {
sort 'id'
prices cascade: "all-delete-orphan"
searchKeys type: 'text'
}
in ProductModelController I have this code for delete()
def delete() {
def pricemodel = Price.findAllByProductModel(productModelInstance).findAll()
pricemodel.each { price ->
productModelInstance.removeFromPrices(price)
}
productModelInstance.save()
pricemodel.each {
it.delete()
}
productModelInstance.delete(flush: true)
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.
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.