Pessoal,
estou tendo problemas em usar uma namedquery... ai vai o codigo onde tento usar:
@Resource EntityManager em;
private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
Query qry = em.createNamedQuery("Horista.findAll");
JOptionPane.showMessageDialog(this, "Chegou 4!");
List<Horista> lista = qry.getResultList();
JOptionPane.showMessageDialog(this, "Chegou 5!");
String [] colunas = {"Matrícula", "Nome", "CPF"};
DefaultTableModel modelo = new DefaultTableModel(colunas, 1);
tabela.setModel(modelo);
Horista hor;
for( int i = 0 ; i < lista.size() ; i ++) {
hor = lista.get(i);
String [] dados= {hor.getMatricula().toString(), hor.getNome(), hor.getCpf() };
modelo.addRow(dados);
}
em.close();
}
agora a minha entidade:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package phoenix.classes;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
*
* @author vinicius
*/
@Entity
@Table(name = "Horistas")
@NamedQueries({@NamedQuery(name = "Horista.findByMatricula", query = "SELECT f FROM Horista f WHERE f.matricula = :matricula"), @NamedQuery(name = "Horista.findByNome", query = "SELECT f FROM Horista f WHERE f.nome = :nome"), @NamedQuery(name = "Horista.findByCpf", query = "SELECT f FROM Horista f WHERE f.cpf = :cpf"), @NamedQuery(name = "Horista.findAll", query= "SELECT f FROM Horista f")})
public class Horista implements Serializable {
@Transient
private PropertyChangeSupport changeSupport = new PropertyChangeSupport(this);
private static final long serialVersionUID = 1L;
@Id
@SequenceGenerator(name = "seq_horistas", sequenceName = "seq_horistas")
@GeneratedValue ( generator="seq_horistas", strategy=GenerationType.SEQUENCE)
@Column(name = "matricula", nullable = false)
private Long matricula;
@Column(name = "nome")
private String nome;
@Column(name = "ident")
private String ident;
@Column(name = "cpf")
private String cpf;
@Column(name = "rua")
private String rua;
@Column(name = "numero")
private String numero;
@Column(name = "complemento")
private String complemento;
@Column(name = "bairro")
private String bairro;
@Column(name = "cidade")
private String cidade;
@Column(name = "estado")
private String estado;
@Column(name = "cep")
private String cep;
@Column(name = "pai")
private String pai;
@Column(name = "mae")
private String mae;
@Column(name = "telefone_res")
private String telefoneRes;
@Column(name = "telefone_cel")
private String telefoneCel;
@Column(name = "conta")
private String conta;
@Column(name = "agencia")
private String agencia;
@Column(name = "banco")
private String banco;
@OneToMany(mappedBy = "idFunc")
private Collection<Ponto> pontoCollection;
public Horista() {
}
public Horista(Long matricula) {
this.matricula = matricula;
}
public Long getMatricula() {
return matricula;
}
public void setMatricula(Long matricula) {
Long oldMatricula = this.matricula;
this.matricula = matricula;
changeSupport.firePropertyChange("matricula", oldMatricula, matricula);
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
String oldNome = this.nome;
this.nome = nome;
changeSupport.firePropertyChange("nome", oldNome, nome);
}
public String getIdent() {
return ident;
}
public void setIdent(String ident) {
String oldIdent = this.ident;
this.ident = ident;
changeSupport.firePropertyChange("ident", oldIdent, ident);
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
String oldCpf = this.cpf;
this.cpf = cpf;
changeSupport.firePropertyChange("cpf", oldCpf, cpf);
}
public String getRua() {
return rua;
}
public void setRua(String rua) {
String oldRua = this.rua;
this.rua = rua;
changeSupport.firePropertyChange("rua", oldRua, rua);
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
String oldNumero = this.numero;
this.numero = numero;
changeSupport.firePropertyChange("numero", oldNumero, numero);
}
public String getComplemento() {
return complemento;
}
public void setComplemento(String complemento) {
String oldComplemento = this.complemento;
this.complemento = complemento;
changeSupport.firePropertyChange("complemento", oldComplemento, complemento);
}
public String getBairro() {
return bairro;
}
public void setBairro(String bairro) {
String oldBairro = this.bairro;
this.bairro = bairro;
changeSupport.firePropertyChange("bairro", oldBairro, bairro);
}
public String getCidade() {
return cidade;
}
public void setCidade(String cidade) {
String oldCidade = this.cidade;
this.cidade = cidade;
changeSupport.firePropertyChange("cidade", oldCidade, cidade);
}
public String getEstado() {
return estado;
}
public void setEstado(String estado) {
String oldEstado = this.estado;
this.estado = estado;
changeSupport.firePropertyChange("estado", oldEstado, estado);
}
public String getCep() {
return cep;
}
public void setCep(String cep) {
String oldCep = this.cep;
this.cep = cep;
changeSupport.firePropertyChange("cep", oldCep, cep);
}
public String getPai() {
return pai;
}
public void setPai(String pai) {
String oldPai = this.pai;
this.pai = pai;
changeSupport.firePropertyChange("pai", oldPai, pai);
}
public String getMae() {
return mae;
}
public void setMae(String mae) {
String oldMae = this.mae;
this.mae = mae;
changeSupport.firePropertyChange("mae", oldMae, mae);
}
public String getTelefoneRes() {
return telefoneRes;
}
public void setTelefoneRes(String telefoneRes) {
String oldTelefoneRes = this.telefoneRes;
this.telefoneRes = telefoneRes;
changeSupport.firePropertyChange("telefoneRes", oldTelefoneRes, telefoneRes);
}
public String getTelefoneCel() {
return telefoneCel;
}
public void setTelefoneCel(String telefoneCel) {
String oldTelefoneCel = this.telefoneCel;
this.telefoneCel = telefoneCel;
changeSupport.firePropertyChange("telefoneCel", oldTelefoneCel, telefoneCel);
}
public String getConta() {
return conta;
}
public void setConta(String conta) {
String oldConta = this.conta;
this.conta = conta;
changeSupport.firePropertyChange("conta", oldConta, conta);
}
public String getAgencia() {
return agencia;
}
public void setAgencia(String agencia) {
String oldAgencia = this.agencia;
this.agencia = agencia;
changeSupport.firePropertyChange("agencia", oldAgencia, agencia);
}
public String getBanco() {
return banco;
}
public void setBanco(String banco) {
String oldBanco = this.banco;
this.banco = banco;
changeSupport.firePropertyChange("banco", oldBanco, banco);
}
@Override
public int hashCode() {
int hash = 0;
hash += (matricula != null ? matricula.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Horista)) {
return false;
}
Horista other = (Horista) object;
if ((this.matricula == null && other.matricula != null) || (this.matricula != null && !this.matricula.equals(other.matricula))) {
return false;
}
return true;
}
@Override
public String toString() {
return "phoenix.classes.Horista[matricula=" + matricula + "]";
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
changeSupport.addPropertyChangeListener(listener);
}
}
Agora, por fim, meu hibernate util
package phoenix.classes;
import org.hibernate.*;
import org.hibernate.cfg.*;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log exception!
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession()
throws HibernateException {
return sessionFactory.openSession();
}
}