MONDAY, 17 SEPTEMBER 2012
HI THIS TOUTORIAL WILL HELP FULL FOR STARTUP IN GRAILS CREATED BY NAVEENRAJU
Parts
Part8: Ternery RelationShip in sql by using Grails views
Part9:Gsp Tags And there Implimentation in gsp Pages
PART4: Build Relation In My Sql(one to one,one to many)
Step1: grails create-app Relations
Step2:grails create-domain-class Book
Step3:Code like this in folloing path
C:/Relations/grails-app/domain/Relations/Book.groovy
package relations
class Book {
String title;
String feedback;
Date dateCreated;
Date lastUpdated;
User user;
static hasMany=[author:Author];
Author selectAuthor;
static mapping={
//------------------ one to one relation ship --------------
table 'book'
title coloumn:'bookTitle'
feedback coloumn:'feedBack'
dateCreated coloumn:'date'
lastUpdated coloumn:'dateUpdated'
//--------------------one to many relationship------------------
user coloumn:'Book_user_id'
author joinTable:[name:'book_author',
key:'Book_Id',
coloumn:'author_id']
//--------------------Many to Many relation ship---------------
author coloumn:'Group_author_id',joinTable:'Book_Group_Associations'
}
static constraints = {
title(blank:false,nullable:false);
feedback(blank:false,nullable:false);
dateCreated(nullabnle:false);
lastUpdated(nullable:true);
author(nullable:true,blank:true);
user(nullable:true,bank:true);
}
String toString()
{
return title;
}
}
Step4:grails create-controller Book
Step5:code like this in folloing path
C:/Relations/grails-app/controller/Relations/Book.groovy
package relations
class BookController {
static scaffold=Book
}
Step6:grails create-domain-class Author
Step7:Code like this in folloing path
C:/Relations/grails-app/domain/Relations/Author.groovy
package relations
class Author {
String authorName;
int age;
String country;
String webPage;
User user;
static belongsTo=[book:Book];
static constraints = {
authorName(blank:false);
age(blank:false);
country(blank:false);
webPage(blank:false,nullable:true);
book(blank:true,nullable:true);
user(blank:true,nullable:true);
}
static mapping={
//------------------------One To One--------------------------------
table 'author'
authorName coloumn:'authorName'
age coloumn:'age'
country coloumn:'country'
webPage coloumn:'web'
//-----------------------one To Many-----------------------------
book coloumn:'Group_book_id',joinTable:'Book_Group_Associations'
}
String toString()
{
return authorName
}
}
Step8:grails create-controller Book
Step9:code like this in folloing path
C:/Relations/grails-app/controller/Relations/Author.groovy
package relations
class AuthorController {
static scaffold=Author
}
Step10:grails create-domain-class User
Step11:Code like this in folloing path
C:/Relations/grails-app/domain/Relations/User.groovy
package relations
class User {
String book_user_name
static mapping={
table 'User'
book_user_name coloumn:'userName'
}
static constraints = {
book_user_name(blank:true,nullable:true);
}
String toString()
{
return book_user_name
}
}
Step12:grails create-controller Book
Step13:code like this in folloing path
C:/Relations/grails-app/controller/Relations/User.groovy
package relations
class UserController {
static scaffold=User
}
Step14:go to folloing path
C:/Relations/grails-app/conf/BuildConfig.groovy
Uncomment on line 37
ie:runtime 'mysql:mysql-connector-java:5.1.20'
grails.servlet.version = "2.5" // Change depending on target container compliance (2.5 or 3.0)
grails.project.class.dir = "target/classes"
grails.project.test.class.dir = "target/test-classes"
grails.project.test.reports.dir = "target/test-reports"
grails.project.target.level = 1.6
grails.project.source.level = 1.6
//grails.project.war.file = "target/${appName}-${appVersion}.war"
grails.project.dependency.resolution = {
// inherit Grails' default dependencies
inherits("global") {
// specify dependency exclusions here; for example, uncomment this to disable ehcache:
// excludes 'ehcache'
}
log "error" // log level of Ivy resolver, either 'error', 'warn', 'info', 'debug' or 'verbose'
checksums true // Whether to verify checksums on resolve
repositories {
inherits true // Whether to inherit repository definitions from plugins
grailsPlugins()
grailsHome()
grailsCentral()
mavenLocal()
mavenCentral()
// uncomment these (or add new ones) to enable remote dependency resolution from public Maven repositories
//mavenRepo "http://snapshots.repository.codehaus.org"
//mavenRepo "http://repository.codehaus.org"
//mavenRepo "http://download.java.net/maven/2/"
//mavenRepo "http://repository.jboss.com/maven2/"
}
dependencies {
// specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes
runtime 'mysql:mysql-connector-java:5.1.20'
}
plugins {
runtime ":hibernate:$grailsVersion"
runtime ":jquery:1.7.2"
runtime ":resources:1.1.6"
// Uncomment these (or add new ones) to enable additional resources capabilities
//runtime ":zipped-resources:1.0"
//runtime ":cached-resources:1.0"
//runtime ":yui-minify-resources:0.1.4"
build ":tomcat:$grailsVersion"
runtime ":database-migration:1.1"
compile ':cache:1.0.0'
}
}
Step15: open folloing link and set configrations like this
C:/Relations/grails-app/conf/DataSource.groovy
dataSource {
pooled = true
driverClassName = "com.mysql.jdbc.Driver"
username = "root"
password = ""
}
hibernate {
cache.use_second_level_cache = true
cache.use_query_cache = false
cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory'
}
// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop', 'update', 'validate', ''
url = "jdbc:mysql://localhost/Relation"
}
}
test {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/Relation"
}
}
production {
dataSource {
dbCreate = "update"
url = "jdbc:mysql://localhost/Relation"
pooled = true
properties {
maxActive = -1
minEvictableIdleTimeMillis=1800000
timeBetweenEvictionRunsMillis=1800000
numTestsPerEvictionRun=3
testOnBorrow=true
testWhileIdle=true
testOnReturn=true
validationQuery="SELECT 1"
}
}
}
}
Step16:Open mysql DataBase and create Database as "Relation"
step17:go to cmnd prompt and run the Application
grais run-app
| Server running. Browse to http://localhost:8080/Relations
After running in data base it will create 3 tables like
Step18:Browose to folloing path
http://localhost:8080/Relations
Running Steps:

No comments:
Post a Comment