[RESOLVIDO] Como usar o @NamedQueries

boa tarde pessoal, to dando uma estudada aqui no hibernate e utilizando o mesmo com netbeans 6.5…to achando show…ja persistiu tudo…mas agora eu quero buscar…vi que quando gero o VO automaticamente a partir da tabela do banco e me traz umas buscas padroes, tipo getElementoById…etc…queria saber como usar isso…to usando com Annotations…

aqui vai meu VO

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package vo;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
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.Query;
import javax.persistence.Table;

/**
 *
 * @author abel
 */
@Entity
@Table(name = "cliente")
@NamedQueries({@NamedQuery(name = "VOCliente.findAll", query = "SELECT v FROM VOCliente v"), @NamedQuery(name = "VOCliente.findByIdCliente", query = "SELECT v FROM VOCliente v WHERE v.idCliente = :idCliente"), @NamedQuery(name = "VOCliente.findByNomeCLiente", query = "SELECT v FROM VOCliente v WHERE v.nomeCLiente = :nomeCLiente"), @NamedQuery(name = "VOCliente.findByEnderecoCliente", query = "SELECT v FROM VOCliente v WHERE v.enderecoCliente = :enderecoCliente"), @NamedQuery(name = "VOCliente.findByTelefoneCliente", query = "SELECT v FROM VOCliente v WHERE v.telefoneCliente = :telefoneCliente")})
public class VOCliente implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idCliente")
    private Integer idCliente;
    @Column(name = "nomeCLiente")
    private String nomeCLiente;
    @Column(name = "enderecoCliente")
    private String enderecoCliente;
    @Column(name = "telefoneCliente")
    private String telefoneCliente;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "cliente")
    private Collection<VOVenda> vOVendaCollection;

    public VOCliente() {
    }

    public VOCliente(Integer idCliente) {
        this.idCliente = idCliente;
    }

    public Integer getIdCliente() {
        return idCliente;
    }

    public void setIdCliente(Integer idCliente) {
        this.idCliente = idCliente;
    }

    public String getNomeCLiente() {
        return nomeCLiente;
    }

    public void setNomeCLiente(String nomeCLiente) {
        this.nomeCLiente = nomeCLiente;
    }

    public String getEnderecoCliente() {
        return enderecoCliente;
    }

    public void setEnderecoCliente(String enderecoCliente) {
        this.enderecoCliente = enderecoCliente;
    }

    public String getTelefoneCliente() {
        return telefoneCliente;
    }

    public void setTelefoneCliente(String telefoneCliente) {
        this.telefoneCliente = telefoneCliente;
    }

    public Collection<VOVenda> getVOVendaCollection() {
        return vOVendaCollection;
    }
    

    public void setVOVendaCollection(Collection<VOVenda> vOVendaCollection) {
        this.vOVendaCollection = vOVendaCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idCliente != null ? idCliente.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 VOCliente)) {
            return false;
        }
        VOCliente other = (VOCliente) object;
        if ((this.idCliente == null && other.idCliente != null) || (this.idCliente != null && !this.idCliente.equals(other.idCliente))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "vo.VOCliente[idCliente=" + idCliente + "]";
    }

}

agradeço desde já…

É o seguinte pessoal, depois de muita luta ficou assim

  1. tava faltando criar o PersistenceUnit:
    que ficou assim
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
  <persistence-unit name="TelasPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>vo.VOCliente</class>
    <class>vo.VOFuncionario</class>
    <class>vo.VOVenda</class>
    <properties>
      <property name="hibernate.connection.username" value="root"/>
      <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
      <property name="hibernate.connection.password" value="root"/>
      <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/testehibernate"/>
      <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
    </properties>
  </persistence-unit>
</persistence>

meu hibernate.cfg

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    
  <session-factory>
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testehibernate</property>
    <property name="hibernate.connection.username">root</property>
    <property name="hibernate.connection.password">root</property>

    <property name="hibernate.show_sql">true</property>
    <property name="hibernate.generate_statistics">true</property>
    <property name="hibernate.use_sql_comments">true</property>
    <mapping class="vo.VOCliente"></mapping>
    <mapping class="vo.VOFuncionario"/>
    <mapping class="vo.VOVenda"/>
  </session-factory>
</hibernate-configuration>

meu vo tem ai em cima

meu DAO

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import vo.VOCliente;

/**
 *
 * @author abel
 */
public class DAOCliente {

    private EntityManager em;
    private EntityManagerFactory emf;

    public DAOCliente() {
        emf = Persistence.createEntityManagerFactory("TelasPU");

        em = emf.createEntityManager();
    }

    public Collection<VOCliente> todosClientes() {

        Query query = em.createNamedQuery("VOCliente.findAll");
        return query.getResultList();
    }

    public VOCliente carregarCLienteById(Integer id){
        Query query = em.createNamedQuery("VOCliente.findByIdCliente");
        query.setParameter("idCliente", id);
        return (VOCliente) query.getSingleResult();
    }
}

está assim apenas para testes…

e minha classe teste

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package teste;

import core.HibernateUtil;
import dao.DAOCliente;
import dao.DAOFuncionario;
import java.util.Collection;
import org.hibernate.Session;
import org.hibernate.Transaction;
import vo.VOCliente;
import vo.VOFuncionario;
import vo.VOVenda;

/**
 *
 * @author abel
 */
public class Teste {

    public static void main(String... args) {

        Session sessao = HibernateUtil.getSession();
        Transaction tx = sessao.beginTransaction();

        VOCliente cliente = new VOCliente();

        //cliente.setId(2);
        /*cliente.setNome("Eduardo");
        cliente.setEndereco("Rua 5 ");
        cliente.setTelefone("999176556");*/

        VOFuncionario func = new VOFuncionario();

        /*func.setNomeFunc("Joyce");
        func.setEnderecoFunc("Centro");
        func.setTelefoneFunc("81662675");*/

        DAOCliente dao = new DAOCliente();
        DAOFuncionario daoFunc = new DAOFuncionario();

        /*Collection<VOCliente> lista = dao.todosClientes();
        
        for (VOCliente vOCliente : lista) {
        System.out.println(vOCliente.getNomeCLiente());

        }*/

        VOVenda venda = new VOVenda();

        venda.setTotalVenda(244);

        venda.setCliente(dao.carregarCLienteById(1));

        venda.setFuncrionario(daoFunc.carregarFuncionarioById(1));

        sessao.save(venda);

        tx.commit();

        sessao.close();
    }
}

com isso está tudo ok…CRUD