<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="DeliveryPU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/bd_delivery"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
/*
* 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 net.g3infotech.delivery.dao;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import net.g3infotech.delivery.util.DeliveryUtil;
/**
*
* @author G3
*/
public class GenericDAO<T extends Serializable> {
private Class<T> aClass;
protected GenericDAO(Class<T> aClass){
this.aClass = aClass;
}
public long count(String jpql, Object... params){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
Query q = entityManager.createQuery(jpql);
for(int i = 0; i < params.length; i++){
q.setParameter(i+1, params[i]);
}
long valor = (long) q.getSingleResult();
entityManager.getTransaction().commit();
entityManager.close();
return valor;
}
protected EntityManager getEntityManager(){
return DeliveryUtil.getInstance().getEntityManager();
}
public void save(T entity){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(entity);
entityManager.getTransaction().commit();
entityManager.close();
}
public void update(T entity){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
entityManager.merge(entity);
entityManager.getTransaction().commit();
entityManager.close();
}
public void delete(Integer id){
EntityManager manager = getEntityManager();
manager.getTransaction().begin();
manager.remove(manager.getReference(aClass, id));
manager.getTransaction().commit();
manager.close();
}
public List<T> findAll(){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
Query q = entityManager.createQuery("from "+aClass.getSimpleName());
List<T> entities = q.getResultList();
entityManager.getTransaction().commit();
entityManager.close();
return entities;
}
public List<T> find(String jpql, Object... params){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
Query q = entityManager.createQuery(jpql);
for(int i = 0; i<params.length; i++){
q.setParameter(i+1, params[i]);
}
List<T> entities = q.getResultList();
entityManager.getTransaction().commit();
entityManager.close();
return entities;
}
public List<T> find(String jpql){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
Query q = entityManager.createQuery(jpql);
List<T> entities = q.getResultList();
entityManager.getTransaction().commit();
entityManager.close();
return entities;
}
@SuppressWarnings("unchecked")
public T findOne(String jpql, Object... params){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
Query q = entityManager.createQuery(jpql);
for(int i = 0; i < params.length; i++){
q.setParameter(i+1, params[i]);
}
T entity = (T) q.getSingleResult();
entityManager.getTransaction().commit();
entityManager.close();
return entity;
}
public T findOne(String jpql){
EntityManager entityManager = getEntityManager();
entityManager.getTransaction().begin();
Query q = entityManager.createQuery(jpql);
T entities = (T) q.getSingleResult();
entityManager.getTransaction().commit();
entityManager.close();
return entities;
}
}
public class MesaDAO extends GenericDAO<Mesa>{
public MesaDAO() {
super(Mesa.class);
}
}
@Entity
@Table(name = "TB_MESA")
public class Mesa implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID_MESA")
private int idMesa;
@Column(name = "STATUS")
private int status; //ocupada ou não
@Column(name = "DESCRICAO_MESA")
private String descricaoMesa;
public int getIdMesa() {
return idMesa;
}
public void setIdMesa(int idMesa) {
this.idMesa = idMesa;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getDescricaoMesa() {
return descricaoMesa;
}
public void setDescricaoMesa(String descricaoMesa) {
this.descricaoMesa = descricaoMesa;
}
@Override
public int hashCode() {
int hash = 7;
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Mesa other = (Mesa) obj;
if (this.idMesa != other.idMesa) {
return false;
}
return true;
}
@Override
public String toString() {
return "Mesa{" + "idMesa=" + idMesa + ", status=" + status + ", descricaoMesa=" + descricaoMesa + '}';
}
}