Agregada busqueda y correccion de errores
This commit is contained in:
@@ -2,11 +2,16 @@ package xyz.danielcortes.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceException;
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
import xyz.danielcortes.framework.PersistenceManager;
|
import xyz.danielcortes.framework.PersistenceManager;
|
||||||
import xyz.danielcortes.models.Categoria;
|
import xyz.danielcortes.models.Categoria;
|
||||||
|
|
||||||
public class CategoriaRepository {
|
public class CategoriaRepository {
|
||||||
|
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
public CategoriaRepository() {
|
public CategoriaRepository() {
|
||||||
@@ -18,20 +23,50 @@ public class CategoriaRepository {
|
|||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Categoria> search(String term) {
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Categoria> query = cb.createQuery(Categoria.class);
|
||||||
|
Root<Categoria> r = query.from(Categoria.class);
|
||||||
|
query.where(
|
||||||
|
cb.like(cb.lower(r.get("nombre")), "%" + term.toLowerCase() + "%")
|
||||||
|
);
|
||||||
|
return em.createQuery(query).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
public void save(Categoria categoria) {
|
public void save(Categoria categoria) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(categoria);
|
|
||||||
|
try {
|
||||||
|
em.persist(categoria);
|
||||||
|
} catch (
|
||||||
|
PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Categoria categoria) {
|
public void update(Categoria categoria) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.merge(categoria);
|
|
||||||
|
try {
|
||||||
|
em.merge(categoria);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Categoria categoria) {
|
public void delete(Categoria categoria) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.remove(categoria);
|
|
||||||
|
try {
|
||||||
|
em.remove(categoria);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,16 @@ package xyz.danielcortes.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceException;
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
import xyz.danielcortes.framework.PersistenceManager;
|
import xyz.danielcortes.framework.PersistenceManager;
|
||||||
import xyz.danielcortes.models.Editorial;
|
import xyz.danielcortes.models.Editorial;
|
||||||
|
|
||||||
public class EditorialRepository {
|
public class EditorialRepository {
|
||||||
|
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
public EditorialRepository() {
|
public EditorialRepository() {
|
||||||
@@ -18,20 +23,51 @@ public class EditorialRepository {
|
|||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Editorial> search(String term) {
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Editorial> query = cb.createQuery(Editorial.class);
|
||||||
|
Root<Editorial> r = query.from(Editorial.class);
|
||||||
|
query.where(
|
||||||
|
cb.like(cb.lower(r.get("nombre")), "%" + term.toLowerCase() + "%")
|
||||||
|
);
|
||||||
|
return em.createQuery(query).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
public void save(Editorial editorial) {
|
public void save(Editorial editorial) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(editorial);
|
|
||||||
|
try {
|
||||||
|
em.persist(editorial);
|
||||||
|
} catch (
|
||||||
|
PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Editorial editorial) {
|
public void update(Editorial editorial) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
|
|
||||||
|
try{
|
||||||
em.merge(editorial);
|
em.merge(editorial);
|
||||||
|
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Editorial editorial) {
|
public void delete(Editorial editorial) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
|
|
||||||
|
try{
|
||||||
em.remove(editorial);
|
em.remove(editorial);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,11 +2,13 @@ package xyz.danielcortes.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceException;
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
import xyz.danielcortes.framework.PersistenceManager;
|
import xyz.danielcortes.framework.PersistenceManager;
|
||||||
import xyz.danielcortes.models.Estado;
|
import xyz.danielcortes.models.Estado;
|
||||||
|
|
||||||
public class EstadoRepository {
|
public class EstadoRepository {
|
||||||
|
|
||||||
private EntityManager em;
|
private EntityManager em;
|
||||||
|
|
||||||
public EstadoRepository() {
|
public EstadoRepository() {
|
||||||
@@ -26,18 +28,38 @@ public class EstadoRepository {
|
|||||||
|
|
||||||
public void save(Estado estado) {
|
public void save(Estado estado) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(estado);
|
|
||||||
|
try {
|
||||||
|
em.persist(estado);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Estado estado) {
|
public void update(Estado estado) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.merge(estado);
|
|
||||||
|
try {
|
||||||
|
em.merge(estado);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Estado estado) {
|
public void delete(Estado estado) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.remove(estado);
|
|
||||||
|
try {
|
||||||
|
em.remove(estado);
|
||||||
|
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,11 @@ package xyz.danielcortes.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceException;
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
import xyz.danielcortes.framework.PersistenceManager;
|
import xyz.danielcortes.framework.PersistenceManager;
|
||||||
import xyz.danielcortes.models.Idioma;
|
import xyz.danielcortes.models.Idioma;
|
||||||
|
|
||||||
@@ -21,21 +25,49 @@ public class IdiomaRepository {
|
|||||||
return idiomas;
|
return idiomas;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Idioma> search(String term) {
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Idioma> query = cb.createQuery(Idioma.class);
|
||||||
|
Root<Idioma> r = query.from(Idioma.class);
|
||||||
|
query.where(
|
||||||
|
cb.like(cb.lower(r.get("nombre")), "%" + term.toLowerCase() + "%")
|
||||||
|
);
|
||||||
|
return em.createQuery(query).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
public void save(Idioma idioma) {
|
public void save(Idioma idioma) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(idioma);
|
|
||||||
|
try {
|
||||||
|
em.persist(idioma);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Idioma idioma) {
|
public void update(Idioma idioma) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.merge(idioma);
|
|
||||||
|
try {
|
||||||
|
em.merge(idioma);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Idioma idioma) {
|
public void delete(Idioma idioma) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.remove(idioma);
|
|
||||||
|
try {
|
||||||
|
em.remove(idioma);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,12 @@ package xyz.danielcortes.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.PersistenceException;
|
||||||
import javax.persistence.Query;
|
import javax.persistence.Query;
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
import xyz.danielcortes.framework.PersistenceManager;
|
import xyz.danielcortes.framework.PersistenceManager;
|
||||||
import xyz.danielcortes.models.Libro;
|
import xyz.danielcortes.models.Libro;
|
||||||
|
|
||||||
@@ -20,28 +24,57 @@ public class LibroRepository {
|
|||||||
return query.getResultList();
|
return query.getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<Libro> search(String term) {
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<Libro> query = cb.createQuery(Libro.class);
|
||||||
|
Root<Libro> r = query.from(Libro.class);
|
||||||
|
query.where(
|
||||||
|
cb.like(cb.lower(r.get("nombre")), "%" + term.toLowerCase() + "%")
|
||||||
|
);
|
||||||
|
return em.createQuery(query).getResultList();
|
||||||
|
}
|
||||||
|
|
||||||
public void save(Libro libro) {
|
public void save(Libro libro) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.persist(libro);
|
|
||||||
|
try {
|
||||||
|
em.persist(libro);
|
||||||
|
} catch (
|
||||||
|
PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(Libro libro) {
|
public void update(Libro libro) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.merge(libro);
|
|
||||||
|
try {
|
||||||
|
em.merge(libro);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(Libro libro) {
|
public void delete(Libro libro) {
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
em.remove(libro);
|
|
||||||
|
try {
|
||||||
|
em.remove(libro);
|
||||||
|
} catch (PersistenceException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
em.getTransaction().commit();
|
em.getTransaction().commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean exists(Integer serie, Integer id) {
|
public boolean exists(Integer serie, Integer id) {
|
||||||
Query query = em.createQuery("SELECT count(l) FROM Libro l WHERE serie = :serie and id != :id");
|
Query query = em.createQuery("SELECT count(l) FROM Libro l WHERE serie = :serie and id != :id");
|
||||||
query.setParameter("serie", serie);
|
query.setParameter("serie", serie);
|
||||||
query.setParameter("id", id);
|
query.setParameter("id", id);
|
||||||
return (Long) query.getResultList().get(0) == 1;
|
return (Long) query.getResultList().get(0) == 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user