Olá!
Sou Iniciante em Java WEB e Hibernate, é estou com uma duvida sobre Hibernate...
Bom a minha duvida é a seguinte,estou construindo um sistema Web, e estou querendo consultar no Banco de Dados, informações utilizando HQL do Hibernate..dai eu num sei onde coloco os metodos HQL???(não sei se coloco no Servelets, ou se coloco no DAO e crio um metodo)...
Para Facilitar, vou inserir minhas classes:
Classe Pessoa com Mapeamento Hibernatepackage br.com.miguelavaltroni;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
/**
*
* @author Avaltroni
*/
@Entity
@Table(name="pessoa")
public class pessoa implements Serializable {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="idPessoa",unique=true)
private String idPessoa=null;
@Id
@Column(name="cpf",unique=true, nullable=false, length=11)
private String cpf=null;
@Column(name="rg", unique=true, nullable=false)
private String rg=null;
@Column(name="nome", length=55, nullable=false)
private String nome=null;
@Column(name="sobrenome", length=80, nullable=false)
private String sobrenome=null;
@Column(name="sexo")
private String sexo=null;
@Column(name="endereco" ,length=100)
private String endereco=null;
@Column(name="bairro" ,length=50)
private String bairro=null;
@Column(name="cidade", length=50)
private String cidade=null;
@Column(name="uf", length=4)
private String uf=null;
@Column(name="cep", length=25)
private String cep=null;
@Temporal(javax.persistence.TemporalType.DATE)
@Column(name="data_nacimento")
private Date data_nacimento=null;
@Column(name="estado_civil")
private String estado_civil=null;
@Column(name="nacionalidade")
private String nacionalidade=null;
@Column(name="fone")
private String fone=null;
@Column(name="celular")
private String celular=null;
@Column(name="fone_comercial")
private String fone_comercial=null;
@Column(name="email")
private String email=null;
@Column(name="situacao")
private String situacao=null;
@Column(name="crm", unique=true, nullable=false)
private String crm=null;
@Column(name="blog")
private String blog=null;
public pessoa(){}
public pessoa(String idPessoa,String cpf, String rg, String nome, String sobrenome, String sexo,String endereco,
String bairro, String cidade, String uf, String cep, Date data_nacimento, String estado_civil,
String nacionalidade, String fone, String celular, String fone_comercial, String email,
String situacao, String crm, String blog){
this.idPessoa=idPessoa;
this.cpf=cpf;
this.rg=rg;
this.nome=nome;
this.sobrenome=sobrenome;
this.sexo=sexo;
this.endereco=endereco;
this.bairro=bairro;
this.cidade=cidade;
this.uf=uf;
this.cep=cep;
this.data_nacimento=data_nacimento;
this.estado_civil=estado_civil;
this.nacionalidade=nacionalidade;
this.fone=fone;
this.celular=celular;
this.fone_comercial=fone_comercial;
this.email=email;
this.situacao=situacao;
this.crm=crm;
this.blog=blog;
}
public String getidPessoa(){
return idPessoa;
}
public void setidPessoa(String idPessoa){
this.idPessoa= idPessoa;
}
/**
* @return the cpf
*/
public String getCpf() {
return cpf;
}
/**
* @param cpf the cpf to set
*/
public void setCpf(String cpf) {
this.cpf = cpf;
}
/**
* @return the rg
*/
public String getRg() {
return rg;
}
/**
* @param rg the rg to set
*/
public void setRg(String rg) {
this.rg = rg;
}
/**
* @return the nome
*/
public String getNome() {
return nome;
}
/**
* @param nome the nome to set
*/
public void setNome(String nome) {
this.nome = nome;
}
/**
* @return the sobrenome
*/
public String getSobrenome() {
return sobrenome;
}
/**
* @param sobrenome the sobrenome to set
*/
public void setSobrenome(String sobrenome) {
this.sobrenome = sobrenome;
}
/**
* @return the sexo
*/
public String getSexo() {
return sexo;
}
/**
* @param sexo the sexo to set
*/
public void setSexo(String sexo) {
this.sexo = sexo;
}
/**
* @return the endereco
*/
public String getEndereco() {
return endereco;
}
/**
* @param endereco the endereco to set
*/
public void setEndereco(String endereco) {
this.endereco = endereco;
}
/**
* @return the bairro
*/
public String getBairro() {
return bairro;
}
/**
* @param bairro the bairro to set
*/
public void setBairro(String bairro) {
this.bairro = bairro;
}
/**
* @return the cidade
*/
public String getCidade() {
return cidade;
}
/**
* @param cidade the cidade to set
*/
public void setCidade(String cidade) {
this.cidade = cidade;
}
/**
* @return the uf
*/
public String getUf() {
return uf;
}
/**
* @param uf the uf to set
*/
public void setUf(String uf) {
this.uf = uf;
}
/**
* @return the cep
*/
public String getCep() {
return cep;
}
/**
* @param cep the cep to set
*/
public void setCep(String cep) {
this.cep = cep;
}
/**
* @return the data_nacimento
*/
public Date getData_nacimento() {
return data_nacimento;
}
/**
* @param data_nacimento the data_nacimento to set
*/
public void setData_nacimento(Date data_nacimento) {
this.data_nacimento = data_nacimento;
}
/**
* @return the estado_civil
*/
public String getEstado_civil() {
return estado_civil;
}
/**
* @param estado_civil the estado_civil to set
*/
public void setEstado_civil(String estado_civil) {
this.estado_civil = estado_civil;
}
/**
* @return the nacionalidade
*/
public String getNacionalidade() {
return nacionalidade;
}
/**
* @param nacionalidade the nacionalidade to set
*/
public void setNacionalidade(String nacionalidade) {
this.nacionalidade = nacionalidade;
}
/**
* @return the fone
*/
public String getFone() {
return fone;
}
/**
* @param fone the fone to set
*/
public void setFone(String fone) {
this.fone = fone;
}
/**
* @return the celular
*/
public String getCelular() {
return celular;
}
/**
* @param celular the celular to set
*/
public void setCelular(String celular) {
this.celular = celular;
}
/**
* @return the fone_comercial
*/
public String getFone_comercial() {
return fone_comercial;
}
/**
* @param fone_comercial the fone_comercial to set
*/
public void setFone_comercial(String fone_comercial) {
this.fone_comercial = fone_comercial;
}
/**
* @return the email
*/
public String getEmail() {
return email;
}
/**
* @param email the email to set
*/
public void setEmail(String email) {
this.email = email;
}
/**
* @return the situacao
*/
public String getSituacao() {
return situacao;
}
/**
* @param situacao the situacao to set
*/
public void setSituacao(String situacao) {
this.situacao = situacao;
}
/**
* @return the crm
*/
public String getCrm() {
return crm;
}
/**
* @param crm the crm to set
*/
public void setCrm(String crm) {
this.crm = crm;
}
/**
* @return the blog
*/
public String getBlog() {
return blog;
}
/**
* @param blog the blog to set
*/
public void setBlog(String blog) {
this.blog = blog;
}
public String getIdPessoa() {
return idPessoa;
}
public void setIdPessoa(String idPessoa) {
this.idPessoa = idPessoa;
}
}
Classe SessionFactory
package br.com.miguelavaltroni.util;
import java.util.List;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author Avaltroni
*/
public class ConnectionProgramaFactory {
private static final SessionFactory sessionFactory;
private static final ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
static{
try{
sessionFactory=new Configuration()
.configure("hibernate.cfg.xml")
.buildSessionFactory();
}catch(Throwable t){
throw new ExceptionInInitializerError(t);
}
}
}
Interface DAO
package br.com.miguelavaltroni;
import java.util.List;
/**
*
* @author Avaltroni
*/
public interface InterfaceUsuarioDAO {
void salvar(usuario usaurio);
void excluir(usuario usuario);
void atualizar(usuario usuario);
void consultar(usuario usuario);
public abstract List todosUsuarios(String usuario) ;
}
Usuario DAO
package br.com.miguelavaltroni.util;
import br.com.miguelavaltroni.InterfaceUsuarioDAO;
import java.util.List;
import br.com.miguelavaltroni.usuario;
import br.com.miguelavaltroni.util.ConnectionProgramaFactory;
import java.beans.Expression;
import javax.management.Query;
import javax.security.auth.login.Configuration;
import org.apache.tomcat.dbcp.dbcp.ConnectionFactory;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
/**
*
* @author Avaltroni
*/
public class usuarioDAO implements InterfaceUsuarioDAO{
private static final SessionFactory sessionFactory;
private static final ThreadLocal<Session> threadLocal=new ThreadLocal<Session>();
public void salvar(usuario usuario){
}
public void excluir(usuario usario){
}
public void atualizar(usuario usuario){
}
public void consultar(usuario usuario){
}
public List todosUsuarios(String usuario){
Session session=sessionFactory.openSession();
String hql="from pessoas";
Query query=(Query) session.createQuery(hql);
return (List) query;
}
}
Session session=sessionFactory.openSession();
String hql="from pessoas";
Query query=(Query) session.createQuery(hql);
return (List) query;
Desde já, muito obrigado pela ajuda....