Java tutorial
/* * 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.Tag; import java.util.List; import org.hibernate.SessionFactory; import org.hibernate.criterion.Projections; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * * @author Admin */ @Repository public class TagDAOImpl implements TagDAO { @Autowired private SessionFactory sessionFactory; @SuppressWarnings("unchecked") @Override public List<Tag> findAll() { List<Tag> tags = sessionFactory.getCurrentSession().createQuery("from Tag").list(); for (Tag tag : tags) { long questionsSize = (Long) sessionFactory.getCurrentSession() .createFilter(tag.getQuestions(), "select count(*)").uniqueResult(); tag.setQuestionsSize(questionsSize); } return tags; } @SuppressWarnings("unchecked") @Override public Tag findById(int id) { return (Tag) sessionFactory.getCurrentSession().get(Tag.class, id); } @SuppressWarnings("unchecked") @Override public Tag findByName(String name) { List<Tag> tags = sessionFactory.getCurrentSession().createQuery("from Tag where name = :name") .setParameter("name", name).list(); return (tags.isEmpty()) ? null : tags.get(0); } @Override public Long countAll() { return (Long) sessionFactory.getCurrentSession().createCriteria(Tag.class) .setProjection(Projections.rowCount()).uniqueResult(); } @Override public void save(Tag tag) { sessionFactory.getCurrentSession().save(tag); } @Override public void update(Tag tag) { sessionFactory.getCurrentSession().update(tag); } @Override public void delete(Tag tag) { sessionFactory.getCurrentSession().delete(tag); } }