Ciao!!!
alura.... con hibernate ultimamente sono riuscito a fare diverse belle cosine, l'unica cosa che mi rimane oscura è l'autoincrement ovvero..
Creo l'entity di una tabella su mysql:
poi uso il metodo della JPA controller (creata automaticamente) da netbeans "Create(utente)":codice:package db.utente; 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; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.xml.bind.annotation.XmlRootElement; /** * * @author daniele */ @Entity @Table(name = "utente") @XmlRootElement @NamedQueries({ @NamedQuery(name = "Utente.findAll", query = "SELECT u FROM Utente u"), @NamedQuery(name = "Utente.findById", query = "SELECT u FROM Utente u WHERE u.id = :id"), @NamedQuery(name = "Utente.findByNome", query = "SELECT u FROM Utente u WHERE u.nome = :nome"), @NamedQuery(name = "Utente.findByCognome", query = "SELECT u FROM Utente u WHERE u.cognome = :cognome"), @NamedQuery(name = "Utente.findByPassword", query = "SELECT u FROM Utente u WHERE u.password = :password"), @NamedQuery(name = "Utente.findByMail", query = "SELECT u FROM Utente u WHERE u.mail = :mail"), @NamedQuery(name = "Utente.findBySocieta", query = "SELECT u FROM Utente u WHERE u.societa = :societa")}) public class Utente implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Basic(optional = false) @NotNull @Column(name = "id") private Integer id; @Basic(optional = false) @NotNull @Size(min = 1, max = 45) @Column(name = "nome") private String nome; @Basic(optional = false) @NotNull @Size(min = 1, max = 45) @Column(name = "cognome") private String cognome; @Basic(optional = false) @NotNull @Size(min = 1, max = 45) @Column(name = "password") private String password; @Basic(optional = false) @NotNull @Size(min = 1, max = 45) @Column(name = "mail") private String mail; @Basic(optional = false) @NotNull @Size(min = 1, max = 45) @Column(name = "societa") private String societa; public Utente() { } public Utente(Integer id) { this.id = id; } public Utente(Integer id, String nome, String cognome, String password, String mail, String societa) { this.id = id; this.nome = nome; this.cognome = cognome; this.password = password; this.mail = mail; this.societa = societa; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } public String getCognome() { return cognome; } public void setCognome(String cognome) { this.cognome = cognome; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getMail() { return mail; } public void setMail(String mail) { this.mail = mail; } public String getSocieta() { return societa; } public void setSocieta(String societa) { this.societa = societa; } @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 Utente)) { return false; } Utente other = (Utente) 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 "db.utente.Utente[ id=" + id + " ]"; } }
codice:package db.utente; import db.exceptions.NonexistentEntityException; import db.exceptions.RollbackFailureException; import java.io.Serializable; import java.util.List; import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Query; import javax.persistence.EntityNotFoundException; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import javax.transaction.UserTransaction; /** * * @author daniele */ public class UtenteJpaController implements Serializable { public UtenteJpaController(UserTransaction utx, EntityManagerFactory emf) { this.utx = utx; this.emf = emf; } private UserTransaction utx = null; private EntityManagerFactory emf = null; public EntityManager getEntityManager() { return emf.createEntityManager(); } public void create(Utente utente) throws RollbackFailureException, Exception { EntityManager em = null; try { utx.begin(); em = getEntityManager(); em.persist(utente); utx.commit(); } catch (Exception ex) { try { utx.rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } throw ex; } finally { if (em != null) { em.close(); } } } public void edit(Utente utente) throws NonexistentEntityException, RollbackFailureException, Exception { EntityManager em = null; try { utx.begin(); em = getEntityManager(); utente = em.merge(utente); utx.commit(); } catch (Exception ex) { try { utx.rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } String msg = ex.getLocalizedMessage(); if (msg == null || msg.length() == 0) { Integer id = utente.getId(); if (findUtente(id) == null) { throw new NonexistentEntityException("The utente with id " + id + " no longer exists."); } } throw ex; } finally { if (em != null) { em.close(); } } } public void destroy(Integer id) throws NonexistentEntityException, RollbackFailureException, Exception { EntityManager em = null; try { utx.begin(); em = getEntityManager(); Utente utente; try { utente = em.getReference(Utente.class, id); utente.getId(); } catch (EntityNotFoundException enfe) { throw new NonexistentEntityException("The utente with id " + id + " no longer exists.", enfe); } em.remove(utente); utx.commit(); } catch (Exception ex) { try { utx.rollback(); } catch (Exception re) { throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re); } throw ex; } finally { if (em != null) { em.close(); } } } public List<Utente> findUtenteEntities() { return findUtenteEntities(true, -1, -1); } public List<Utente> findUtenteEntities(int maxResults, int firstResult) { return findUtenteEntities(false, maxResults, firstResult); } private List<Utente> findUtenteEntities(boolean all, int maxResults, int firstResult) { EntityManager em = getEntityManager(); try { CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); cq.select(cq.from(Utente.class)); Query q = em.createQuery(cq); if (!all) { q.setMaxResults(maxResults); q.setFirstResult(firstResult); } return q.getResultList(); } finally { em.close(); } } public Utente findUtente(Integer id) { EntityManager em = getEntityManager(); try { return em.find(Utente.class, id); } finally { em.close(); } } public int getUtenteCount() { EntityManager em = getEntityManager(); try { CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); Root<Utente> rt = cq.from(Utente.class); cq.select(em.getCriteriaBuilder().count(rt)); Query q = em.createQuery(cq); return ((Long) q.getSingleResult()).intValue(); } finally { em.close(); } } }
gli passo nome, cognome, email, societa, password... e non gli passo l'id in quanto dovrebbe essere autoincrement maaaa! da errore di persistence in quanto l'id non puo essere null.
Qualcuno ha idea di cosa devo fare?

Rispondi quotando