com.muslim.family.dao.impl.QuestionDAOImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.muslim.family.dao.impl.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.muslim.family.dao.impl;

import com.muslim.family.dao.inft.QuestionDAO;
import com.muslim.family.hib.entity.Question_tbl;
import com.muslim.family.util.HibernateUtil;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Tariq
 */
@Repository
@Transactional
public class QuestionDAOImpl implements QuestionDAO {

    private HibernateUtil hibernateUtil;
    private SessionFactory sessionFactory;

    public QuestionDAOImpl() {
        System.out.println(" *** QuestionDAO Called *** ");
    }

    @Autowired
    public QuestionDAOImpl(HibernateUtil hibernateUtil, SessionFactory sessionFactory) {
        this.hibernateUtil = hibernateUtil;
        this.sessionFactory = sessionFactory;
    }

    @Override
    public long addQuestionDao(Question_tbl qs) {
        return (Long) hibernateUtil.create(qs);
    }

    @Override
    public List<Question_tbl> getAllQuestionsDao() {
        return hibernateUtil.fetchAll(Question_tbl.class);
    }

    public List<Question_tbl> getAllQuestionsByDateDao() {
        List<Question_tbl> result = hibernateUtil.fetchAll("FROM Question_tbl q ORDER BY q.date DESC");
        return result;
    }

    public List<Question_tbl> getAllUnansweredQuestionDao() {

        List<Question_tbl> result = hibernateUtil
                .fetchAll("FROM Question_tbl q where status='unanswered' ORDER BY q.date DESC");
        return result;
    }

    public List<Question_tbl> getPaginatedQuestionsDao(int start, int length) {

        Criteria cr = sessionFactory.getCurrentSession().createCriteria(Question_tbl.class)
                .setProjection(Projections.projectionList().add(Projections.property("id"), "id")
                        .add(Projections.property("subject"), "Subject")
                        .add(Projections.property("views"), "Views"))
                .setResultTransformer(Transformers.aliasToBean(Question_tbl.class));
        cr.setFirstResult(start);
        cr.setMaxResults(length);

        return cr.list();

    }

    public int getQuestionCountByLimitDao(int limit) {

        Query qry = sessionFactory.getCurrentSession().createQuery("from Question_tbl q ORDER BY q.date DESC");
        return qry.setMaxResults(limit).list().size();

    }

    public Question_tbl getAllQuestionByIdDao(long id) {

        Question_tbl result = hibernateUtil.fetchById(id, Question_tbl.class);
        return result;

    }
}