Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/02/2013, 13:27
ARAPSPGON
 
Fecha de Ingreso: octubre-2007
Mensajes: 57
Antigüedad: 18 años, 11 meses
Puntos: 0
Respuesta: JPA Controladores de Entidades Relacionadas

También genero las clases controller de las clases entidad con netbeans.. que lo hace al vuelo

Clase DepartamentosJpaController

Código PHP:
package Controladores;

import Controladores.exceptions.IllegalOrphanException;
import Controladores.exceptions.NonexistentEntityException;
import Entidades.Departamentos;
import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import Entidades.Empleados;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;


public class 
DepartamentosJpaController implements Serializable {

    public 
DepartamentosJpaController(EntityManagerFactory emf) {
        
this.emf = emf;
    }
    private 
EntityManagerFactory emf = null;

    public 
EntityManager getEntityManager() {
        return 
emf.createEntityManager();
    }

    public 
void create(Departamentos departamentos) {
        if (
departamentos.getEmpleadosCollection() == null) {
            
departamentos.setEmpleadosCollection(new ArrayList<Empleados>());
        }
        
EntityManager em = null;
        try {
            
em = getEntityManager();
            
em.getTransaction().begin();
            
Collection<Empleados> attachedEmpleadosCollection = new ArrayList<Empleados>();
            for (
Empleados empleadosCollectionEmpleadosToAttach : departamentos.getEmpleadosCollection()) {
                
empleadosCollectionEmpleadosToAttach = em.getReference(empleadosCollectionEmpleadosToAttach.getClass(), empleadosCollectionEmpleadosToAttach.getDni());
                
attachedEmpleadosCollection.add(empleadosCollectionEmpleadosToAttach);
            }
            
departamentos.setEmpleadosCollection(attachedEmpleadosCollection);
            
em.persist(departamentos);
            for (
Empleados empleadosCollectionEmpleados : departamentos.getEmpleadosCollection()) {
                
Departamentos oldIddepOfEmpleadosCollectionEmpleados = empleadosCollectionEmpleados.getIddep();
                
empleadosCollectionEmpleados.setIddep(departamentos);
                
empleadosCollectionEmpleados = em.merge(empleadosCollectionEmpleados);
                if (
oldIddepOfEmpleadosCollectionEmpleados != null) {
                    
oldIddepOfEmpleadosCollectionEmpleados.getEmpleadosCollection().remove(empleadosCollectionEmpleados);
                    
oldIddepOfEmpleadosCollectionEmpleados = em.merge(oldIddepOfEmpleadosCollectionEmpleados);
                }
            }
            
em.getTransaction().commit();
        } 
finally {
            if (
em != null) {
                
em.close();
            }
        }
    }

    public 
void edit(Departamentos departamentos) throws IllegalOrphanException, NonexistentEntityException, Exception {
        
EntityManager em = null;
        try {
            
em = getEntityManager();
            
em.getTransaction().begin();
            
Departamentos persistentDepartamentos = em.find(Departamentos.class, departamentos.getIddep());
            
Collection<Empleados> empleadosCollectionOld = persistentDepartamentos.getEmpleadosCollection();
            
Collection<Empleados> empleadosCollectionNew = departamentos.getEmpleadosCollection();
            List<
String> illegalOrphanMessages = null;
            for (
Empleados empleadosCollectionOldEmpleados : empleadosCollectionOld) {
                if (!
empleadosCollectionNew.contains(empleadosCollectionOldEmpleados)) {
                    if (
illegalOrphanMessages == null) {
                        
illegalOrphanMessages = new ArrayList<String>();
                    }
                    
illegalOrphanMessages.add("You must retain Empleados " + empleadosCollectionOldEmpleados + " since its iddep field is not nullable.");
                }
            }
            if (
illegalOrphanMessages != null) {
                throw new 
IllegalOrphanException(illegalOrphanMessages);
            }
            
Collection<Empleados> attachedEmpleadosCollectionNew = new ArrayList<Empleados>();
            for (
Empleados empleadosCollectionNewEmpleadosToAttach : empleadosCollectionNew) {
                
empleadosCollectionNewEmpleadosToAttach = em.getReference(empleadosCollectionNewEmpleadosToAttach.getClass(), empleadosCollectionNewEmpleadosToAttach.getDni());
                
attachedEmpleadosCollectionNew.add(empleadosCollectionNewEmpleadosToAttach);
            }
            
empleadosCollectionNew = attachedEmpleadosCollectionNew;
            
departamentos.setEmpleadosCollection(empleadosCollectionNew);
            
departamentos = em.merge(departamentos);
            for (
Empleados empleadosCollectionNewEmpleados : empleadosCollectionNew) {
                if (!
empleadosCollectionOld.contains(empleadosCollectionNewEmpleados)) {
                    
Departamentos oldIddepOfEmpleadosCollectionNewEmpleados = empleadosCollectionNewEmpleados.getIddep();
                    
empleadosCollectionNewEmpleados.setIddep(departamentos);
                    
empleadosCollectionNewEmpleados = em.merge(empleadosCollectionNewEmpleados);
                    if (
oldIddepOfEmpleadosCollectionNewEmpleados != null && !oldIddepOfEmpleadosCollectionNewEmpleados.equals(departamentos)) {
                        
oldIddepOfEmpleadosCollectionNewEmpleados.getEmpleadosCollection().remove(empleadosCollectionNewEmpleados);
                        
oldIddepOfEmpleadosCollectionNewEmpleados = em.merge(oldIddepOfEmpleadosCollectionNewEmpleados);
                    }
                }
            }
            
em.getTransaction().commit();
        } catch (
Exception ex) {
            
String msg = ex.getLocalizedMessage();
            if (
msg == null || msg.length() == 0) {
                
Integer id = departamentos.getIddep();
                if (
findDepartamentos(id) == null) {
                    throw new 
NonexistentEntityException("The departamentos with id " + id + " no longer exists.");
                }
            }
            throw 
ex;
        } 
finally {
            if (
em != null) {
                
em.close();
            }
        }
    }

    public 
void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        
EntityManager em = null;
        try {
            
em = getEntityManager();
            
em.getTransaction().begin();
            
Departamentos departamentos;
            try {
                
departamentos = em.getReference(Departamentos.class, id);
                
departamentos.getIddep();
            } catch (
EntityNotFoundException enfe) {
                throw new 
NonexistentEntityException("The departamentos with id " + id + " no longer exists.", enfe);
            }
            List<
String> illegalOrphanMessages = null;
            
Collection<Empleados> empleadosCollectionOrphanCheck = departamentos.getEmpleadosCollection();
            for (
Empleados empleadosCollectionOrphanCheckEmpleados : empleadosCollectionOrphanCheck) {
                if (
illegalOrphanMessages == null) {
                    
illegalOrphanMessages = new ArrayList<String>();
                }
                
illegalOrphanMessages.add("This Departamentos (" + departamentos + ") cannot be destroyed since the Empleados " + empleadosCollectionOrphanCheckEmpleados + " in its empleadosCollection field has a non-nullable iddep field.");
            }
            if (
illegalOrphanMessages != null) {
                throw new 
IllegalOrphanException(illegalOrphanMessages);
            }
            
em.remove(departamentos);
            
em.getTransaction().commit();
        } 
finally {
            if (
em != null) {
                
em.close();
            }
        }
    }

    public List<
