All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.talk2object.plum.repository.biz.HibernateCollection Maven / Gradle / Ivy

There is a newer version: 0.1.22
Show newest version
package com.talk2object.plum.repository.biz;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;

import com.talk2object.plum.domain.repository.search.condition.Condition;
import com.talk2object.plum.repository.biz.interafce.Collection;
import com.talk2object.plum.repository.biz.interafce.Identifiable;

public class HibernateCollection implements
		Collection {
	private SessionFactory sessionFactory;
	private Class clazz;

	public HibernateCollection(SessionFactory sessionFactory, Class clazz) {
		this.sessionFactory = sessionFactory;
		this.clazz = clazz;
	}

	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}

	@Override
	public long size() {
		return (long) getSession().createCriteria(clazz)
				.setProjection(Projections.rowCount()).uniqueResult();

	}

	@Override
	public List getAll() {
		return getSession().createCriteria(clazz).list();
	}

	@Override
	public T get(Serializable id) {
		return (T) getSession().get(clazz, id);
	}

	@Override
	public List getByPage(int pageSize, int pageNumber) {
		int intPageNumber = (int) pageNumber;
		Criteria criteria = getSession().createCriteria(clazz);
		criteria.setFirstResult(pageSize * (pageNumber - 1));
		criteria.setMaxResults(pageSize);
		List list = criteria.list();
		return list;
	}

	@Override
	public void add(T entity) {
		getSession().save(entity);
	}

	@Override
	public void update(T newEntity) {
		getSession().update(newEntity);

	}

	@Override
	public void remove(T entity) {
		getSession().delete(entity);

	}

	@Override
	public List find(Condition condition) {
		// TODO Auto-generated method stub
		return null;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy