Pessoal,
Comecei a ler sobre persistência e achei muito inteligente a forme de manipular o banco, gostaria de uma ajuda.
1° Criei uma Classe de Entidade de banco de dados.
2° Selecionei meu banco, e a tabela OK!
3° Depois criei uma CLASSE DE CONTROLADOR DO JPA, onde vem práticamente pronto o CRUD.
Minha dúvida é no meu metodo Main como seria para utilizar meu objeto instanciado?
import java.io.Serializable;
import javax.persistence.Basic;
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.Table;
/**
*
* @author Caio Fernando
*/
@Entity
@Table(name = "contatos")
@NamedQueries({
@NamedQuery(name = "Contatos.findAll", query = "SELECT c FROM Contatos c"),
@NamedQuery(name = "Contatos.findByIdcontatos", query = "SELECT c FROM Contatos c WHERE c.idcontatos = :idcontatos"),
@NamedQuery(name = "Contatos.findByIdNome", query = "SELECT c FROM Contatos c WHERE c.idNome = :idNome"),
@NamedQuery(name = "Contatos.findByIdEndereco", query = "SELECT c FROM Contatos c WHERE c.idEndereco = :idEndereco"),
@NamedQuery(name = "Contatos.findByIdTell", query = "SELECT c FROM Contatos c WHERE c.idTell = :idTell"),
@NamedQuery(name = "Contatos.findByIdEmail", query = "SELECT c FROM Contatos c WHERE c.idEmail = :idEmail"),
@NamedQuery(name = "Contatos.findByIdSexo", query = "SELECT c FROM Contatos c WHERE c.idSexo = :idSexo")})
public class Contatos implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "idcontatos")
private Integer idcontatos;
@Column(name = "idNome")
private String idNome;
@Column(name = "idEndereco")
private String idEndereco;
@Column(name = "idTell")
private String idTell;
@Column(name = "idEmail")
private String idEmail;
@Column(name = "idSexo")
private String idSexo;
public Contatos() {
}
public Contatos(Integer idcontatos) {
this.idcontatos = idcontatos;
}
public Integer getIdcontatos() {
return idcontatos;
}
public void setIdcontatos(Integer idcontatos) {
this.idcontatos = idcontatos;
}
public String getIdNome() {
return idNome;
}
public void setIdNome(String idNome) {
this.idNome = idNome;
}
public String getIdEndereco() {
return idEndereco;
}
public void setIdEndereco(String idEndereco) {
this.idEndereco = idEndereco;
}
public String getIdTell() {
return idTell;
}
public void setIdTell(String idTell) {
this.idTell = idTell;
}
public String getIdEmail() {
return idEmail;
}
public void setIdEmail(String idEmail) {
this.idEmail = idEmail;
}
public String getIdSexo() {
return idSexo;
}
public void setIdSexo(String idSexo) {
this.idSexo = idSexo;
}
@Override
public int hashCode() {
int hash = 0;
hash += (idcontatos != null ? idcontatos.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 Contatos)) {
return false;
}
Contatos other = (Contatos) object;
if ((this.idcontatos == null && other.idcontatos != null) || (this.idcontatos != null && !this.idcontatos.equals(other.idcontatos))) {
return false;
}
return true;
}
@Override
public String toString() {
return "teste1.Contatos[idcontatos=" + idcontatos + "]";
}
}
public class ContatosJpaController {
public ContatosJpaController() {
emf = Persistence.createEntityManagerFactory("teste1PU");
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Contatos contatos) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(contatos);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Contatos contatos) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
contatos = em.merge(contatos);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = contatos.getIdcontatos();
if (findContatos(id) == null) {
throw new NonexistentEntityException("The contatos with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Contatos contatos;
try {
contatos = em.getReference(Contatos.class, id);
contatos.getIdcontatos();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The contatos with id " + id + " no longer exists.", enfe);
}
em.remove(contatos);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Contatos> findContatosEntities() {
return findContatosEntities(true, -1, -1);
}
public List<Contatos> findContatosEntities(int maxResults, int firstResult) {
return findContatosEntities(false, maxResults, firstResult);
}
private List<Contatos> findContatosEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Contatos.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Contatos findContatos(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Contatos.class, id);
} finally {
em.close();
}
}
public int getContatosCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Contatos> rt = cq.from(Contatos.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
Na execução que é minha duvida seria algo do tipo....?????????????
public static void main(String[] args) {
Contatos c1 = new Contatos();
c1.setIdNome("CAIO");
ContatosJpaController cjpa = new ContatosJpaController();
cjpa.create(c1);
}