Fórum Meu Join não funciona usando HQL #400566
08/05/2011
0
Query query = getEntityManager().createQuery(" from Escola esc ,Matricula mat, Professor prof " +
" where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and" +
" mat.situacao = 'A' and idprofessor = 2 order by esc.nome"); Como devo fazer para que funcione? O erro abaixo
exception javax.servlet.ServletException: org.hibernate.QueryException: could not resolve property: idprofessor of: br.com.mosaicodigital.modelo.Professor [ from br.com.mosaicodigital.modelo.Escola esc ,br.com.mosaicodigital.modelo.Matricula mat, br.com.mosaicodigital.modelo.Professor prof where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and mat.situacao = 'A' and idprofessor = 2 order by esc.nome] root cause java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: idprofessor of: br.com.mosaicodigital.modelo.Professor [ from br.com.mosaicodigital.modelo.Escola esc ,br.com.mosaicodigital.modelo.Matricula mat, br.com.mosaicodigital.modelo.Professor prof where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and mat.situacao = 'A' and idprofessor = 2 order by esc.nome] root cause org.hibernate.QueryException: could not resolve property: idprofessor of: br.com.mosaicodigital.modelo.Professor [ from br.com.mosaicodigital.modelo.Escola esc ,br.com.mosaicodigital.modelo.Matricula mat, br.com.mosaicodigital.modelo.Professor prof where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and mat.situacao = 'A' and idprofessor = 2 order by esc.nome] note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 3.0.1 logs.
Pjava
Curtir tópico
+ 0Posts
08/05/2011
Davi Costa
Posta ele p gente ver.
Att Davi
Gostei + 0
08/05/2011
Robson Teixeira
att
robson
Gostei + 0
08/05/2011
Pjava
@Entity
@Table(name="professor")
@SequenceGenerator(name="seq_professor", sequenceName="professor_idprofessor_seq", allocationSize=1)
public class Professor extends Pessoa implements Serializable {
private static final long serialVersionUID = 8683441216699864995L;
//@Column(name="idprofessor")
private int idProfessor;
//@Column(name="foto")
private String foto;
private Pessoa pessoa;
public int getIdProfessor() {
return idProfessor;
}
public void setIdProfessor(int idProfessor) {
this.idProfessor = idProfessor;
}
public String getFoto() {
return foto;
}
public void setFoto(String foto) {
this.foto = foto;
}
public Pessoa getPessoa() {
return pessoa;
}
public void setPessoa(Pessoa pessoa) {
this.pessoa = pessoa;
}
@Override
public int hashCode() {
final int prime = 31;
int result = super.hashCode();
result = prime * result + ((foto == null) ? 0 : foto.hashCode());
result = prime * result + idProfessor;
result = prime * result + ((pessoa == null) ? 0 : pessoa.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!super.equals(obj))
return false;
if (getClass() != obj.getClass())
return false;
Professor other = (Professor) obj;
if (foto == null) {
if (other.foto != null)
return false;
} else if (!foto.equals(other.foto))
return false;
if (idProfessor != other.idProfessor)
return false;
if (pessoa == null) {
if (other.pessoa != null)
return false;
} else if (!pessoa.equals(other.pessoa))
return false;
return true;
}
}Gostei + 0
08/05/2011
Pjava
javax.servlet.ServletException: org.hibernate.exception.SQLGrammarException: could not execute query org.postgresql.util.PSQLException: ERRO: coluna professor2_.tipopessoa não existe Posição: 662
Esse é meu select em HQL.
public List<Escola> findAll() {
Query query = getEntityManager().createQuery(" from Escola esc ,Matricula mat, Professor prof " +
" where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and" +
" mat.situacao = 'A' and mat.idprofessor = 2 order by esc.nome");
return (List<Escola>) query.getResultList();
}Gostei + 0
08/05/2011
Pjava
org.postgresql.util.PSQLException: ERRO: coluna professor2_.idprofessor não existe Posição: 692
Realmente eu não estou entendendo mais nada. A coluna idprofessor ela existe em várias entidades, várias mesmo,
pois ele é o cara principal no projeto, o professor.
Gostei + 0
08/05/2011
Pjava
Gostei + 0
09/05/2011
Davi Costa
@Column(name="idprofessor")
private int idProfessor;
É esperado que no banco a coluna esteja com esse nome: idprofessor,
e no hql vc esqueci banco e o que está entre parênteses e coloca idProfessor, o HQL já faz o meio de campo para vc. Não é problema de join mas sim de coluna, vc está trabalhando com java e um framework ORM, não é uma chamada comum no banco.
Seu hql depois de corrigir essa parte ainda vai dar outro erro:
Query query = getEntityManager().createQuery(" from Escola esc ,Matricula mat, Professor prof " +
" where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and" +
" mat.situacao = 'A' and mat.idprofessor = 2 order by esc.nome");
return (List<Escola>) query.getResultList();
Vc não especificou qual tabela retornaria a consulta, qdo fosse fazer o cast no return tb talvez dê erro: que tal colocar :
Query query = getEntityManager().createQuery("select esc from Escola esc ,Matricula mat, Professor prof " +
" where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and" +
" mat.situacao = 'A' and mat.idprofessor = 2 order by esc.nome");
return (List<Escola>) query.getResultList();
Att Davi
Gostei + 0
09/05/2011
Pjava
org.postgresql.util.PSQLException: ERRO: coluna professor2_.dtype não existe Posição: 218
Eu não sei mesmo onde esse dtype aparece. Pesquisei nos meus beans, em todas as classes e nada consegui
resolver isso. Não sei de onde ele aparece. Se retiro o Join aí funciona. To pesquisando, mas não sei de onde veio esse ara.
Query query = getEntityManager().createQuery("select esc from Escola esc ,Matricula mat, Professor prof " +
" where mat.idescola = esc.idescola and mat.idprofessor = prof.idprofessor and" +
" mat.situacao = 'A' and mat.idprofessor = 2 order by esc.nome");
return (List<Escola>) query.getResultList();
Gostei + 0
09/05/2011
Davi Costa
att Davi
Gostei + 0
09/05/2011
Pjava
@Entity
@Table(name="pessoa")
@SequenceGenerator(name="seq_pessoa", sequenceName="pessoa_idpessoa_seq", allocationSize=1)
public class Pessoa implements Serializable {
private static final long serialVersionUID = -7427597435054021998L;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_pessoa")
@Column(name="idPessoa")
private int idPessoa;
@Column(name="nome")
private String nome;
@Column(name="sobrenome")
private String sobrenome;
@Column(name="sexo")
private EnumSexo sexo;
@Column(name="cpf")
private String cpf;
@Column(name="telefoneComercial")
private String telefoneComercial;
@Column(name="telefoneResidencial")
private String telefoneResidencial;
@Column(name="email")
private String email;
@Column(name="tipo")
private EnumTipoPessoa tipo;
public int getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(int idPessoa) {
this.idPessoa = idPessoa;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getSobrenome() {
return sobrenome;
}
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
public EnumSexo getSexo() {
return sexo;
}
public void setSexo(EnumSexo sexo) {
this.sexo = sexo;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getTelefoneComercial() {
return telefoneComercial;
}
public void setTelefoneComercial(String telefoneComercial) {
this.telefoneComercial = telefoneComercial;
}
public String getTelefoneResidencial() {
return telefoneResidencial;
}
public void setTelefoneResidencial(String telefoneResidencial) {
this.telefoneResidencial = telefoneResidencial;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public EnumTipoPessoa getTipoPessoa() {
return tipo;
}
public void setTipoPessoa(EnumTipoPessoa tipo) {
this.tipo = tipo;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cpf == null) ? 0 : cpf.hashCode());
result = prime * result + ((email == null) ? 0 : email.hashCode());
result = prime * result + idPessoa;
result = prime * result + ((nome == null) ? 0 : nome.hashCode());
result = prime * result + ((sexo == null) ? 0 : sexo.hashCode());
result = prime * result
+ ((sobrenome == null) ? 0 : sobrenome.hashCode());
result = prime
* result
+ ((telefoneComercial == null) ? 0 : telefoneComercial
.hashCode());
result = prime
* result
+ ((telefoneResidencial == null) ? 0 : telefoneResidencial
.hashCode());
result = prime * result
+ ((tipo == null) ? 0 : tipo.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pessoa other = (Pessoa) obj;
if (cpf == null) {
if (other.cpf != null)
return false;
} else if (!cpf.equals(other.cpf))
return false;
if (email == null) {
if (other.email != null)
return false;
} else if (!email.equals(other.email))
return false;
if (idPessoa != other.idPessoa)
return false;
if (nome == null) {
if (other.nome != null)
return false;
} else if (!nome.equals(other.nome))
return false;
if (sexo != other.sexo)
return false;
if (sobrenome == null) {
if (other.sobrenome != null)
return false;
} else if (!sobrenome.equals(other.sobrenome))
return false;
if (telefoneComercial == null) {
if (other.telefoneComercial != null)
return false;
} else if (!telefoneComercial.equals(other.telefoneComercial))
return false;
if (telefoneResidencial == null) {
if (other.telefoneResidencial != null)
return false;
} else if (!telefoneResidencial.equals(other.telefoneResidencial))
return false;
if (tipo != other.tipo)
return false;
return true;
}
}Gostei + 0
09/05/2011
Davi Costa
bom mais alguns toques,
esse serial que vc gera por conta de impleemntar Serializable, vc não cria getters e setters para ele não.
para não ter problema e o hibernate pensar que vc precisa incluí-lo no banco.
No equals e hashCode será que não seria interessante vc usar só atributo id?
Não é impossível termos situações que passemos outros atributos não, mas lembre vc tem realmente um porquê fazer isso?
Com somente o id vai ser mais performático.
Se vc quer que a coluna seja unique, tem umaforma de fazer isso:
@Table(name = "pessoa", uniqueConstraints={@javax.persistence.UniqueConstraint(columnNames={"cpf","email","nome"})})
Sexo não dá pq ai vc só vai consegui cadastrar duas pessoas no banco.
Cara posta agora seu arquivo ou classe de configuração do hibernate para ver se no console está mostarndo a consulta gerada pelo hibernate, se já estiver mostrando a consulta, posta aki a consula para verificarmos se:
professor2_.dtype é referente a tabela professor mesmo, as vezes nasconsultas o hibernate cria uns nomes meio estranho para tabelas..
Att Davi
Gostei + 0
09/05/2011
Pjava
<persistence>
<persistence-unit name="puMosaicoDigital">
<properties>
<property name="hibernate.archive.autodetection" value="class, hbm" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver" />
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5435/Teste" />
<property name="hibernate.connection.username" value="postgres" />
<property name="hibernate.connection.password" value="123456" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
A porta é a 5435 mesmo. Isso tá correto.
Gostei + 0
09/05/2011
Pjava
Gostei + 0
09/05/2011
Pjava
INFO: Hibernate: select escola0_.idescola as idescola2_, escola0_.nome as nome2_, escola0_.telefone as telefone2_, escola0_.fax as fax2_, escola0_.email as email2_ from escola escola0_, matricula matricula1_, pessoa professor2_ where professor2_.DTYPE='Professor' and matricula1_.idprofessor=professor2_.idprofessor and matricula1_.idescola=escola0_.idescola and matricula1_.situacao='A' and idprofessor=1 order by escola0_.nome
Gostei + 0
09/05/2011
Davi Costa
Vê isso nakele material que te passei por email, lá fala das estratégias de herança.
Att Davi
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)