Departamentos> findDepartamentosEntities() {
        return 
findDepartamentosEntities(true, -1, -1);
    }

    public List<
Departamentos> findDepartamentosEntities(int maxResults, int firstResult) {
        return 
findDepartamentosEntities(false, maxResults, firstResult);
    }

    private List<
Departamentos> findDepartamentosEntities(boolean all, int maxResults, int firstResult) {
        
EntityManager em = getEntityManager();
        try {
            
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            
cq.select(cq.from(Departamentos.class));
            
Query q = em.createQuery(cq);
            if (!
all) {
                
q.setMaxResults(maxResults);
                
q.setFirstResult(firstResult);
            }
            return 
q.getResultList();
        } 
finally {
            
em.close();
        }
    }

    public 
Departamentos findDepartamentos(Integer id) {
        
EntityManager em = getEntityManager();
        try {
            return 
em.find(Departamentos.class, id);
        } 
finally {
            
em.close();
        }
    }

    public 
int getDepartamentosCount() {
        
EntityManager em = getEntityManager();
        try {
            
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            
Root<Departamentos> rt = cq.from(Departamentos.class);
            
cq.select(em.getCriteriaBuilder().count(rt));
            
Query q = em.createQuery(cq);
            return ((
Long) q.getSingleResult()).intValue();
        } 
finally {
            
em.close();
        }
    }
    
}