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.Categories;

public class CategoriesDao {
	private SessionFactory factory;

	public CategoriesDao(SessionFactory factory) {
		super();
		this.factory = factory;
	}
	
	public int saveCategories(Categories cat) {
		Session session = this.factory.openSession();
		Transaction tx = session.beginTransaction();
		int catId = (int) session.save(cat);
		tx.commit();
		
		
		session.close();
		return catId;
		
	}
	
	public List<Categories> getCategoriees(){
		
		
		Session s = this.factory.openSession();
		
		Query<?> query = s.createQuery("from Categories");
		@SuppressWarnings("unchecked")
		List<Categories> list = (List<Categories>) query.list();
		
		return list;
	}
	
	public Categories getCategoriesById(int cid) {
		Categories cat=null;
		try {
			Session session = this.factory.openSession();
			cat = session.get(Categories.class, cid);
			session.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		return cat;
		
	}

}
