persistence.BranchDao.java Source code

Java tutorial

Introduction

Here is the source code for persistence.BranchDao.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 persistence;

import entity.Branch;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import persistence.parent.Dao;
import support.StringAdapter;

/**
 *
 * @author Rice Pavel
 */
@Repository("branchDao")
public class BranchDao extends Dao<Branch> {

    public Class getSupportedClass() {
        return Branch.class;
    }

    public List<Branch> getList(Set<Long> ids) {
        if (!ids.isEmpty()) {
            Criteria crit = currentSession().createCriteria(Branch.class);
            crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
            if (!ids.isEmpty()) {
                crit.add(Restrictions.in("branchId", ids));
            }
            return crit.list();
        } else {
            return new ArrayList<Branch>();
        }
    }

    /*
     public List<Branch> getList(Set<Long> ids) {
     Query q = currentSession().createQuery("from Branch where branchId in :ids");
     if (!ids.isEmpty()) {
     q.setParameterList("ids", ids);
     }
     return q.list();
     }
     */
    public List<Branch> search(String name, String freeAuthors, String abbrevation) {
        Criteria crit = currentSession().createCriteria(Branch.class);
        if (StringAdapter.NotNull(name)) {
            crit.add(Restrictions.like("name", "%" + name + "%"));
        }
        if (StringAdapter.NotNull(abbrevation)) {
            crit.add(Restrictions.like("abbrevation", "%" + abbrevation + "%"));
        }
        if (StringAdapter.NotNull(freeAuthors)) {
            Boolean bool = Boolean.parseBoolean(freeAuthors);
            crit.add(Restrictions.eq("freeAuthors", bool));
        }
        crit.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        return crit.list();
    }

}