Classe com campo tipo list
01/08/2010 00:00
Bom dia
Estou com o seguinte problema criei uma classe Usuario e uma classe Cliente, em Cliente fiz uma relação de hasMany ou seja
cada cliente pode ter vários contatos e contatos e do tipo usuario. Como faço para pesquisa a empresa de um contato, segue abaixo as classes criadas, o banco que estou usando é o Derby.
class Cliente {
String empresa
String endereco
Cidade cidade
Estado estado
String cep
String fone
String cnpj
TipoCli tipo
Boolean ativo
Boolean contrato
String toString(){
return "$empresa"
}
static hasMany = [contatos:Usuario]
static belogTo = [tipocli:TipoCli]
static constraints = {
empresa(size:0..100,blank:false,nullable:false,unique:true)
endereco(size:0..200)
cidade(nullable:false)
estado(nullable:false)
cep(size:0..10)
cnpj(size:0..15)
fone(size:0..15)
tipo(blank:false,nullable:false)
contrato(nullable:false)
ativo(nullable:false)
}
static Empresa(){
def session = RequestContextHolder.currentRequestAttributes().getSession()
def b = Usuario.findByNome(session.usuario.toString())
if(session.tipo == 'Cliente'){
Cliente.findAllWhere(contatos:b)
}else{
Cliente.list()
}
}
}
class Usuario {
String nome
String usuario
TipoUsu tipo
String senha
String hashsenha
String email
String toString(){
this.nome
}
void setSenha(String valor){
this.senha = valor
this.hashsenha = valor.encodeAsSenha()
}
static transients = ['senha']
static belogTo = TipoUsu
static constraints = {
nome(blank:false,nullable:false)
usuario(blank:false,nullable:false,size:1..10,unique:true)
senha(blank:false,nullable:false)
email(blank:false,nullable:false,email:true,unique:true)
}
static Cliente(){
def b = TipoUsu.findByCategoria("Cliente")
this.findAllWhere(tipo:b)
}
}
Tags:
Grails