1 package org.hibernate.ce.auction.dao; 2 3 import org.hibernate.*; 4 import org.hibernate.criterion.*; 5 import org.hibernate.ce.auction.exceptions.InfrastructureException; 6 import org.hibernate.ce.auction.model.Category; 7 import org.hibernate.ce.auction.persistence.HibernateUtil; 8 9 import java.util.Collection ; 10 11 16 public class CategoryDAO { 17 18 public CategoryDAO() { 19 HibernateUtil.beginTransaction(); 20 } 21 22 24 public Category getCategoryById(Long categoryId, boolean lock) 25 throws InfrastructureException { 26 27 Session session = HibernateUtil.getSession(); 28 Category cat = null; 29 try { 30 if (lock) { 31 cat = (Category) session.load(Category.class, categoryId, LockMode.UPGRADE); 32 } else { 33 cat = (Category) session.load(Category.class, categoryId); 34 } 35 } catch (HibernateException ex) { 36 throw new InfrastructureException(ex); 37 } 38 return cat; 39 } 40 41 43 public Collection findAll(boolean onlyRootCategories) 44 throws InfrastructureException { 45 46 Collection categories; 47 try { 48 if (onlyRootCategories) { 49 Criteria crit = HibernateUtil.getSession().createCriteria(Category.class); 50 categories = crit.add(Expression.isNull("parentCategory")).list(); 51 } else { 52 categories = HibernateUtil.getSession().createCriteria(Category.class).list(); 53 } 54 } catch (HibernateException ex) { 55 throw new InfrastructureException(ex); 56 } 57 return categories; 58 } 59 60 62 public Collection findByExample(Category exampleCategory) 63 throws InfrastructureException { 64 65 Collection categories; 66 try { 67 Criteria crit = HibernateUtil.getSession().createCriteria(Category.class); 68 categories = crit.add(Example.create(exampleCategory)).list(); 69 } catch (HibernateException ex) { 70 throw new InfrastructureException(ex); 71 } 72 return categories; 73 } 74 75 77 public void makePersistent(Category category) 78 throws InfrastructureException { 79 80 try { 81 HibernateUtil.getSession().saveOrUpdate(category); 82 } catch (HibernateException ex) { 83 throw new InfrastructureException(ex); 84 } 85 } 86 87 89 public void makeTransient(Category category) 90 throws InfrastructureException { 91 92 try { 93 HibernateUtil.getSession().delete(category); 94 } catch (HibernateException ex) { 95 throw new InfrastructureException(ex); 96 } 97 } 98 99 } 100 | Popular Tags |