Java tutorial
/* * 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.fe.daos; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import model.Film; import org.hibernate.Criteria; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.Transaction; import org.hibernate.criterion.Restrictions; import util.HibernateUtil; /** * * @author <Eduardo at EBS> */ public class DAOFilm { private Session session; private Transaction transaction; public boolean initTransaction() { boolean rs = false; try { session = HibernateUtil.getSession(); transaction = session.beginTransaction(); rs = true; } catch (HibernateException e1) { System.out.println("Hibernate Exception on DAOFilm>iniTransaction() " + e1.getMessage()); } catch (Exception e2) { e2.printStackTrace(System.err); } return rs; } /** * Get a film register from database * @param idFilm * @return */ public Film loadFilm(int idFilm) { Film tmp = null; short id = (short) idFilm; try { initTransaction(); tmp = (Film) session.get(Film.class, id); transaction.commit(); } catch (HibernateException ex1) { System.out.println("HibernateException" + ex1.getMessage()); } finally { HibernateUtil.close(session); } return tmp; } /** * Busca todas la grabaciones que coincidan con un length especifico * @param leng * @return */ public ArrayList<Film> loadFilms(int leng) { ArrayList<Film> tmp = new ArrayList(); short tt = (short) leng; try { initTransaction(); Criteria cr = session.createCriteria(Film.class); cr.add(Restrictions.eq("length", tt));//Defines a restriction equal type to compare only one property for ids //cr.addOrder(Order.asc("fechaRegistro"));//To sort the results ascendent or descendent cr.setMaxResults(10);//Max results tmp = (ArrayList) cr.list();//No puede ser convertido a LinkedList, solo a ArrayList transaction.commit(); } catch (HibernateException ex) { System.out.println("HibernateException on loadFilms()"); ex.printStackTrace(); } finally { HibernateUtil.close(session); } return tmp; } /** * Busca todas las grabaciones en un rango de length * @param init * @param end * @return */ public ArrayList<Film> loadFilmsByRange(int init, int end) { ArrayList<Film> tmp = new ArrayList();//Temporal Array to storage the results short s_init = (short) init;//TO sconsider the lower number short s_end = (short) end; try { initTransaction(); Criteria cr = session.createCriteria(Film.class);//Define the type of the class, if you define the class you //don't need specify the context class cr.add(Restrictions.between("length", s_init, s_end));//Defines a restriction between acording the property cr.setMaxResults(100); tmp = (ArrayList) cr.list(); transaction.commit();//does flush the session, but it also ends the unit of work. } catch (HibernateException ex1) { System.out.println("HibernateEx on loadFilmsByRange()"); ex1.printStackTrace(); } finally { HibernateUtil.close(session); } return tmp; } /** * Busca todos los films deacuerdo a una categoria * @param idCategory * @return */ public ArrayList<Film> loadFilmsByCategory(int idCategory) { short s_idCategory = (short) idCategory; ArrayList<Film> tmp = new ArrayList(); try { initTransaction(); Criteria cr = session.createCriteria(Film.class); //Conjuntion->logic and //Disjuntion->logic or //cr.add(Restrictions.) //Do a nested query //Saw the pdf file tmp = (ArrayList) cr.list(); } catch (HibernateException ex1) { System.out.println("HibernateException on loadFilmByCategory()"); ex1.printStackTrace(); } finally { HibernateUtil.close(session); } return tmp; } public boolean saveNewFilm(Film newFilm) { boolean result = false; try { initTransaction(); session.save(newFilm); transaction.commit(); result = true; } catch (HibernateException he) { System.out.println("HibernateException on saveNewFilm"); he.printStackTrace(); } finally { HibernateUtil.close(session); } return result; } }