package ${entity.getPackage('dao.hibernate')};

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

import org.asyrinx.brownie.core.query.BasicSelectBuilder;
import org.asyrinx.brownie.core.query.Select;
import org.asyrinx.brownie.core.query.SelectBuilder;
import org.asyrinx.brownie.core.query.StatementType;
import org.asyrinx.brownie.core.query.exp.IExpression;
import org.seasar.hibernate.S2SessionFactory;

import ${entity.getPackage('dao')}.*;
import ${entity.getPackage('entity')}.*;

#set( $daoFqn = "${entity.getPackage('dao')}.${daoPrefix}${entity.getName()}${daoSuffix}" )
#set( $entityFqn = "${entity.getPackage('entity')}.${entity.getName()}" )

/** 
 *
#if ($addTimeStamp)
 * [$now]
#end
 */
public class ${daoHibernatePrefix}${entity.getName()}${daoHibernateSuffix} implements ${daoPrefix}${entity.getName()}${daoSuffix} {

	/**
	 * 
	 */
	public ${daoHibernatePrefix}${entity.getName()}${daoHibernateSuffix}(S2SessionFactory sessionFactory) {
		super();
		this.sessionFactory = sessionFactory;
	}

    protected final S2SessionFactory sessionFactory;

    /*
     * (non-Javadoc)
     * 
     * @see ${daoFqn}#delete(${entityFqn})
     */
    public void delete(${entity.getName()} obj) {
        sessionFactory.getSession().delete(obj);
    }

    /*
     * (non-Javadoc)
     * 
     * @see ${daoFqn}#save(${entityFqn})
     */
    public void save(${entity.getName()} obj) {
        sessionFactory.getSession().saveOrUpdate(obj);
    }

    /*
     * (non-Javadoc)
     * 
     * @see ${daoFqn}#load(java.io.Serializable)
     */
    public ${entity.getName()} load(Serializable key) {
        if (key == null)
            return null;
        return (${entity.getName()}) sessionFactory.getSession().load(${entity.getName()}.class, key);
    }

    /*
     * (non-Javadoc)
     * 
     * @see ${daoFqn}#select(org.asyrinx.brownie.core.query.exp.IExpression)
     */
    public List select(IExpression expression) {
        final Select select = new Select();
        select.getFroms().add(${entity.getOriginal().getName().toUpperCase()}.alias());
		select.getWhere().add(expression);
		select.prepareTableAlias();
        final SelectBuilder builder = new BasicSelectBuilder(StatementType.HQL);
        return sessionFactory.getSession().find(builder.buildSelect(select));
    }

}
