Java tutorial
/** * openkm, Open Document Management System (http://www.openkm.com) * Copyright (c) 2006-2013 Paco Avila & Josep Llort * * No bytes were intentionally harmed during the development of this application. * * 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; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ package com.ikon.dao.extension; import org.hibernate.HibernateException; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.ikon.core.DatabaseException; import com.ikon.dao.HibernateUtil; import com.ikon.dao.bean.extension.DropboxToken; /** * DropboxDAO * * @author jllort * */ public class DropboxDAO { private static Logger log = LoggerFactory.getLogger(DropboxDAO.class); private static DropboxDAO single = new DropboxDAO(); private DropboxDAO() { } public static DropboxDAO getInstance() { return single; } /** * Create */ public void create(DropboxToken dbt) throws DatabaseException { log.debug("create({})", dbt); Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); session.save(dbt); HibernateUtil.commit(tx); log.debug("create: {}"); } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } } /** * Find by pk */ public DropboxToken findByPk(String usrId) throws DatabaseException { log.debug("findByPk({})", usrId); String qs = "from DropboxToken dbt where dbt.user=:user"; Session session = null; Transaction tx = null; try { session = HibernateUtil.getSessionFactory().openSession(); tx = session.beginTransaction(); Query q = session.createQuery(qs); q.setString("user", usrId); DropboxToken ret = (DropboxToken) q.setMaxResults(1).uniqueResult(); HibernateUtil.commit(tx); log.debug("findByPk: {}", ret); return ret; } catch (HibernateException e) { HibernateUtil.rollback(tx); throw new DatabaseException(e.getMessage(), e); } finally { HibernateUtil.close(session); } } }