Search code examples
mysqlhibernategrails-ormmicronaut

How to configure application.yml for mysql using Gorm in micronaut


I am trying to make a CRUD application in micronaut and groovy, i am using hibernate and GORM for validation,save,delete etc with mySql database and @Transactional.

While i run the application it runs fine but there is no tables initialized in mysql. While sending the Post request to save the Data in db i get following error

5:54:48.840 [nioEventLoopGroup-1-3] ERROR i.m.h.s.netty.RoutingInBoundHandler - Unexpected error occurred: could not prepare statement org.hibernate.exception.SQLGrammarException: could not prepare statement

Caused by: org.h2.jdbc.JdbcSQLException: Table "STUDENT" not found; SQL statement: select this_.id as y0_ from student this_ where this_.name=? limit ? [42102-196]

My domain class code is:

@Entity
class Student {
   String name
   int age
   String gender
   String number

   static constraints = {
       name nullable:false, blank:false,unique:true
       age nullable:true,blank:true,size: 1..5
       gender nullable:true,blank:true
       number size: 11..13,blank:true,unique:true,nullable:true
   }
}

The code of my Controller is as follow:

@Transactional(readOnly = true)
@Controller("/")
class StudntController {

   @Post("/save")
   @Transactional
   def save(@Body Object JSON)
   {
     def result = [:]

     Student stu = new Student(name: 
      JSON?.name,gender:JSON?.gender)

     stu.save(flush: true,failOnError: true)

     result.put("Student",stu)
     result.put("Message", "Student created successfully")
   }
}

Here is my Application.yml configurations:

micronaut:
 application:
    name: mincronaut-crud

 server:
        port: 8080
 ---

 hibernate:
    cache:
       queries: false
       use_second_level_cache: false
       use_query_cache: false


 environments:
    development:
       dataSource:
           dbCreate: update
           pooled: true
           jmxExport: true
           driverClassName: com.mysql.jdbc.Driver
           username: admin
           password: qwerzxcv123
           dialect: org.hibernate.dialect.MySQL5InnoDBDialect
           url: jdbc:mysql://192.168.1.121:3306/student
    test:
        dataSource:
           dbCreate: update
           url: jdbc:mysql://192.168.1.121:3306/student
    production:
        dataSource:
           dbCreate: update
           url: jdbc:mysql://192.168.1.121:3306/student

And atlast here are my build.gradle configurations:

 plugins {
   id "io.spring.dependency-management" version "1.0.6.RELEASE"
   id "com.github.johnrengelman.shadow" version "4.0.2"
   }

   apply plugin:"application"
   apply plugin:"groovy"

   version "0.1"
   group "mincronaut.crud"

   repositories {
     mavenLocal()
     mavenCentral()
     maven { url "https://jcenter.bintray.com" }
   }

   ext {
      gormVersion = '6.1.9.RELEASE'
      h2Version = '1.4.196'
      tomcatJdbcVersion = '8.5.28'
   }


  dependencyManagement {
   imports {
      mavenBom 'io.micronaut:micronaut-bom:1.0.1'
     }
    }

   dependencies {
     compile "io.micronaut:micronaut-http-client"
     compile "io.micronaut:micronaut-http-server-netty"
     compile "io.micronaut:micronaut-runtime-groovy"
     compile "io.micronaut:micronaut-validation"
     compile "io.micronaut.configuration:micronaut-hibernate-gorm"
     compileOnly "io.micronaut:micronaut-inject-groovy"
     compile "org.grails:grails-datastore-gorm- 
     hibernate5:$gormVersion"
     runtime "org.apache.tomcat:tomcat-jdbc:$tomcatJdbcVersion"
     runtime 'mysql:mysql-connector-java:6.0.6'
     runtime "ch.qos.logback:logback-classic:1.2.3"
     runtime "com.h2database:h2:$h2Version"
     testCompile "io.micronaut:micronaut-inject-groovy"
     testCompile("org.spockframework:spock-core") {
       exclude group: "org.codehaus.groovy", module: "groovy-all"
      }
     testCompile "junit:junit:4.12"
     testCompile "io.micronaut:micronaut-inject-java"
     testCompile "org.hamcrest:hamcrest-all:1.3"
     }

     shadowJar {
         mergeServiceFiles()
     }

     run.jvmArgs('-noverify', '-XX:TieredStopAtLevel=1')

     mainClassName = "mincronaut.crud.Application"
     tasks.withType(GroovyCompile) {
     groovyOptions.forkOptions.jvmArgs.add('- 
       Dgroovy.parameters=true')
   }

Why the code after run doesnt create any table in database and why it is picking the h2 database even when my url in application is configured for mysql and i've also provided mysql plugin in build.gradle. Please help me out to configure mysql properly with gorm and groovy in micronaut.


Solution

  • I have just made this working CURD with MySQL. you can refer this post:

    Micronaut MySQL delete record org.hibernate.HibernateException: No Session found for current thread