Java tutorial
/* * 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 co.utb.softeng.contactsapp.dao.impl; import co.utb.softeng.contactsapp.entities.Cita; import co.utb.softeng.contactsapp.entities.Contact; import java.util.List; import org.hibernate.Criteria; import org.hibernate.FetchMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.hibernate.criterion.DetachedCriteria; import org.hibernate.criterion.MatchMode; import org.hibernate.criterion.Projections; import org.hibernate.criterion.Restrictions; import org.hibernate.criterion.Subqueries; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import co.utb.softeng.contactsapp.dao.CitaDAO; /** * * @author William */ @Repository public class CitaDAOImpl implements CitaDAO { @Autowired SessionFactory sessionFactory; private Session getSession() { return sessionFactory.getCurrentSession(); } @Override public List<Cita> getAllCitas(boolean includeContacts) { Criteria crit = getSession().createCriteria(Cita.class); if (includeContacts) { crit = crit.setFetchMode("contacts", FetchMode.JOIN); } return crit.list(); } @Override public Cita getById(Long id) { return (Cita) getSession().createCriteria(Cita.class).add(Restrictions.idEq(id)) .setFetchMode("contacts", FetchMode.JOIN).uniqueResult(); } @Override public List<Cita> getByName(String name) { return getSession().createCriteria(Cita.class).add(Restrictions.ilike("name", name)) .setFetchMode("contacts", FetchMode.JOIN).list(); } public List<Cita> getByContactsId(Long contactId) { return getSession().createCriteria(Cita.class).createCriteria("contacts").add(Restrictions.idEq(contactId)) .list(); } @Override public List<Cita> getByContactName(String contactName) { DetachedCriteria subQuery = DetachedCriteria.forClass(Contact.class).createCriteria("citas") .add(Restrictions.ilike("name", contactName, MatchMode.ANYWHERE)).setProjection(Projections.id()); Criteria mainCriteria = getSession().createCriteria(Cita.class).add(Subqueries.propertyIn("id", subQuery)) .setFetchMode("contacts", FetchMode.JOIN); return mainCriteria.list(); } @Override public void saveOrUpdate(Cita cita) { getSession().saveOrUpdate(cita); } @Override public void delete(Long id) { Cita cita = new Cita(); cita.setId(id); getSession().delete(cita); } @Override public List<Cita> getByContactId(Long contactId) { throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. } }