fabioissamu 10 de jan. de 2008
não seria melhor mapear o POJO?
cliente.getTelefones()
onde getTelefones é uma List de telefones?
fabioissamu 10 de jan. de 2008
Link com exemplos de relacionamentos com annotations:
http://www.hibernate.org/hib_docs/annotations/reference/en/html/entity.html#entity-mapping-association
Acho que o q vc precisa é "OneToMany"
Um cliente que tem vários telefones.
paulofafism 10 de jan. de 2008
As classes ja estão todas mapeadas
Classe TelefoneCliente mapeada
<hibernate-mapping package= "oberon.dominio" >
<class name= "TelefoneCliente" table= "TelefoneCliente" >
<id name= "codtelefone" column= "CodTelefone" >
<generator class= "sequence" >
<param name= "sequence" > codtelefone_gen</param>
</generator>
</id>
<property name= "ddd" column= "ddd" type= "string" length= "5" />
<property name= "telefone" column= "telefone" type= "string" length= "15" />
<property name= "ramal" column= "ramal" type= "string" length= "5" />
<property name= "tipo" column= "tipo" type= "string" length= "15" />
<!--Relacionamentos-->
<many-to-one
name= "codCliente"
column= "CODCLIENTE"
class= "Cliente"
not-null= "true"
foreign-key= "FK_CLIENTE"
fetch= "join" />
</class>
</hibernate-mapping>
Classe Cliente mapeada
<hibernate-mapping package= "oberon.dominio" >
<class name= "Cliente" table= "Cliente" >
<id name= "codcliente" column= "CodCliente" >
<generator class= "sequence" >
<param name= "sequence" > codcliente_gen</param>
</generator>
</id>
<property name= "nome" column= "NOME" type= "string" length= "100" not-null= "false" />
<property name= "endereco" column= "ENDERECO" type= "string" length= "100" />
<property name= "tipoPessoa" column= "TIPOPESSOA" type= "integer" length= "1" />
<property name= "cep" column= "CEP" type= "string" length= "9" />
<property name= "numero" column= "NUMERO" type= "string" length= "6" />
<property name= "bairro" column= "BAIRRO" type= "string" length= "60" />
<property name= "complemento" column= "COMPLEMENTO" type= "string" length= "60" />
<property name= "cidade" column= "CIDADE" type= "string" length= "60" />
<property name= "uf" column= "uf" type= "string" length= "2" />
<property name= "datanascimento" column= "datanascimento" type= "date" />
<property name= "datacadastro" column= "datacadastro" type= "date" />
<property name= "horacadastro" column= "horacadastro" type= "timestamp" />
<property name= "status" column= "status" type= "string" length= "60" />
<property name= "cpfCnpj" column= "cpfcnpj" type= "string" length= "18" />
<property name= "rgIe" column= "rgie" type= "string" length= "18" />
<property name= "inscMunicipal" column= "inscmunicipal" type= "string" length= "18" />
<!--Relacionamentos-->
<set name= "telefoneCliente" inverse= "true"
order-by= "codtelefone asc"
cascade= "all-delete-orphan" lazy= "true" table= "TelefoneCliente"
>
<key column= "CodTelefone" not-null= "true" />
<composite-element class= "TelefoneCliente" >
<property name= "ddd" column= "ddd" type= "string" />
<property name= "telefone" column= "telefone" type= "string" />
<property name= "ramal" column= "ramal" type= "string" />
<property name= "tipo" column= "tipo" type= "string" />
<many-to-one name= "codCliente"
column= "CodCliente"
class= "Cliente"
not-null= "true"
foreign-key= "FK_CLIENTE"
/>
</composite-element>
</set>
<set name= "referenciaCliente" inverse= "true"
order-by= "codreferencia asc"
cascade= "all-delete-orphan" lazy= "true" table= "ReferenciaCliente" >
<key column= "CodReferencia" not-null= "true" />
<composite-element class= "ReferenciaCliente" >
<property name= "nome" column= "nome" type= "string" />
<property name= "telefone" column= "telefone" type= "string" />
<many-to-one name= "codCliente"
column= "CodCliente"
class= "Cliente"
not-null= "true" />
</composite-element>
</set>
</class>
</hibernate-mapping>
acdesouza 10 de jan. de 2008
paulofafism:
[…]
sql.append("SELECT T.*, C.NOME FROM CLIENTE C INNER JOIN TELEFONECLIENTE T");
sql.append("ON C.CODCLIENTE = T.CODCLIENTE");
sql.append("Where C.NOME LIKE '" + edtCampoPesquisa.getText + "%' ");
[…]
Não está dando erro, não? Estou perguntando pq a string que será gerada vai ficar assim:
SELECT T.*, C.NOME FROM CLIENTE C INNER JOIN TELEFONECLIENTE TON C.CODCLIENTE = T.CODCLIENTEWhere C.NOME LIKE
Coloca um espaço no fim de cada linha e dá uma olhada se funciona.
sql.append("SELECT T.*, C.NOME FROM CLIENTE C INNER JOIN TELEFONECLIENTE T ");
sql.append("ON C.CODCLIENTE = T.CODCLIENTE ");
sql.append("Where C.NOME LIKE '" + edtCampoPesquisa.getText + "%' ");
paulofafism 10 de jan. de 2008
Tb não obtive sucesso dessa forma. So consigo retornar os dados quando coloco
a seguinte consulta
"from TelefoneCliente where codcliente = " + edtCampoPesquisa.getText()
fabioissamu 10 de jan. de 2008
vc está tentando colocar no TelefoneCliente
List<TelefoneCliente> list = q.list();
coisas do cliente, não irá funcionar.
Selecione os clientes
"from Cliente where codcliente = " + edtCampoPesquisa.getText()// ou nome
e faça a lista só dos clientes
List<Cliente> list = q.list();
depois vc acessa a propriedade de telefone dos clientes
for ( Cliente cliente : list ) {
for(TelefoneCliente tel : cliente . getTelefoneCliente ()) {
tel . getNumero ();
}
}
fabioissamu 10 de jan. de 2008
Lógico que estou falando a forma mais simples
segue um material de HQL
http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html
fabioissamu 10 de jan. de 2008
Queries may return multiple objects and/or properties as an array of type Object[],
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
No caso lá de cima vc está retornando uma lista de array de objetos.