Problemas com JPA

1 resposta
Paulinho_sismax
Boa noite a todos, estou começando com JPA e estou em uma duvida em insrir dados de um simples cadastro de cliente, criei uma classe chamada ClienteDAO onde não tem nehum erro:
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.LinkedList;
import sismax.modelo.Cliente;


public class ClienteDAO {

    public static Object getInstance() {
        throw new UnsupportedOperationException("Não esta dando certo!!");
    }

    LinkedList < Cliente > dados;
    public ClienteDAO() {
        dados = new LinkedList < Cliente > ();
    }
    public void inserir (Cliente p) throws SQLException{
        Connection cnx =GenericDAO.getConexao();
        PreparedStatement ps = GenericDAO.getConexao().prepareStatement(
                "Insert into Cliente (nome, dtNasc, rg, telefone, celular, email, dtCad) values (?, ?, ?, ?, ?, ?, ?)");

    }
}

a classe GenericDO onde faz a conexão com o Banco este aparece um erro na linha 5:

import java.sql.Connection;
import java.util.List;
import javax.persistence.*;

[u]public class GenericDAO {[/u]

    static Connection getConexao() {
        throw new UnsupportedOperationException("Ainda não está dando certo!");
    }

    private EntityManager em = null;
    private static GenericDAO dao = null;

    private GenericDAO() {
        em = Persistence.createEntityManagerFactory("SISMAXPU").createEntityManager();
    }

    public static GenericDAO getInstance() {
        if (dao == null)
            dao = new GenericDAO();
        return dao;
    }
    public void persistir(Object obj) {
        EntityTransaction t = em.getTransaction();
        try {
            t.begin();
            obj = em.merge(obj);
            em.persist(obj);
            t.commit();
        } catch (Exception ex) {
            if (t.isActive()) t.rollback();
        }
    }
    public void remover(Object obj) {
        EntityTransaction t = em.getTransaction();
        try {
            t.begin();
            obj = em.merge(obj);
            em.remove(obj);
            t.commit();
        }
        catch (Exception ex) {
            if ( t.isActive() ) t.rollback();
        }
    }

    List listar( String query ) {
        return em.createQuery( query ).getResultList();
    }
}
e a classe Clente onde consta os itens da tabela que consta um erro na linha sublinhada:
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.Temporal;


@Entity
public class [u]Cliente[/u] implements Serializable {
    private static long serialVersionUID = 1L;

    /**
     * @return the serialVersionUID
     */
    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    /**
     * @param aSerialVersionUID the serialVersionUID to set
     */
    public static void setSerialVersionUID(long aSerialVersionUID) {
        serialVersionUID = aSerialVersionUID;
    }
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

        private String cpf;

        @Column( length=50, unique=true, nullable=false )
        private String nome;

        @Temporal(javax.persistence.TemporalType.DATE)
        private Date dtNasc;

        @Column( length=10, unique=true, nullable=false )
        private String rg;

        @Column( length=10, unique=true, nullable=false )
        private String telefone;

        @Column( length=10, unique=true, nullable=false )
        private String celular;

        @Column( length=20, unique=true, nullable=false )
        private String email;

        @Temporal(javax.persistence.TemporalType.DATE)
        private Date dtCad;

    public Cliente(String text, String text0, String text1, String text2, String text3, String text4, String text5, String text6) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public Date getDtCad() {
        return dtCad;
    }

    public void setDtCad(Date dtCad) {
        this.dtCad = dtCad;
    }

    public String getCelular() {
        return celular;
    }

    public void setCelular(String celular) {
        this.celular = celular;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }

    public Date getDtNasc() {
        return dtNasc;
    }

    public void setDtNasc(Date dtNasc) {
        this.dtNasc = dtNasc;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getRg() {
        return rg;
    }

    public void setRg(String rg) {
        this.rg = rg;
    }

    public String getTelefone() {
        return telefone;
    }

    public void setTelefone(String telefone) {
        this.telefone = telefone;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

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

    @Override
    public String toString() {
        return "sismax.modelo.NewEntity[id=" + id + "]";
    }

}
e o meu action perfomed Inserir e consta um erro no inserir sublinhado:
private void BtSalvar1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
            try {
            Cliente p = new Cliente( TxNome1.getText(), TxRg1.getText(), TxCpf1.getText(), TxfDtNasc1.getText(),
                    TxCelular1.getText(), TxEmail1.getText(), TxTelefone1.getText(), txDtCadCliente.getText());
            ClienteDAO.getInstance().inserir(p);

        } catch (Exception ex) {
            javax.swing.JOptionPane.showMessageDialog(this, "Erro:\n"+ex.getMessage() );
        }
    }
alguém pode me ajudar nisso já me bati e não consigo encontrar um maneira de conserta-lo

1 Resposta

Paulinho_sismax

No actionPerfomed Inserir ele da um erro na seguinte linha:

ClienteDAO.getInstance().inserir(p);

e o erro que mostra é o seguinte
“CANNOT FIND SIMBOL
SIMBOL: METHOD INSERIR (SISMAX.MODELO.CLIENTE)
LOCATION: CLASS JAVA.LANG.OBJECT”

com tudo não consegui identificar o problema .

Criado 19 de dezembro de 2009
Ultima resposta 19 de dez. de 2009
Respostas 1
Participantes 1