Pillai’s

Technology and Everything…..

Archive for the ‘Uncategorized’ Category

Connecting to Oracle 11g from Grails

Posted by Shibu on October 20, 2010

If you are connecting to Oracle 11g and getting Hibernate Dialect related error, try adding following in the DataSource.groovy file
dialect=’org.hibernate.dialect.Oracle10gDialect’.
You should add this in the datasource section and not in the hibernate section

Advertisements

Posted in Uncategorized | Tagged: , | Leave a Comment »

Grails + Oracle

Posted by Shibu on October 19, 2010

If you are wondering how to use Grails  (from windows) with Oracle,

  • Download Grails from Grails  website. I used version 1.3.5
  • Unzip to your local folder (c:\grails-1.3.5)
  • You have to detine environment variable  JAVA_HOME. Ponit JAVA_HOME to your JDK. I am using version 5.0
  • Include the bin directory of grails installation into your PATH variable. (set PATH=%PATH%;c:\grails-1.3.5\bin)
  • Test your installation. Open a comman prompt window and type grails. You should see like below

C:\Users\xx>grails
Welcome to Grails 1.3.5 – http://grails.org/
Licensed under Apache Standard License 2.0
Grails home is set to: C:\grails-1.3.5

No script name specified. Use ‘grails help’ for more info or ‘grails interactive’ to enter interactive mode
C:\Users\xx>

  • If you see any error message, fix them before proceeding to next step
  •  You can see following steps in Grails website
  • Create a new project using command, grails create-app my-project, from command prompt. You issue this comman from any custom directory you have created for this project
  • Configure Data Source. This is optional, but this where we have setup if we need to connect Oracle Database. Following example shows what I have done to connect to my Oracle Database. You need to modify the DataSource.groovy file like below.

             dataSource {
                    pooled = true
                    driverClassName = “oracle.jdbc.OracleDriver”
                    username = “<schema_name>”
                    password = “<schema_password>”
                  }
                  hibernate {
                       cache.use_second_level_cache = true
                       cache.use_query_cache = true
                       cache.provider_class = ‘net.sf.ehcache.hibernate.EhCacheProvider’
                   }
                 // environment specific settings
                   environments {
                  development {
                          dataSource {
                                       dbCreate = “create-drop” // one of ‘create’, ‘create-drop’,’update’
                                        url =”jdbc:oracle:thin:@<ServerName>:<Port>:<SID>”
                                  }
                   }
                   test {
                           dataSource {
                                        dbCreate = “update”
                                         url = “jdbc:oracle:thin:@<ServerName>:<Port>:<SID>”
                            }
                     }
                     production {
                                     dataSource {
                                            dbCreate = “update”
                                            url =”jdbc:oracle:thin:@<ServerName>:<Port>:<SID>”
                                    }
                      }
               }

You have to use appropriate server names, ports and SID names

  • Now you have to copy ojdbc14.jar to the lib directory of your Grails Application. This Oracel jar file you can get from any Oracle installation. For example if you have install SQLDeveloper, JDeveloper or Orcel XE you should be able to find this jar file within the subdirectory.
  • Now create domain class using command  grails create-domain-class book
  • The above command should create Book.groovy. Edit the Book.groovy file and add your attributes. This should match with your table field names. I added like below as suggested in Grails site,
class Book {
    String title
    String author
}
  • Create controller, grails create-controller my.project.Book
  • Above command should create file BookController.groovy. Modify this controller file like below as suggested in the Grails website
class BookController {
     def scaffold = Book // Note the capital "B"
}

 

 



Posted in Uncategorized | Leave a Comment »