Skip to content
Snippets Groups Projects
Commit 7be7ca36 authored by JotaJota96's avatar JotaJota96
Browse files

Clases con id autoincremental en BDD (falta quitar variable estatica generadora)

parent fe14f630
No related branches found
No related tags found
No related merge requests found
Showing
with 20 additions and 1475 deletions
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Administrador;
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;
/**
*
* @author Juan
*/
public class AdministradorJpaController implements Serializable {
public AdministradorJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Administrador administrador) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(administrador);
em.getTransaction().commit();
} catch (Exception ex) {
if (findAdministrador(administrador.getId()) != null) {
throw new PreexistingEntityException("Administrador " + administrador + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Administrador administrador) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
administrador = em.merge(administrador);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = administrador.getId();
if (findAdministrador(id) == null) {
throw new NonexistentEntityException("The administrador with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Administrador administrador;
try {
administrador = em.getReference(Administrador.class, id);
administrador.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The administrador with id " + id + " no longer exists.", enfe);
}
em.remove(administrador);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Administrador> findAdministradorEntities() {
return findAdministradorEntities(true, -1, -1);
}
public List<Administrador> findAdministradorEntities(int maxResults, int firstResult) {
return findAdministradorEntities(false, maxResults, firstResult);
}
private List<Administrador> findAdministradorEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Administrador.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Administrador findAdministrador(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Administrador.class, id);
} finally {
em.close();
}
}
public int getAdministradorCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Administrador> rt = cq.from(Administrador.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Canal;
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;
/**
*
* @author Juan
*/
public class CanalJpaController implements Serializable {
public CanalJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Canal canal) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(canal);
em.getTransaction().commit();
} catch (Exception ex) {
if (findCanal(canal.getId()) != null) {
throw new PreexistingEntityException("Canal " + canal + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Canal canal) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
canal = em.merge(canal);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = canal.getId();
if (findCanal(id) == null) {
throw new NonexistentEntityException("The canal with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Canal canal;
try {
canal = em.getReference(Canal.class, id);
canal.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The canal with id " + id + " no longer exists.", enfe);
}
em.remove(canal);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Canal> findCanalEntities() {
return findCanalEntities(true, -1, -1);
}
public List<Canal> findCanalEntities(int maxResults, int firstResult) {
return findCanalEntities(false, maxResults, firstResult);
}
private List<Canal> findCanalEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Canal.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Canal findCanal(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Canal.class, id);
} finally {
em.close();
}
}
public int getCanalCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Canal> rt = cq.from(Canal.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Categoria;
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;
/**
*
* @author Juan
*/
public class CategoriaJpaController implements Serializable {
public CategoriaJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Categoria categoria) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(categoria);
em.getTransaction().commit();
} catch (Exception ex) {
if (findCategoria(categoria.getNombre()) != null) {
throw new PreexistingEntityException("Categoria " + categoria + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Categoria categoria) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
categoria = em.merge(categoria);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
String id = categoria.getNombre();
if (findCategoria(id) == null) {
throw new NonexistentEntityException("The categoria with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(String id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Categoria categoria;
try {
categoria = em.getReference(Categoria.class, id);
categoria.getNombre();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The categoria with id " + id + " no longer exists.", enfe);
}
em.remove(categoria);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Categoria> findCategoriaEntities() {
return findCategoriaEntities(true, -1, -1);
}
public List<Categoria> findCategoriaEntities(int maxResults, int firstResult) {
return findCategoriaEntities(false, maxResults, firstResult);
}
private List<Categoria> findCategoriaEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Categoria.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Categoria findCategoria(String id) {
EntityManager em = getEntityManager();
try {
return em.find(Categoria.class, id);
} finally {
em.close();
}
}
public int getCategoriaCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Categoria> rt = cq.from(Categoria.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Comentario;
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;
/**
*
* @author Juan
*/
public class ComentarioJpaController implements Serializable {
public ComentarioJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Comentario comentario) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(comentario);
em.getTransaction().commit();
} catch (Exception ex) {
if (findComentario(comentario.getId()) != null) {
throw new PreexistingEntityException("Comentario " + comentario + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Comentario comentario) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
comentario = em.merge(comentario);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = comentario.getId();
if (findComentario(id) == null) {
throw new NonexistentEntityException("The comentario with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Comentario comentario;
try {
comentario = em.getReference(Comentario.class, id);
comentario.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The comentario with id " + id + " no longer exists.", enfe);
}
em.remove(comentario);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Comentario> findComentarioEntities() {
return findComentarioEntities(true, -1, -1);
}
public List<Comentario> findComentarioEntities(int maxResults, int firstResult) {
return findComentarioEntities(false, maxResults, firstResult);
}
private List<Comentario> findComentarioEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Comentario.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Comentario findComentario(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Comentario.class, id);
} finally {
em.close();
}
}
public int getComentarioCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Comentario> rt = cq.from(Comentario.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.ListaDeReproduccion;
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;
/**
*
* @author Juan
*/
public class ListaDeReproduccionJpaController implements Serializable {
public ListaDeReproduccionJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(ListaDeReproduccion listaDeReproduccion) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(listaDeReproduccion);
em.getTransaction().commit();
} catch (Exception ex) {
if (findListaDeReproduccion(listaDeReproduccion.getId()) != null) {
throw new PreexistingEntityException("ListaDeReproduccion " + listaDeReproduccion + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(ListaDeReproduccion listaDeReproduccion) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
listaDeReproduccion = em.merge(listaDeReproduccion);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = listaDeReproduccion.getId();
if (findListaDeReproduccion(id) == null) {
throw new NonexistentEntityException("The listaDeReproduccion with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
ListaDeReproduccion listaDeReproduccion;
try {
listaDeReproduccion = em.getReference(ListaDeReproduccion.class, id);
listaDeReproduccion.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The listaDeReproduccion with id " + id + " no longer exists.", enfe);
}
em.remove(listaDeReproduccion);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<ListaDeReproduccion> findListaDeReproduccionEntities() {
return findListaDeReproduccionEntities(true, -1, -1);
}
public List<ListaDeReproduccion> findListaDeReproduccionEntities(int maxResults, int firstResult) {
return findListaDeReproduccionEntities(false, maxResults, firstResult);
}
private List<ListaDeReproduccion> findListaDeReproduccionEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(ListaDeReproduccion.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public ListaDeReproduccion findListaDeReproduccion(int id) {
EntityManager em = getEntityManager();
try {
return em.find(ListaDeReproduccion.class, id);
} finally {
em.close();
}
}
public int getListaDeReproduccionCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<ListaDeReproduccion> rt = cq.from(ListaDeReproduccion.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.ListaPorDefecto;
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;
/**
*
* @author Juan
*/
public class ListaPorDefectoJpaController implements Serializable {
public ListaPorDefectoJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(ListaPorDefecto listaPorDefecto) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(listaPorDefecto);
em.getTransaction().commit();
} catch (Exception ex) {
if (findListaPorDefecto(listaPorDefecto.getNombre()) != null) {
throw new PreexistingEntityException("ListaPorDefecto " + listaPorDefecto + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(ListaPorDefecto listaPorDefecto) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
listaPorDefecto = em.merge(listaPorDefecto);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
String id = listaPorDefecto.getNombre();
if (findListaPorDefecto(id) == null) {
throw new NonexistentEntityException("The listaPorDefecto with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(String id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
ListaPorDefecto listaPorDefecto;
try {
listaPorDefecto = em.getReference(ListaPorDefecto.class, id);
listaPorDefecto.getNombre();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The listaPorDefecto with id " + id + " no longer exists.", enfe);
}
em.remove(listaPorDefecto);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<ListaPorDefecto> findListaPorDefectoEntities() {
return findListaPorDefectoEntities(true, -1, -1);
}
public List<ListaPorDefecto> findListaPorDefectoEntities(int maxResults, int firstResult) {
return findListaPorDefectoEntities(false, maxResults, firstResult);
}
private List<ListaPorDefecto> findListaPorDefectoEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(ListaPorDefecto.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public ListaPorDefecto findListaPorDefecto(String id) {
EntityManager em = getEntityManager();
try {
return em.find(ListaPorDefecto.class, id);
} finally {
em.close();
}
}
public int getListaPorDefectoCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<ListaPorDefecto> rt = cq.from(ListaPorDefecto.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Persona;
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;
/**
*
* @author Juan
*/
public class PersonaJpaController implements Serializable {
public PersonaJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Persona persona) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(persona);
em.getTransaction().commit();
} catch (Exception ex) {
if (findPersona(persona.getId()) != null) {
throw new PreexistingEntityException("Persona " + persona + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Persona persona) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
persona = em.merge(persona);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = persona.getId();
if (findPersona(id) == null) {
throw new NonexistentEntityException("The persona with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Persona persona;
try {
persona = em.getReference(Persona.class, id);
persona.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The persona with id " + id + " no longer exists.", enfe);
}
em.remove(persona);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Persona> findPersonaEntities() {
return findPersonaEntities(true, -1, -1);
}
public List<Persona> findPersonaEntities(int maxResults, int firstResult) {
return findPersonaEntities(false, maxResults, firstResult);
}
private List<Persona> findPersonaEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Persona.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Persona findPersona(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Persona.class, id);
} finally {
em.close();
}
}
public int getPersonaCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Persona> rt = cq.from(Persona.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Usuario;
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;
/**
*
* @author Juan
*/
public class UsuarioJpaController implements Serializable {
public UsuarioJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Usuario usuario) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(usuario);
em.getTransaction().commit();
} catch (Exception ex) {
if (findUsuario(usuario.getId()) != null) {
throw new PreexistingEntityException("Usuario " + usuario + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Usuario usuario) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
usuario = em.merge(usuario);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = usuario.getId();
if (findUsuario(id) == null) {
throw new NonexistentEntityException("The usuario with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Usuario usuario;
try {
usuario = em.getReference(Usuario.class, id);
usuario.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The usuario with id " + id + " no longer exists.", enfe);
}
em.remove(usuario);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Usuario> findUsuarioEntities() {
return findUsuarioEntities(true, -1, -1);
}
public List<Usuario> findUsuarioEntities(int maxResults, int firstResult) {
return findUsuarioEntities(false, maxResults, firstResult);
}
private List<Usuario> findUsuarioEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Usuario.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Usuario findUsuario(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Usuario.class, id);
} finally {
em.close();
}
}
public int getUsuarioCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Usuario> rt = cq.from(Usuario.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import Logica.Clases.Valoracion;
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;
/**
*
* @author Juan
*/
public class ValoracionJpaController implements Serializable {
public ValoracionJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Valoracion valoracion) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(valoracion);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Valoracion valoracion) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
valoracion = em.merge(valoracion);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = valoracion.getId();
if (findValoracion(id) == null) {
throw new NonexistentEntityException("The valoracion with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Valoracion valoracion;
try {
valoracion = em.getReference(Valoracion.class, id);
valoracion.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The valoracion with id " + id + " no longer exists.", enfe);
}
em.remove(valoracion);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Valoracion> findValoracionEntities() {
return findValoracionEntities(true, -1, -1);
}
public List<Valoracion> findValoracionEntities(int maxResults, int firstResult) {
return findValoracionEntities(false, maxResults, firstResult);
}
private List<Valoracion> findValoracionEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Valoracion.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Valoracion findValoracion(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Valoracion.class, id);
} finally {
em.close();
}
}
public int getValoracionCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Valoracion> rt = cq.from(Valoracion.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
/*
* 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 JPAControllerClasses;
import JPAControllerClasses.exceptions.NonexistentEntityException;
import JPAControllerClasses.exceptions.PreexistingEntityException;
import Logica.Clases.Video;
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;
/**
*
* @author Juan
*/
public class VideoJpaController implements Serializable {
public VideoJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
public void create(Video video) throws PreexistingEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
em.persist(video);
em.getTransaction().commit();
} catch (Exception ex) {
if (findVideo(video.getId()) != null) {
throw new PreexistingEntityException("Video " + video + " already exists.", ex);
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void edit(Video video) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
video = em.merge(video);
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
int id = video.getId();
if (findVideo(id) == null) {
throw new NonexistentEntityException("The video with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}
public void destroy(int id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Video video;
try {
video = em.getReference(Video.class, id);
video.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The video with id " + id + " no longer exists.", enfe);
}
em.remove(video);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}
public List<Video> findVideoEntities() {
return findVideoEntities(true, -1, -1);
}
public List<Video> findVideoEntities(int maxResults, int firstResult) {
return findVideoEntities(false, maxResults, firstResult);
}
private List<Video> findVideoEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Video.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}
public Video findVideo(int id) {
EntityManager em = getEntityManager();
try {
return em.find(Video.class, id);
} finally {
em.close();
}
}
public int getVideoCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Video> rt = cq.from(Video.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}
}
package JPAControllerClasses.exceptions;
import java.util.ArrayList;
import java.util.List;
public class IllegalOrphanException extends Exception {
private List<String> messages;
public IllegalOrphanException(List<String> messages) {
super((messages != null && messages.size() > 0 ? messages.get(0) : null));
if (messages == null) {
this.messages = new ArrayList<String>();
}
else {
this.messages = messages;
}
}
public List<String> getMessages() {
return messages;
}
}
package JPAControllerClasses.exceptions;
public class NonexistentEntityException extends Exception {
public NonexistentEntityException(String message, Throwable cause) {
super(message, cause);
}
public NonexistentEntityException(String message) {
super(message);
}
}
package JPAControllerClasses.exceptions;
public class PreexistingEntityException extends Exception {
public PreexistingEntityException(String message, Throwable cause) {
super(message, cause);
}
public PreexistingEntityException(String message) {
super(message);
}
}
package Logica.Clases;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
......@@ -11,6 +14,9 @@ public class Administrador extends Persona implements Serializable{
private static int contadorAdministrador = 1;
@Id
// No logre hacer que el id fuera auto-incremental, no se por que, probe de todo y no pude
//@GeneratedValue(strategy=GenerationType.IDENTITY)
//@Column(name = "id")
private int id;
//-------------------------------------------------------------------------
......
......@@ -16,6 +16,8 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
......@@ -27,6 +29,7 @@ public class Canal implements Serializable {
private static int contadorCanal = 1;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
......
......@@ -9,6 +9,8 @@ import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
......@@ -21,6 +23,7 @@ public class Comentario implements Serializable {
private static int contadorComentarios = 1;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
......
......@@ -13,6 +13,8 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
......@@ -25,6 +27,7 @@ public class ListaDeReproduccion implements Serializable {
private static int contadorListasDeReproduccion = 1;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
......
......@@ -4,6 +4,7 @@ import Logica.DataType.DtValoracion;
import Logica.Enumerados.TipoValoracion;
import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
......@@ -20,7 +21,7 @@ public class Valoracion implements Serializable {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@JoinColumn(name = "id")
@Column(name = "id")
private int id;
@Enumerated(EnumType.STRING)
......
......@@ -17,6 +17,8 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
......@@ -28,6 +30,7 @@ public class Video implements Serializable {
private static int idActual = 1;
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private int id;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment