package org.sistema.hibernate.sql.dao;

import java.util.List;

import org.hibernate.Query;
import org.sistema.hibernate.sql.models.Product;

/**
 * implementation of ProductDAO
 * 
 * @author Eugenia Pérez
 * @email eugenia_perez@cuatrovientos.org
 *
 */
public class ProductDAO extends GenericDAO<Product> {

	/**
	 * Selects all products with empty description using Native SQL
	 * 
	 * @return List of Object[]
	 */
	public List<Object[]> selectEmptyDescription() {

		startTransaction();
		
		Query query = session
				.createSQLQuery("Select * FROM product p WHERE p.description='' OR p.description is null")
				  .addScalar("id")
					.addScalar("name");
		
		List<Object[]> products = query.list();
		
		endTransaction();
		
		return products;
	}

	/**
	 * Selects all products of given name Native SQL style
	 * 
	 * @return List of products
	 */
	public List<Product> selectName(String name) {
		
		startTransaction();

		Query query = session.createSQLQuery(
				"SELECT * FROM product p WHERE p.name= :name").addEntity(
				Product.class);

		query.setParameter("name", name);

		List<Product> products = query.list();

		endTransaction();

		return products;
	}

}
