com.humber.java.dao.AlbumDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.humber.java.dao.AlbumDAO.java

Source

/*
 * 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.Album;
import com.humber.java.HibernateUtil;
import org.hibernate.CacheMode;

public class AlbumDAO {
    public void addAlbum(Album album) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.save(album);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public void deleteAlbum(int albumId) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            Album album = (Album) session.load(Album.class, new Integer(albumId));
            session.delete(album);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public void updateAlbum(Album album) {
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            session.update(album);
            session.getTransaction().commit();
        } catch (RuntimeException e) {
            if (trns != null) {
                trns.rollback();
            }
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
    }

    public List<Album> getAllAlbums() {
        List<Album> albums = new ArrayList<Album>();
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            albums = session.createQuery("from Album").list();
            session.setCacheMode(CacheMode.IGNORE);
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.close();
        }
        return albums;
    }

    public Album getAlbumById(int albumId) {
        Album album = null;
        Transaction trns = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            trns = session.beginTransaction();
            String queryString = "from Album where id = :id";
            Query query = session.createQuery(queryString);
            query.setInteger("id", albumId);
            album = (Album) query.uniqueResult();
        } catch (RuntimeException e) {
            e.printStackTrace();
        } finally {
            session.clear();
            session.flush();
            session.close();
        }
        return album;
    }
}