package com.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.query.Query;


import com.entities.Category;

public class CategoryDao {
	private SessionFactory factory;

	public CategoryDao(SessionFactory factory) {
		super();
		this.factory = factory;
	}
	
	public int saveCategory(Category category) {
		
			
			Session session = this.factory.openSession();
			Transaction tx = session.beginTransaction();
			
			int catyId= (int)session.save(category);
			
			tx.commit();
			
			session.close();
			
		
		return catyId;
	}
	
public List<Category> getCategory(){
		
		
		Session s = this.factory.openSession();
		
		Query<?> query = s.createQuery("from Category");
		@SuppressWarnings("unchecked")
		List<Category> list = (List<Category>) query.list();
		
		return list;
	}
public Category getCategoryById(int cd) {
	Category cati=null;
	try {
		Session session = this.factory.openSession();
		cati = session.get(Category.class, cd);
		session.close();
	}catch(Exception e) {
		 
	}
	return cati;
	
}

public List<Category> getAllCategoryById(int cids){
	
	
	Session s = this.factory.openSession();
	
	Query<?> query = s.createQuery("from Category as a where a.categories.categoriesId =: id");
	query.setParameter("id", cids);
		
	@SuppressWarnings("unchecked")
	List<Category> list = (List<Category>) query.list();
	
	return list;
}

	

}
