Bem, estou quebrando a cabeça com o Hibernate.
estou tentando fazer de um jeito que quando a janela for aberta ele já preencha a tabela com os dados já existentes no banco.
mas… bem, vejam os códigos.
Onde deveria preencher:
[code]private void formWindowOpened(java.awt.event.WindowEvent evt) {
jTextFieldId.setFocusable(false);
jTextFieldId.setEditable(false);
jTextFieldClienteNovo.setVisible(false);
jTextFieldResponsavelNovo.setVisible(false);
//Tabela Preenchida
OrdemServicoDao<OrdemServico> osDao = new HibernateOrdemServicoDao();
ResponsavelDao<Responsavel> respDao = new HibernateResponsavelDao();
OrdemServico os = new OrdemServico();
//Responsavel resp = new Responsavel();
for(Integer i = 0; i < osDao.listar().size(); i++) {
for (Integer z = 0; z <respDao.listar().size(); z++) {
os.setId(osDao.listar().get(i).getId());
os.setResponsavel(osDao.listar().get(i).getResponsavel());
//os.setResponsavel(respDao.listar().get(z));
os.setCliente(osDao.listar().get(i).getCliente());
os.setTeamViewerId(osDao.listar().get(i).getTeamViewerId());
os.setAmmy(osDao.listar().get(i).getAmmy());
os.setDescricao(osDao.listar().get(i).getDescricao());
os.setData(osDao.listar().get(i).getData());
os.setHora(osDao.listar().get(i).getHora());
for(Integer x = 1; x < osDao.listar().size(); x++) {
for (Integer y = 1; y <osDao.listar().size(); y++) {
jTableOrdemServico.setValueAt(os.getId(), x, y);
}
}
}
}
}[/code]
Onde está o método está:
[code]package br.gs.dao;
import java.lang.reflect.ParameterizedType;
import java.util.List;
public class HibernateGenericDao implements DaoGenerico {
@Override
public T inserir(T t) {
HibernateUtil.getSessao().save(t);
return t;
}
@Override
public T alterar(T t) {
HibernateUtil.getSessao().update(t);
return t;
}
@Override
public T excluir(T t) {
HibernateUtil.getSessao().delete(t);
return t;
}
@SuppressWarnings("unchecked")
@Override
public List<T> listar() {
return HibernateUtil.getSessao().createCriteria(getClassEntidade()).list();
}
@SuppressWarnings("unchecked")
protected Class<T> getClassEntidade() {
return (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}
}
[/code]
Classe OrdemServico:
[code]package br.gs.domain;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
@Entity
@Table (name = “ordem_servico”)
public class OrdemServico implements Serializable {
//Attributes
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private Long id;
@Column (name = "cliente", nullable = false)
private String cliente;
@Column (name = "teamviewer_id")
private String teamViewerId;
@Column (name = "ammy")
private String ammy;
@Column (name = "descricao", nullable = false)
private String descricao;
@Column (name = "data", nullable = false)
@Temporal(javax.persistence.TemporalType.DATE)
private Date data;
@Column (name = "hora", nullable = false)
@Temporal(TemporalType.TIME)
private Date hora;
@ManyToOne
@JoinColumn (name = "id_responsavel")
private Responsavel responsavel;
//Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTeamViewerId() {
return teamViewerId;
}
public void setTeamViewerId(String teamViewerId) {
this.teamViewerId = teamViewerId;
}
public String getAmmy() {
return ammy;
}
public void setAmmy(String ammy) {
this.ammy = ammy;
}
public String getDescricao() {
return descricao;
}
public void setDescricao(String descricao) {
this.descricao = descricao;
}
public Date getData() {
return data;
}
public void setData(Date data) {
this.data = data;
}
public Responsavel getResponsavel() {
return responsavel;
}
public void setResponsavel(Responsavel responsavel) {
this.responsavel = responsavel;
}
public String getCliente() {
return cliente;
}
public void setCliente(String cliente) {
this.cliente = cliente;
}
public Date getHora() {
return hora;
}
public void setHora(Date hora) {
this.hora = hora;
}
}
[/code]
Classe Responsavel:
[code]package br.gs.domain;
import java.io.Serializable;
import java.util.List;
import javax.persistence.*;
@Entity
@Table (name = “responsavel”)
public class Responsavel implements Serializable {
//Attributes
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue (strategy = GenerationType.AUTO)
private Long id;
@Column(name = "nome", nullable = false, unique = true)
private String nome;
@OneToMany(mappedBy="responsavel", fetch = FetchType.EAGER)
List<OrdemServico> ordens;
//Getters and Setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public List<OrdemServico> getOrdens() {
return ordens;
}
public void setOrdens(List<OrdemServico> ordens) {
this.ordens = ordens;
}
}
[/code]
Hibernate.cfg:
[code]<?xml version="1.0" encoding="UTF-8"?>
org.hibernate.dialect.PostgreSQLDialect org.postgresql.Driver jdbc:postgresql://localhost:5432/gs gs 123456 update true true<mapping class="br.gs.domain.OrdemServico"></mapping>
<mapping class="br.gs.domain.Responsavel"></mapping>
[/code]
E o maldito Erro:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at br.gs.dao.HibernateGenericDao.listar(HibernateGenericDao.java:29)
at br.gs.vision.Principal.formWindowOpened(Principal.java:392)
at br.gs.vision.Principal.access$0(Principal.java:379)
at br.gs.vision.Principal$1.windowOpened(Principal.java:88)
at java.awt.AWTEventMulticaster.windowOpened(Unknown Source)
at java.awt.Window.processWindowEvent(Unknown Source)
at javax.swing.JFrame.processWindowEvent(Unknown Source)
at java.awt.Window.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Se alguém puder me ajude…
obrigado.!