Modelo Tabela + Hibernate

3 respostas
adriano_si

Fala galera… na verdade não sei se a dúvida está no lugar certo, se não tiver pode mover ela pro fórum certo…

tenho um ModeloTabelaItem que configura minha JTable… e a mesma está assim:

package com.wordpress.aohana.gecon.geral.modelos;

import com.wordpress.aohana.gecon.cadastros.item.ItemDAO;
import com.wordpress.aohana.gecon.geral.base.BaseDAO;
import com.wordpress.aohana.gecon.geral.entidades.Item;
import java.util.List;
import javax.swing.table.AbstractTableModel;

/**
 *
 * @author Adriano Ohana
 */
public class ModeloTabelaItem extends AbstractTableModel {

    BaseDAO dao = new ItemDAO();
    List<Item> lista;
    String[] colunas = {"Cód.","Nome"};

    public ModeloTabelaItem() {
        this.lista = dao.findByAll("select idItem, nome from Item");
    }

    @Override
    public int getColumnCount() {
        return colunas.length;
    }

    @Override
    public int getRowCount() {
        return lista.size();
    }

    @Override
    public String getColumnName(int columnIndex) {
        return colunas[columnIndex];
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Item item = this.lista.get(rowIndex);

        if(columnIndex == 0)
            return item.getIdItem();
        else if(columnIndex == 1)
            return item.getNome();
        else
            return null;
    }

}

Minha entidade Item está anotada com JPA e minha classe está fazendo Persistẽncia de Dados com o Hibernate…

package com.wordpress.aohana.gecon.geral.base;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * Classe Abstrata que implementa os metodos de persistencia da aplicaçao,
 * recebendo a Entidade necessaria atraves de um generico <T>.
 *
 * @author Adriano Ohana
 * @since Abril 15, 2009
 */
public abstract class BaseDAO<T> implements IBaseDAO<T> {

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("GECONPU");
    private EntityManager em = emf.createEntityManager();

    public void persist(T entidade) {
        em.getTransaction().begin();
        try {
            em.persist(entidade);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
            em.getTransaction().rollback();
        } finally {
            em.close();
        }
    }

    public List<T> findByAll(String query) {
        try {
            return em.createQuery(query).getResultList();
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}

pois bem… na hora em que chamo a tela, a Seguinte exception me é lançada…

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.wordpress.aohana.gecon.geral.entidades.Item
        at com.wordpress.aohana.gecon.geral.modelos.ModeloTabelaItem.getValueAt(ModeloTabelaItem.java:40)
        at javax.swing.JTable.getValueAt(JTable.java:2695)
        at javax.swing.JTable.prepareRenderer(JTable.java:5712)
        at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2075)
        at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1977)
        at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1773)
        at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
        at javax.swing.JComponent.paintComponent(JComponent.java:763)
        at javax.swing.JComponent.paint(JComponent.java:1027)
        at javax.swing.JComponent.paintChildren(JComponent.java:864)
        at javax.swing.JComponent.paint(JComponent.java:1036)
        at javax.swing.JViewport.paint(JViewport.java:747)
        at javax.swing.JComponent.paintChildren(JComponent.java:864)
        at javax.swing.JComponent.paint(JComponent.java:1036)
        at javax.swing.JComponent.paintChildren(JComponent.java:864)
        at javax.swing.JComponent.paint(JComponent.java:1036)
        at javax.swing.JComponent.paintChildren(JComponent.java:864)
        at javax.swing.JComponent.paint(JComponent.java:1036)
        at javax.swing.JComponent.paintChildren(JComponent.java:864)
        at javax.swing.JComponent.paint(JComponent.java:1036)
        at javax.swing.JLayeredPane.paint(JLayeredPane.java:564)
        at javax.swing.JComponent.paintChildren(JComponent.java:864)
        at javax.swing.JComponent.paintToOffscreen(JComponent.java:5129)
        at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:277)
        at javax.swing.RepaintManager.paint(RepaintManager.java:1217)
        at javax.swing.JComponent.paint(JComponent.java:1013)
        at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
        at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
        at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
        at java.awt.Container.paint(Container.java:1762)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
        at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
        at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
        at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
        at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Pow… ClassCast ?? No que estou errando ?? entidades anotadas não podem receber cast ?? será que o findByAll não está preenchendo a lista com Objetos Item ??

Valew :wink:

3 Respostas

adriano_si

Pow… ninguém ??

ken420

Posso estar errado sou novato em programação mas posso tentar te da uma dica do que pode ser,
lendo isso

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.wordpress.aohana.gecon.geral.entidades.Item

Ta gerando uma execao, dentro de uma CAST em um OBJETO! se estiver errado alguem me corriga! flw

Marky.Vasconcelos

Esse método findByAll esta retornando uma lista mesmo?

Criado 25 de abril de 2009
Ultima resposta 27 de abr. de 2009
Respostas 3
Participantes 3