com.bookselling.dao.SubjectDaoImpl.java Source code

Java tutorial

Introduction

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

import com.bookselling.domain.Subject;
import com.bookselling.form.filter.SubjectFilterForm;
import com.bookselling.util.PaginationData;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Phan Phat
 */
@Repository
public class SubjectDaoImpl extends GenericDao<Subject> implements SubjectDao {

    @Override
    public Class<Subject> registeredClass() {
        return Subject.class;
    }

    @Override
    public PaginationData filter(SubjectFilterForm form, int first, int items) {
        Criteria criteria = getSession().createCriteria(Subject.class);

        //Get keyword
        String keyword = "%" + form.getKeyword() + "%";

        //Set up criteria
        criteria.add(Restrictions.like("name", keyword));

        //Get list
        Set<Subject> subjects = new LinkedHashSet<>(criteria.list());

        //pagination
        PaginationData paginationData = new PaginationData(
                (long) criteria.setProjection(Projections.rowCount()).uniqueResult(), items, first, subjects);

        return paginationData;
    }

}