I am using oracle11g as the database, and trying to do inserts into globalusers table from my grails service class.
I am retrieving the datasource in grails service like this:
import org.codehaus.groovy.grails.commons.ApplicationHolder as AH
class UserImportService {
def dataSource = AH.application.mainContext.dataSource
def sql = new Sql(dataSource) ;
String insertQuery="insert into GLOBALUSERS (..) values (..)
try{
sql.execute(insertQuery)
}
catch(Exception e){
println "Failed to insert : " +insertQuery
println "Exception is:" + e;
}
}
When I run the service from front end I get sql exception saying
Exception is:java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("GRA"."GLOBALUSERS"."ID")
Since I am using a Datasource with my custom dialect which handles ID generation and autoincrement this should be handled by grail/hibernate.
When use the same datasource from other UI .. one by one.. it works..so the dialect is working fine. but this service for batch updates is not working.
Doing direct Sql on the
dataSourcebean is going to bypass Hibernate’s method of id generation, which, as I understand it, does anextval()on the appropriate Oracle sequence. So your options are:.save()nextval()on the sequence, but it would make your Sql Oracle-specificnextval()sequence query and adds it to the new database row. This will keep your direct Sql compatible with MySql autoincrement and Oracle sequences.