/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package dao_impl;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import dao.RatingDAO;
import dao.UserDAO;
import pojo.Product;
import pojo.Rating;
import pojo.User;
import util.HibernateUtil;
/**
*
* @author phuc
*/
public class RatingDAOImpl implements RatingDAO {
private static final Logger logger = Logger.getLogger(RatingDAOImpl.class);
// get all list rating from database
public List<Rating> getListRating() {
logger.debug("getListRating start");
List<Rating> ds = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
String hql = "from Rating";
Query query = session.createQuery(hql);
ds = query.list();
logger.debug("getListRating success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getListRating error : " + ex);
} finally {
session.close();
}
logger.debug("getListRating end");
return ds;
}
// get info of a rating by id
public Rating getInfoRating(int id) {
logger.debug("getInfoRating start");
Rating sp = null;
Session session = HibernateUtil.getSessionFactory().openSession();
try {
sp = (Rating) session.get(Rating.class, id);
logger.debug("getInfoRating success");
} catch (HibernateException ex) {
System.err.println(ex);
logger.error("getInfoRating error : " + ex);
} finally {
session.close();
}
logger.debug("getInfoRating end");
return sp;
}
// add arating to database
public boolean addRating(Rating sp) {
logger.debug("addRating start");
Session session = HibernateUtil.getSessionFactory().openSession();
RatingDAO khDAO = new RatingDAOImpl();
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.save(sp);
transaction.commit();
logger.debug("addRating success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("addRating error : " + ex);
} finally {
session.close();
}
logger.debug("addRating end");
return true;
}
// delete a rating by id
public boolean delRating(int id) {
logger.debug("delRating start");
Session session = HibernateUtil.getSessionFactory().openSession();
Rating u = (Rating) session.get(Rating.class, id);
Transaction transaction = null;
try {
transaction = session.beginTransaction();
session.delete(u);
transaction.commit();
logger.debug("delRating success");
} catch (HibernateException ex) {
transaction.rollback();
System.err.println(ex);
logger.error("delRating error : " + ex);
} finally {
session.close();
}
logger.debug("delRating end");
return true;
}
}