Estou iniciando em Java e tive que obter um projeto imediatamente, do qual não tive tempo de aprender os detalhes. De qualquer forma, tenho 3 classes, duas das quais estão no pacote do modelo, sendo a classe Produto com as entidades e a classe ProdutoDAO, que era para recuperar as entidades, e no pacote testebase, tenho a classe TesteBase, na qual instancio um objeto da classe ProdutoDAO, mas não consigo recuperar os relacionamentos da tabela, veja os códigos dos três arquivos, ficaria muito grato se puderem me ajudar
Classe Produto
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//Database Entity Class
package modelo;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
/**
*
* @author Yuri0x0
*/
@Entity
@Table(name = "PRODUTO")
@NamedQueries({
@NamedQuery(name = "Produto.findAll", query = "SELECT p FROM Produto p")})
public class Produto implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@Column(name = "CODIGO")
private Integer codigo;
@Column(name = "NOME")
private String nome;
@Column(name = "QUANTIDADE")
private Integer quantidade;
public Produto() {
}
public Produto(Integer codigo) {
this.codigo = codigo;
}
public Integer getCodigo() {
return codigo;
}
public void setCodigo(Integer codigo) {
this.codigo = codigo;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public Integer getQuantidade() {
return quantidade;
}
public void setQuantidade(Integer quantidade) {
this.quantidade = quantidade;
}
@Override
public int hashCode() {
int hash = 0;
hash += (codigo != null ? codigo.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 Produto)) {
return false;
}
Produto other = (Produto) object;
if ((this.codigo == null && other.codigo != null) || (this.codigo != null && !this.codigo.equals(other.codigo))) {
return false;
}
return true;
}
@Override
public String toString() {
return "modelo.Produto[ codigo=" + codigo + " ]";
}
}
Classe ProdutoDAO
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package modelo;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaQuery;
/**
*
* @author Yuri0x0
*/
public class ProdutoDAO implements Serializable {
private final EntityManagerFactory emf;
public ProdutoDAO() {
emf = Persistence.createEntityManagerFactory("TesteBasePU");
}
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void incluir(Produto produto) {
EntityManager em = getEntityManager();
try {
em.getTransaction().begin();
em.persist(produto);
em.getTransaction().commit();
} finally {
em.close();
}
}
public void excluir(Integer codigo) {
EntityManager em = getEntityManager();
try {
em.getTransaction().begin();
Produto produto = em.find(Produto.class, codigo);
em.remove(produto);
em.getTransaction().commit();
} finally {
em.close();
}
}
public List obterTodos() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq =
em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Produto.class));
Query q = em.createQuery(cq);
return q.getResultList();
} finally {
em.close();
}
}
}
Classe TesteBase
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package testebase;
/**
*
* @author Yuri0x0
*/
import modelo.*;
public class TesteBase {
public static void main(String[] args) {
ProdutoDAO dao = new ProdutoDAO();
// Error dao.obterTodos.getCodigo();
}
}
