Dominios e mais dominios
27/06/2012 02:47
package com.grailsinaction
class Post {
// mapstyle
// Posso chegar no Usuário atravez de um Post,
// tanto quando posso chegar em um Post atravez do
// usuário
static belongsTo = [usuario: Usuario]
static hasMany = [tags: Tag]
String conteudo
Date dataCriacao = new Date()
static constraints = {
conteudo(blank: false)
}
static mapping = {
sort dataCriacao:"desc"
}
}
package com.grailsinaction
class Profile {
// relação um para um ->> unidirecional
// Você pode chegar em um Profile via um Usuário, mas
// não o inverso --> relação 1:1
static belongsTo = Usuario
byte[] photo
String fullName
String bio
String homepage
String email
String timezone
String country
String jabberAddress
static constraints = {
fullName(nullable: true)
bio(nullable: true, maxSize: 1000)
email(email:true, nullable: true)
photo(nullable: true)
country(nullable: true)
timezone(nullable: true)
jabberAddress(email: true, nullable: true)
}
}
package com.grailsinaction
class Tag {
String name
Usuario usuario
static constraints = {
name(blank:false)
}
static belongsTo = [Usuario, Post]
static hasMany = [posts: Post]
}
package com.grailsinaction
class Usuario {
String usuarioId
String senha
String homepage
Date dataCriacao = new Date()
Profile profile
static constraints = {
usuarioId(maxsize: 20, size:3..20, unique: true)
senha(size:6..8,
validator: { senha_aux, usuario ->
return senha_aux != usuario.usuarioId }
)
homepage(url:true,nullable:true) // opcional com url patter
dataCriacao()
profile(nullable: true)
}
// relação 1:m
// Um usuário tem vários Posts
static hasMany = [posts: Post, tags: Tag]
static mapping = {
// Nesse caso sempre quando Grails carregar um usuário,
// ele vai carregar o profile junto, tambem podemos
// declara-lo eagerly (lazy é o que usamos)
profile (lazy:false) // Grails carrega profile com o usuario
// Permite ordenar os post quando acessarmos a collection posts
// atravez de um usuário
posts sort: 'dataCriacao'
}
}
Back references, i.e. properties linking back to the owner, can be added in one of two ways:
class Book {
Author author
static belongsTo = Author
}
Para se registrar, clique aqui.