Created
July 6, 2015 15:38
-
-
Save yogonza524/025f9152d7ca19c18274 to your computer and use it in GitHub Desktop.
Implementation of AbstractController like must to be
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.blh.habilitacion.controllers; | |
import java.util.ArrayList; | |
import java.util.Iterator; | |
import org.blh.habilitacion.entities.Centros; | |
import org.blh.habilitacion.util.HibernateUtil; | |
import org.hibernate.Hibernate; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
import org.hibernate.criterion.Order; | |
/** | |
* | |
* @author Gonza | |
*/ | |
public class CentrosController extends AbstractController<Centros> { | |
public static Boolean add(String nombre, String domicilio, String telefono){ | |
Boolean resp = false; | |
try { | |
sf = HibernateUtil.getSessionFactory(); | |
Session s = sf.openSession(); | |
Transaction tx = s.beginTransaction(); | |
int idMax = 0; | |
Centros max = (Centros)s.createCriteria(Centros.class).addOrder(Order.desc("idCentro")).setMaxResults(1).uniqueResult(); | |
if(max != null){ | |
idMax = max.getIdCentro() + 1; | |
} | |
Centros p = new Centros(); | |
p.setIdCentro(idMax); | |
p.setNombreCentro(nombre); | |
p.setDomicilioCentro(domicilio); | |
p.setTelefonoCentro(telefono); | |
s.persist(p); | |
tx.commit(); | |
s.close(); | |
resp = true; | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return resp; | |
} | |
@Override | |
public Class className() { | |
return Centros.class; | |
} | |
/* | |
* This method is for init the proxys object, for example: if one object is defined by de developer | |
* and then will be used for componenet another object, that composite object must be initilizated | |
* or the virtual machina throws a Runtime Exception. In this use case the entity Centros have Consentimientos | |
* and Entregadebiberonesacentros like sets (One to many relationships in database). | |
*/ | |
@Override | |
public void initialize(Centros entity) { | |
Hibernate.initialize(entity.getEntregadebiberonacentros()); | |
Hibernate.initialize(entity.getConsentimientos()); | |
} | |
/* | |
* This method is used before the all method will be runed. Composite is a boolean variable that indicate | |
* to the father class that object is composite and must be inizializated | |
@Override | |
public void beforeAll(){ | |
super.composite = true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment