I wrote a simple code to populate the Grail domain classes and a code for showing db contents, but it doesn’t work. Attached is my code! I appreciate any help.
def populateDB(int nofelements)
{
def instance
for (int i=1;i<=nofelements;i++){
instance=new Avendpoint()
instance.avName=org.apache.commons.lang.RandomStringUtils.random(9, true, true)
instance.bridge=org.apache.commons.lang.RandomStringUtils.random(9, true, true)
instance.callerID=org.apache.commons.lang.RandomStringUtils.random(9, true, true)
instance.con=false
instance.state=AvendpointState.ONE_WAY
instance.uid=org.apache.commons.lang.RandomStringUtils.random(5, true, true)
instance.save(flush: true)
}
render "The database has been populated successfully!"
}
def showDB(){
def instance
String res
res+=Integer.toString(Avendpoint.count())
for(int i=1; i<Avendpoint.count(); i++){
instance=Avendpoint.get(i)
res+=instance.avName+"<br>"+instance.bridge+"<br>"+instance.callerID+"<br>"+
instance.con+"<br>"+instance.state+"<br>"+instance.uid+"<br>"
}
render res
}
The first thing is try to use:
instance.save(flush: true,failOnError:true). By default Grails doesn’t throw an exception when domain doesn’t it’s fields constraints. Maybe instances are rejected while validation and will not be saved.