Java tutorial
/** * * @author DS */ /* * 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 com.humber.java.dao; /** * * @author DS */ import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.Transaction; import com.humber.java.model.Picture; import com.humber.java.HibernateUtil; public class ImageDAO { public void addImage(Picture image) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.save(image); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void deleteImage(int imageId) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); Picture image = (Picture) session.load(Picture.class, new Integer(imageId)); session.delete(image); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public void updateImage(Picture image) { Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); session.update(image); session.getTransaction().commit(); } catch (RuntimeException e) { if (trns != null) { trns.rollback(); } e.printStackTrace(); } finally { session.flush(); session.close(); } } public List<Picture> getAllImages() { List<Picture> images = new ArrayList<Picture>(); Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); images = session.createQuery("from Image").list(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return images; } public Picture getImageById(int imageId) { Picture image = null; Transaction trns = null; Session session = HibernateUtil.getSessionFactory().openSession(); try { trns = session.beginTransaction(); String queryString = "from Image where id = :id"; Query query = session.createQuery(queryString); query.setInteger("id", imageId); image = (Picture) query.uniqueResult(); } catch (RuntimeException e) { e.printStackTrace(); } finally { session.flush(); session.close(); } return image; } }