Java tutorial
/* * GeoSDI ERA - The new era of webGIS * http://code.google.com/p/geosdiera/ * ==================================================================== * * Copyright (C) 2008-2009 GeoSDI Group (CNR IMAA). * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. * * ==================================================================== * * This software consists of voluntary contributions made by developers * of GeoSDI Group. For more information on GeoSDI, please see * <http://www.geosdi.org/>. * */ package it.geosdi.era.server.dao.hibernate; import it.geosdi.era.client.model.UtenteProgetto; import it.geosdi.era.exception.DAOException; import it.geosdi.era.server.dao.IDAOUtenteProgetto; import java.util.List; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.LockMode; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.criterion.Criterion; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; public class DAOUtenteProgettoHibernate implements IDAOUtenteProgetto { private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } private Session getSession() { return this.sessionFactory.getCurrentSession(); } @Transactional(propagation = Propagation.REQUIRED) public UtenteProgetto makePersistent(UtenteProgetto utenteProgetto) { try { getSession().saveOrUpdate(utenteProgetto); } catch (HibernateException ex) { throw new DAOException(ex); } return utenteProgetto; } public List<UtenteProgetto> findAll() throws DAOException { return findByCriteria(); } public List<UtenteProgetto> findAll(int offset, int limite) throws DAOException { return findByCriteria(offset, limite); } @SuppressWarnings("unchecked") public List<UtenteProgetto> findByCriteria(Criterion... criterion) throws DAOException { try { Criteria crit = getSession().createCriteria(UtenteProgetto.class); for (Criterion c : criterion) { crit.add(c); } return crit.list(); } catch (HibernateException ex) { throw new DAOException(ex); } } @SuppressWarnings("unchecked") public List<UtenteProgetto> findByCriteria(int offset, int limite, Criterion... criterion) throws DAOException { try { Criteria crit = getSession().createCriteria(UtenteProgetto.class); for (Criterion c : criterion) { crit.add(c); } crit.setFirstResult(offset); crit.setMaxResults(limite); return crit.list(); } catch (HibernateException ex) { throw new DAOException(ex); } } public UtenteProgetto findById(Long id, boolean lock) throws DAOException { UtenteProgetto entity; try { if (lock) { entity = (UtenteProgetto) getSession().load(UtenteProgetto.class, id, LockMode.UPGRADE); } else { entity = (UtenteProgetto) getSession().load(UtenteProgetto.class, id); } } catch (HibernateException ex) { throw new DAOException(ex); } return entity; } public void lock(UtenteProgetto entity) throws DAOException { try { getSession().lock(entity, LockMode.UPGRADE); } catch (HibernateException ex) { throw new DAOException(ex); } } public void makeTransient(UtenteProgetto entity) throws DAOException { // TODO Auto-generated method stub } }