com.askme.dao.QuestionDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.askme.dao.QuestionDAOImpl.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.askme.dao;

import com.askme.model.Question;
import java.util.List;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Admin
 */
@Repository
public class QuestionDAOImpl implements QuestionDAO {

    @Autowired
    private SessionFactory sessionFactory;

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Question").list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findLast(int first, int max) {
        return sessionFactory.getCurrentSession().createQuery("from Question order by id desc")
                .setFirstResult(first - 1).setMaxResults(max).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findTopViews(int first, int max) {
        return sessionFactory.getCurrentSession().createQuery("from Question order by views desc")
                .setFirstResult(first - 1).setMaxResults(max).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findNoAnswers(int first, int max) {
        return sessionFactory.getCurrentSession().createCriteria(Question.class)
                .add(Restrictions.isEmpty("answers")).addOrder(Order.desc("id")).setFirstResult(first - 1)
                .setMaxResults(max).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findAllByCategory(int cid) {
        return sessionFactory.getCurrentSession()
                .createQuery("from Question where category.id = :cid order by id desc").setParameter("cid", cid)
                .list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findAllByTag(String name) {
        return sessionFactory.getCurrentSession()
                .createQuery("select q from Question q join q.tags t where t.name = :name order by q.id desc")
                .setParameter("name", name).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findByCategory(int cid, int first, int max) {
        return sessionFactory.getCurrentSession()
                .createQuery("from Question where category.id = :cid order by id desc").setParameter("cid", cid)
                .setFirstResult(first - 1).setMaxResults(max).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> findByTag(String name, int first, int max) {
        return sessionFactory.getCurrentSession()
                .createQuery("select q from Question q join q.tags t where t.name = :name order by q.id desc")
                .setParameter("name", name).setFirstResult(first - 1).setMaxResults(max).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> search(String keyword, int first, int max) {
        return sessionFactory.getCurrentSession().createQuery("from Question where title like :keyword")
                .setParameter("keyword", "%" + keyword + "%").setFirstResult(first - 1).setMaxResults(max).list();
    }

    @SuppressWarnings("unchecked")
    @Override
    public Question findById(int id) {
        return (Question) sessionFactory.getCurrentSession().get(Question.class, id);
    }

    @Override
    public Long countAll() {
        return (Long) sessionFactory.getCurrentSession().createCriteria(Question.class)
                .setProjection(Projections.rowCount()).uniqueResult();
    }

    @Override
    public void save(Question question) {
        sessionFactory.getCurrentSession().save(question);
    }

    @Override
    public void upViews(Question question) {
        sessionFactory.getCurrentSession()
                .createQuery("update Question q set q.views = q.views + 1 where q.id = :id")
                .setParameter("id", question.getId()).executeUpdate();
    }

    @Override
    public void delete(Question question) {
        sessionFactory.getCurrentSession().delete(question);
    }

}