001/* 002 * #%L 003 * Netarchivesuite - wayback 004 * %% 005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library, 006 * the National Library of France and the Austrian National Library. 007 * %% 008 * This program is free software: you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation, either version 2.1 of the 011 * License, or (at your option) any later version. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Lesser Public License for more details. 017 * 018 * You should have received a copy of the GNU General Lesser Public 019 * License along with this program. If not, see 020 * <http://www.gnu.org/licenses/lgpl-2.1.html>. 021 * #L% 022 */ 023package dk.netarkivet.wayback.indexer; 024 025import java.io.Serializable; 026import java.util.List; 027 028import org.hibernate.Criteria; 029import org.hibernate.Session; 030import org.hibernate.criterion.Criterion; 031 032/** 033 * An implementation of Generic DAO which is specialised for hibernate object stores. 034 * 035 * @param <T> The type of the persistent entity. 036 * @param <PK> The type of the primary key for the entity. 037 */ 038@SuppressWarnings({"unchecked"}) 039public class GenericHibernateDAO<T, PK extends Serializable> implements GenericDAO<T, PK> { 040 041 private Class<T> type; 042 043 /** 044 * Constructor for the class. 045 * 046 * @param type the type of the persistent entity managed by this class. 047 */ 048 public GenericHibernateDAO(Class<T> type) { 049 this.type = type; 050 } 051 052 @Override 053 public PK create(T o) { 054 Session sess = getSession(); 055 sess.beginTransaction(); 056 PK key = (PK) sess.save(o); 057 sess.getTransaction().commit(); 058 sess.close(); 059 return key; 060 } 061 062 @Override 063 public T read(PK id) { 064 Session sess = getSession(); 065 T result = (T) sess.get(type, id); 066 sess.close(); 067 return result; 068 } 069 070 @Override 071 public void update(T o) { 072 Session sess = getSession(); 073 sess.beginTransaction(); 074 sess.update(o); 075 sess.getTransaction().commit(); 076 sess.close(); 077 } 078 079 @Override 080 public void delete(T o) { 081 Session sess = getSession(); 082 sess.beginTransaction(); 083 sess.delete(o); 084 sess.getTransaction().commit(); 085 sess.close(); 086 } 087 088 /** 089 * Return a session object for managing instances of this class. 090 * 091 * @return the session. 092 */ 093 protected Session getSession() { 094 return HibernateUtil.getSession(); 095 } 096 097 /** 098 * Use this inside subclasses as a convenience method to find objects matching a given criterion. 099 * 100 * @param criterion the criteria to be matched. 101 * @return a list of objects matching the criterion. 102 */ 103 protected List<T> findByCriteria(Criterion... criterion) { 104 Criteria crit = getSession().createCriteria(type); 105 for (Criterion c : criterion) { 106 crit.add(c); 107 } 108 return crit.list(); 109 } 110}